开发者

How do I communicate from my global hook to my application?

开发者 https://www.devze.com 2022-12-19 11:21 出处:网络
I installed a global hook using win32 but I need to communicate with my main application. How should I do this? I guess the dll can have a function where I set my apps HWND and then the hook can sendm

I installed a global hook using win32 but I need to communicate with my main application. How should I do this? I guess the dll can have a function where I set my apps HWND and then the hook can sendmessage it? or should I search the window titles to find my application from the hook and sendmessage that way? What's the best strategy and is sendmessage the best way or 开发者_如何学Pythonshould I use something else to communicate with my global hook.


  • Use PostMessage, not SendMessage: SendMessage can take a while, and this may cause delays in other applications.

  • When initializing the hook, give your application's HWND in a to the hook DLL using a function you define. This should be stored in a shared data segment in the DLL, like this:

    #pragma data_seg(".shared")
    HWND hWndApp = NULL;
    #pragma data_seg()
    #pragma comment("linker, /section:.shared,rws")
    
    void setAppWnd(HWND hWnd) {
        hWndApp = hWnd;
    }
    

You can see a full example for doing this here.


What type of information are you trying to get back to your app?

SendMessage is ill-equipped for passing anything beyond DWORD's across process boundries (eg, you can't pass pointers to memory in one process to another process), but if all you really need to do is pass simple values it will work well enough.

As @interjay states, PostMessage is a better plan but ultimately still has the same restrictions.

Related: "Hazards of cross-process messages", "PasswordSpy - Retrieving lost passwords using Windows hooks"

0

精彩评论

暂无评论...
验证码 换一张
取 消