cplusplus
cplusplus

Reputation: 614

Not getting Window Procedure messages after hooking WH_CALLWNDPROC

I've tried using a global hook but the hookprocedure only recieved window procedure messages for my program thread, and targeting a specific application (thread) results in no messages at all.

I'm properly using a function in a DLL for a non-local hook. Here is my app code.

#include <Windows.h>
#include <stdio.h>

HINSTANCE hinst;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int main() {
    HWND notepad = FindWindow(NULL, L"Untitled - Notepad");

    if (!notepad)
        return 0;

    hinst = GetModuleHandle(NULL);

    // create a window class:
    WNDCLASS wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hinst;
    wc.lpszClassName = L"hooking";

    // register class with operating system:
    RegisterClass(&wc);

    // create and show window:
    HWND hwnd = CreateWindow(L"hooking", L"hooking", WS_OVERLAPPEDWINDOW, 0, 0, 500, 400, NULL, NULL, hinst, NULL);

    if (hwnd == NULL) {
        return 0;
    }

    ShowWindow(hwnd, SW_SHOW);

    DWORD threadID = GetWindowThreadProcessId(notepad, NULL);

    HINSTANCE hinstDLL = LoadLibrary(TEXT("..\\Debug\\ProcHookDLL.dll"));

    void (*AttachHookProc)(DWORD);
    AttachHookProc = (void (*)(DWORD)) GetProcAddress(hinstDLL, "AttachHook"); 
    AttachHookProc(threadID);

    // handle messages:
    MSG msg = {};

    while(GetMessage(&msg, hwnd, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    printf("Done execution... press any key to exit");
    char garbage = getchar();
    return 0;
}


LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    if (uMsg == WM_DESTROY) {
        PostQuitMessage(0);
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

Here is the code for the DLL. Is there a reason i'm not receiving any messages?

#include <Windows.h>
#include <stdio.h>

// TODO: create a mutex so this can only be loaded once
HMODULE thisModule;
HHOOK hook;
LRESULT CALLBACK LaunchListener(int nCode, WPARAM wParam, LPARAM lParam);

BOOL APIENTRY DllMain(HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
{
    thisModule = hModule;

    // Very restricted set of things that can be done in DllMain, refer to documentation
    // before adding anything here.

    switch (ul_reason_for_call) {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
        break;
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

#ifdef __cplusplus    // If used by C++ code, 
extern "C" {          // we need to export the C interface
#endif
//window message loop is necessary for hooks to work? (didn't work with console app)
//provide function pointer to execute when notepad is launched.
__declspec(dllexport) void AttachHook(DWORD threadID) {
    hook = SetWindowsHookEx(WH_CALLWNDPROC, LaunchListener, thisModule, threadID);
}
#ifdef __cplusplus
}
#endif

LRESULT CALLBACK LaunchListener(int nCode, WPARAM wParam, LPARAM lParam) {
    // process event here
    if (nCode >= 0) {
        //wparam specifies if the message was sent by the current thread or not.
        CWPSTRUCT * cwp = (CWPSTRUCT *)lParam;
        wchar_t windowName[256];
        GetWindowText(cwp->hwnd, windowName, 256);
        wprintf(L"%#8X: %s\n", cwp->message, windowName);
        if (cwp->message == WM_CREATE) {
            __debugbreak();
            wchar_t moduleName[256];
            //cwp->hwnd
            //GetModuleFileName(0, moduleName, 256);
            GetWindowText(cwp->hwnd, moduleName, 256);
            int x = 0;
            x++;
        }
    }

    return CallNextHookEx(NULL, nCode, wParam, lParam);
}

Upvotes: 2

Views: 9507

Answers (2)

cplusplus
cplusplus

Reputation: 614

There was no problem, the hook installs correctly. However I didn't know that the hook procedure runs from the context of the process whose window procedure has gotten a message.

Upvotes: 2

Nick Whaley
Nick Whaley

Reputation: 2799

Looks like it should work. Just checking a few suggestions.

  • A 32-bit DLL will only hook 32-bit processes. And a 64-bit DLL will only hook 64-bit processes.
  • Try setting dwThreadId to 0 to make a global hook just to see if it works that way.
  • Make sure the hooking DLL can be found and is readable by the target process.

Upvotes: 1

Related Questions