Reputation: 181
I want to install a hook (on Windows using the WinAPI and C++) to get key-input events that are sent to the WindowProc of a specific process/thread (known to my program by process ID). As far as I understood I have to put the hook procedure into a DLL. So far everything is fine for me. But the hook procedure needs to use data from the program that installed the hook. Now I don't know how to access this data from my hook procedure inside the DLL.
My first thoughts were to maintain a data struct inside the DLL itself and update it from outside by calls to another function placed inside the DLL. But I am not sure, how exactly to do that (for example: I assume this data struct would have to be shared data so that it is the same for all calls, no matter from which process/thread, but I am not sure about it).
I've looked at a few examples on how to implement hooks but these examples never used data from the original program that installed the hook (or any other 'user-code').
I would really appreciate it when somebody could explain this to me or even give me a brief outline on how to solve the problem described above (and whether my approach is right).
Many thanks in advance!
Upvotes: 3
Views: 359
Reputation: 7620
You may use Shared Data section
// dll.cpp
#pragma data_seg("myshared")
int iShared;
#pragma data_seg()
#pragma comment(linker, "/section:myshared,RWS")
Export a function from the DLL, allowing to pass the value(s) to be used for the variable(s) in the section. Call that function from your hooking EXE (before hooking). The instance DLL in the hooked process will see the value(s) set by the hooking EXE.
Upvotes: 2
Reputation: 23495
What you really are describing is inter-process communication(IPC): http://en.wikipedia.org/wiki/Inter-process_communication
Your options for windows are to create a socket, a pipe, or shared-memory and use mutexes, semaphores, or events for synchronisation.
Other than that, there is no way to call functions in a dll injected into another process.
You can view an example I posted here: Two separate processes sharing the same Camera feed OpenCv
Upvotes: 1