Question

I have been trying to avoid storing any window handles as a global. Up to this point I have been fine.

I am using a keyboard hook procedure now, and I am trying to find, if there is a way, to pass my main windows handle to it.

If it can't really be done in a simple way, I can just make the main window handle global.

LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam);

Is there a way I can include the handle within one of the parameters?

Thanks.

Was it helpful?

Solution 2

No, there is not. The hook parameters are fixed, and all of them are used by the OS. You must store your handle globally. And if you are implementing the hook procedure in a DLL that is hooking multiple processes, you need to store that global in shared memory so every instance of the DLL can reach it.

OTHER TIPS

It appears you're looking for a way to pass a state object to your KeyboardProc. You can do that as described here, using a thunk object. This way, KeyboardProc can be a non-static member method of your C++ class, without global variables. It is a bit of a hack, but it's very convinent. A similar technique is used by ATL Library (CStdCallThunk in atlstdthunk.h). Because of certain issues with DEP (Data Execution Prevention), you'd be better off using the ATL implementation of thunks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top