views:

61

answers:

3

Hi there, I'm trying to get the handle to the process which loaded a dll from the dll.

My approach is: in DLL_PROCESS_ATTACH I call EnumWindows(EnumWindowsProc,NULL);

my EnumWindowsProc implementation is the following:

BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) {
    if(GetCurrentProcessId() == GetWindowThreadProcessId(hWnd,NULL)){
        MessageBox(hWnd,L"I loaded your dll!",L"it's me",MB_OK);
        return TRUE;
}
    return FALSE;
}

the problem is that GetCurrentProcessId() == GetWindowThreadProcessId(hWnd,NULL) is never true (if i place the messagebox call outside the if block everything works but it gets called once for every listed window).

Is there any other way to get to the point? Is this approach totally wrong or am I just missing something?

Thanx in advance

+1  A: 

Easiest way would be to simply use GetCurrentProcess whenever you need the handle.

monoceres
A: 

Try calling GetProcessHandleFromHwnd().

ntcolonel
+2  A: 

Use GetCurrentProcess, which returns a pseudo-handle to the current process. If you need a real handle, pass in the pseudo-handle to DuplicateHandle.

Note that it is very dangerous to do too much in DllMain. Calling anything other than KERNEL32 functions is quite dangerous, and even then there are some KERNEL32 functions that you shouldn't be calling. See the DllMain documentation, this document, and several blog posts from Microsoft developers recommending against doing too much in DllMain.

Aaron Klotz