WRummery

Before I get down to the meat of the question let me explain what I'm trying to do. I am using Microsoft Detours Version 1.5 to hook into a few OpenAL functions. These functions are mainly alcOpenDevice and alSourcePlay. Now I do have access to the code that calls the functions, BUT my team mates don't want me to have to call loadlibrary in the main project, just to load a file that that is supposed intersept calls to functions that it makes. SO I was wondering if there was a way to link it to that project, or something without having to call loadlibrary in the main projects code.

Now I am well, aware that there is a Microsoft Detours verions 2.1, BUT when I compiled and tryed to use this version of detours instead, for some reason the DLL wouldn't even load up..... so I switched back to v1.5 and it works fine, besides the whole need to call loadlibrary thing.

So does anyone have any ideas as to how I can escape the loop-hole of having to call loadlibrary in my main project

Any suggestions/help is greatly appreciated.


Re: Visual C++ General Linking DLL to procject without modification

Bruno van Dooren

It depends.

First of all, the simplest way is to convince your team mates to take 2 minutes to program the call in exchange for a glass of beer or coffee.

Failing that, there are a couple of trick that can be used to inject a DLL into a running process.

One such trick is to use CreateRemoteThread to inject a new thread in your process, and have that thread perform the call to LoadLibrary.

This is a fairly simple approach that will do exacty what you want. There are others. Google should be able to find you more, but the beauty of CreateRemoteThread is that it is a documented API so you are not using any 'iffy' stuff that might have side effects.





Re: Visual C++ General Linking DLL to procject without modification

Simple Samples

You did not specify what version of VC you are using but version 2.1 of Detours definitely works with VC 2005. I however built Detours 2.1 using VC 2005; I don't know if the copy of Detours supplied with the source works as-is with VC 2005. In other words, you might have to build Detours.

I don't understand why you are intercepting alcOpenDevice and alSourcePlay. I do not need to know except it's relevance to LoadLibrary. I really don't understand how LoadLibrary is relevant to anything except you say you can't use it but I don't understand why you need it at all.






Re: Visual C++ General Linking DLL to procject without modification

Simple Samples

Bruno van Dooren wrote:
One such trick is to use CreateRemoteThread to inject a new thread in your process

Detours has a function that is quite easy to use that does all that. More specifically, it has a function that is essentially CreateProcess with a couple of added arguments for injecting the DLL. I forget what other functions it has that are relevant but it is likely that Detours can do whatever needs to be done that is truly needed.






Re: Visual C++ General Linking DLL to procject without modification

WRummery

Thanks Bruno and Simple, I am using Detours version 1.5, but am updating it to 2.1. I was pretty sure that 2.1 had what I needed. I have looked through the Detours documentation, and I think the function I'm looking for is DetourCreateProcessWithDll. I'm not sure if this is it. but the documentation states "Create a new process and load a DLL into it.." which makes me assume that if I set it up, I might be able to take out that LoadLibrary call.

As for why I'm detouring alSourcePlay and alcOpenDevice, I can't get into specifics, but its to filter out certain sounds and copy the rest of the sounds and play on a different sound card on the same machine.

Anyway, is this the function I'm looking for and should I just be able to throw that in my DllMain and have it work This is my first real assignment working with DLL's so I'm kinda green on all this.

Any help is greatly appreciated.




Re: Visual C++ General Linking DLL to procject without modification

Simple Samples

I have used DetourCreateProcessWithDll but I don't know what you are doing so I can't be sure it is what you need.

You are sure jumping in deep if this is your first experience with DLLs. I learned Windows programming when I wrote a Windows device driver, so I can understand doing things like that but I did it without the internet. Expect to spend time learning. Start by learning about DLLs.