c ++ - Fås ikke Window Procedure meddelelser efter tilslutning WH\_CALLWNDPROC

Indlæg af Hanne Mølgaard Plasc

Problem



Jeg har forsøgt at bruge en global krog, men hookproceduren modtog kun vinduet proceduremeddelelser til min programtråd, og målretning mod en bestemt applikation (tråd) resulterer i ingen meddelelser overhovedet.


Jeg bruger korrekt en funktion i en DLL til en ikke-lokal krog. Her er min app-kode.


#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);
}


Her er koden til DLL'en. Er der en grund til, at jeg ikke modtager nogen beskeder?


#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
", 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);
}

Bedste reference


Det ligner, at det skal fungere. Bare tjekke nogle forslag.



  • En 32-bit DLL vil kun koble 32-bit-processer. Og en 64-bit DLL vil kun køre 64-bit processer.

  • Prøv at indstille dwThreadId til 0 for at lave en global krog bare for at se, om det virker på den måde.

  • Sørg for, at hook-DLL'en kan findes og læses af målprocessen.


Andre referencer 1


Der var ikke noget problem, krogen installerede korrekt. Men jeg vidste ikke, at krogproceduren løber fra konteksten af ​​processen, hvis vinduesprocedure har fået en besked.