Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get data from hookproc function winapi (C++)

I have a program that sets a hook on another window. I want this program to know if my hook function received a message. How can I do it?

I’ve had an idea to send message from hook function to my main window, but I don’t know how to pass the HWND handler. Also I don’t want to use EnumWindows or FindWindow to find my main window.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The simplest solution is to store your target HWND in shared memory that both the main program and the hook can access.

If your compiler supports #pragma data_seg() (ie, MSVC), you can use that to declare an HWND variable in a shared code section in a DLL. Have your main program and your hook use the same DLL. See Process shared variable #pragma data_seg usage.

Otherwise, you can use CreateFileMapping()+MapViewOfFile() to allocate a block of memory at runtime, and then store/read the HWND value from it. See Creating Named Shared Memory.

Either way, the main program can then assign the shared hwnd, and the hook can read it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading