using the EnumWindows method i posted before i realized that it created an endless loop until FALSE is returned which could be a problem. after alot of reading and trial and error i came up with a way to get the window handle of the process you've injected your DLL into.
Code:
#define T TEXT
LONG oWndProc;
DWORD threadID;
HWND hijHwnd;
BOOL CALLBACK WinEnum(HWND hwnd, LPARAM lParam)
{
if (hwnd == NULL)
MessageBox(HWND_DESKTOP, T("Error getting window"),T("Error"), MB_OK);
else
{
DWORD processId;
GetWindowThreadProcessId(hwnd, &processId);
if (processId == GetCurrentProcessId()) {
hijHwnd = hwnd;
return FALSE;
}
}
return TRUE;
}
DWORD WINAPI thread(LPVOID)
{
HWND hwnd;
EnumWindows(WinEnum, NULL);
if (hijHwnd != NULL) {
hwnd = hijHwnd;
//just printing the window name just for an example
char *wName = (char *)malloc(sizeof(char) * MAX_PATH);
GetWindowText(hwnd, LPWSTR(wName), MAX_PATH);
MessageBox(NULL, LPCWSTR(wName), TEXT("Window Name"), MB_OK | MB_ICONINFORMATION);
free(wName);
oWndProc = SetWindowLong(hwnd, GWL_WNDPROC, (long)nWndProc);
}
return TRUE;
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&thread, 0, 0, &threadID);
}
break;
case DLL_THREAD_ATTACH:
{
}
break;
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}