I'm using SetWindowsHookEx with WH_CALLWNDPROC to catch all WndProc messages - and it works fine. I was wondering if there is a way to store a parameter somewhere where the callback function can read and use it.
I assume that because the callback function is inside the "other" process, there's no connection to the process calling the SetWindowsHookEx, so just storing a value in a static variable wont do any good..
For example:
void Hook(DWORD dwThread, int randomNumber)
{
MagicallyStore(randomNumber);
SetWindowsHookEx(WH_CALLWNDPROC, hookProc, GetModuleHandle(L"WNDProcHooks"), dwThread);
...
...
}
LRESULT CALLBACK hookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
int randomNumber = MagicallyRestore();
DoSomthing(randomNumber);
return CallNextHookEx(0, nCode, wParam, lParam);
}
I already thought about having the "MagicallyStore" function write the parameter to a file and reading it once in the "MagicallyRestore" function - but there must be a better way..
Thanks in advance :)
thread_local
keyword is not available in older IDEs. Plus there are issues with it in DLLs with Visual Studio. But, if that is not the concern, then yes, it's a shorter version. What I showed is a lower-level API implementation of the same thing. As for your multi-threaded point, then your're right. I assumed that he is calling it on the same process because ofdwThread
. Otherwise, he needs an std::map with the keys as thread ID, secured by a mutex. Or, then yes, use a shared memory in a DLL. – Derbyshire