Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinAPI: Non-dll data in hook function

Tags:

c++

dll

winapi

hook

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!

like image 997
QI3it Avatar asked May 17 '26 22:05

QI3it


1 Answers

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.

like image 143
manuell Avatar answered May 20 '26 12:05

manuell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!