Win32 C++ Can Extra Info be Sent with a Procedure

主宰稳场 提交于 2019-12-12 16:00:07

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/18324296/win32-c-can-extra-info-be-sent-with-a-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!