DLL Injection

valente500 valente500 at live.com
Tue Feb 7 07:00:05 PST 2012


I've been trying for a while now to inject a DLL written in D 
into another process, and I just haven't been able to get it 
working.

Here's the code for the DLL:


import std.c.windows.windows;
import core.sys.windows.dll;

__gshared HINSTANCE g_hInst;

extern (Windows)
BOOL DllMain(HINSTANCE hInstance, ULONG ulReason, LPVOID 
pvReserved)
{
    switch (ulReason)
    {
        case DLL_PROCESS_ATTACH:
			g_hInst = hInstance;
			dll_process_attach(hInstance, true);
			
			*cast(int*)0x22FF3C = 1337;
	    break;

        case DLL_PROCESS_DETACH:
			dll_process_detach(hInstance, true);
	    break;

        case DLL_THREAD_ATTACH:
			dll_thread_attach(true, true);
	   	break;

        case DLL_THREAD_DETACH:
			dll_thread_detach(true, true);
		break;
			
		default: break;
    }
    return true;
}



Basically I just copy and pasted the code from the DLL tutorial 
on the D website and added the *cast(int*)0x22FF3C = 1337; line. 
The process I'm injecting it into has a value at that address 
which I want to change.

The problem is that when I inject the DLL into the process with 
Winject (a DLL Injector), the value changes fine, but Winject 
complains "Injection seemed successful, but DLLMain() never 
returned (TIMEOUT)", and then the process crashes 30 seconds 
later.

I also tested it with another injector, and the process just 
freezes. So I'm wondering how to write a DLL that injects 
successfully.

Cheers.


More information about the Digitalmars-d-learn mailing list