I'm trying to get messages from the process that loaded my DLL.
我正在尝试从加载我的DLL的进程中获取消息。
I've tried:
SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC)WndProc, hInstance, 0);
Which gives me error popups about how "Program X could not be started because Y.dll is missing from your system". This is the reason I put "safely" in the title.
这给了我关于“程序X无法启动因为你的系统中缺少Y.dll”的错误弹出窗口。这就是我把“安全”放在标题中的原因。
I've also tried:
我也尝试过:
SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC)WndProc, hInstance, threadId);
Where threadId
is the result of GetCurrentThreadId()
in my DLLMain. This one works, but I don't get any messages for the window (just a bunch of 512 and 1025).
其中threadId是我的DLLMain中GetCurrentThreadId()的结果。这个工作,但我没有得到窗口的任何消息(只是一堆512和1025)。
2 个解决方案
#1
4
Messages are related to threads and windows, your DLL might be loaded by processes. So there is no direct correspondence.
消息与线程和窗口相关,您的DLL可能由进程加载。所以没有直接的对应关系。
Hooks on the other hand are either global or per-thread.
另一方面,钩子是全局的或每个线程。
All together this means that you have to choose whether you want messages just from specific threads, or global (all threads in all processes in specific desktop) where you will filter out the process of your interest yourself.
总之,这意味着您必须选择是仅希望来自特定线程的消息,还是全局(特定桌面中所有进程中的所有线程),您将自己过滤掉您感兴趣的过程。
Your second code snippet is a thread hook. The first one is the global hook where you definitely do something wrong as successful hook itself does not cause error messages you mentioned.
你的第二个代码片段是一个线程钩子。第一个是全局钩子,你肯定做错了,因为成功的钩子本身不会导致你提到的错误信息。
#2
0
I'm guessing that you are calling these functions directly from inside DLLMain. There are serious limitations on which functions you can safely call from DLLMain, see the MSDN documentation on DLLMain about this.
我猜你是直接从DLLMain中调用这些函数。您可以从DLLMain安全地调用哪些函数存在严重的限制,请参阅DLLMain上的MSDN文档。
There is no explicit list of safe and unsafe functions, but it seems likely that the call to SetWindowsHookEx in your first code fragment is doing something that is forbidden in a DLLMain function, hence the failure for the DLL to load properly.
没有安全和不安全函数的明确列表,但似乎第一个代码片段中对SetWindowsHookEx的调用正在执行DLLMain函数中禁止的操作,因此DLL无法正确加载。
#1
4
Messages are related to threads and windows, your DLL might be loaded by processes. So there is no direct correspondence.
消息与线程和窗口相关,您的DLL可能由进程加载。所以没有直接的对应关系。
Hooks on the other hand are either global or per-thread.
另一方面,钩子是全局的或每个线程。
All together this means that you have to choose whether you want messages just from specific threads, or global (all threads in all processes in specific desktop) where you will filter out the process of your interest yourself.
总之,这意味着您必须选择是仅希望来自特定线程的消息,还是全局(特定桌面中所有进程中的所有线程),您将自己过滤掉您感兴趣的过程。
Your second code snippet is a thread hook. The first one is the global hook where you definitely do something wrong as successful hook itself does not cause error messages you mentioned.
你的第二个代码片段是一个线程钩子。第一个是全局钩子,你肯定做错了,因为成功的钩子本身不会导致你提到的错误信息。
#2
0
I'm guessing that you are calling these functions directly from inside DLLMain. There are serious limitations on which functions you can safely call from DLLMain, see the MSDN documentation on DLLMain about this.
我猜你是直接从DLLMain中调用这些函数。您可以从DLLMain安全地调用哪些函数存在严重的限制,请参阅DLLMain上的MSDN文档。
There is no explicit list of safe and unsafe functions, but it seems likely that the call to SetWindowsHookEx in your first code fragment is doing something that is forbidden in a DLLMain function, hence the failure for the DLL to load properly.
没有安全和不安全函数的明确列表,但似乎第一个代码片段中对SetWindowsHookEx的调用正在执行DLLMain函数中禁止的操作,因此DLL无法正确加载。