InjectDLL

Every now and then I need to inject some code into a running program on Windows. This can be tricky but the easiest way to do it is to produce a DLL and then inject that DLL into the running process using OpenProcess(), VirtualAllocEx(), WriteProcessMemory(), CreateRemoteThread(), etc.

Here is a command line utility to inject a DLL into a running process, and here is the source. You need to know the PID of the running process, which is easily obtainable by using Sysinternals PsList or Process Explorer.

Also, you may find that sticking code in DllMain of your DLL is not a very good way to go.. for example, just doing a simple MessageBox() is not guarenteed to work in DllMain. Instead, use a global constructor, like this:

class CMakeNoise
{
public:
	CMakeNoise() {
		MessageBoxA(NULL, "hello", "hellodll", MB_OK);
	}
};

CMakeNoise noisy;

That way you let the C runtime take care of running your code on DLL startup and you can be sure that everything is setup for you. Here's a compiled DLL which uses this code.



QuantumG
<< back to my home page

I thought I heard you say something...
Your name: