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 should 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"