0
我想在所有线程上设置一个全局的GetMessage钩子。这是我的DLL:WH_GETMESSAGE全局钩子不工作
#include <windows.h>
__declspec(dllexport) LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
{
MessageBeep(0);
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
正如你所看到的,它并不多。我只是希望它在调用MessageBeep时调用它。
#include <windows.h>
typedef LRESULT (CALLBACK *LPGetMsgProc)(int nCode, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int nCmdShow)
{
if(!(HMODULE hDll = LoadLibrary("library.dll")))
return 1;
if(!(LPGetMsgProc pfnProc = (LPGetMsgProc)GetProcAddress(hDll, "[email protected]")))
return 2;
HHOOK hMsgHook = SetWindowsHookEx(WH_GETMESSAGE, pfnProc, hInstance, 0);
MSG msg;
while(GetMessage(&msg, NULL, 0, 0) > 0) {}
UnhookWindowsHookEx(hMsgHook);
return 0;
}
我的WinMain加载库,获取过程并设置钩子。但是,MessageBeep永远不会被调用。有什么我在这里做错了吗?
此外,另一件事一直困扰着我。在此调用中:
if(!(LPGetMsgProc pfnProc = (LPGetMsgProc)GetProcAddress(hDll, "[email protected]")))
我被迫使用“GetMsgProc @ 12”,因为我无法以任何其他方式正确使用它。有人可以告诉我我应该如何使用.def文件或其他东西,所以我可以把它作为“GetMsgProc”?虽然MSDN声明,因为我在我的声明中有__declspec(dllexport),所以我不需要它...
我的IDE是Code :: Blocks with MinGW。提前致谢。
要回答你的最后一个问题,你想使用'extern“C”'来防止名称混乱。虽然不确定第一个;你是否证实你实际上正在发送任何消息? – Shog9 2010-07-09 03:05:00
这两个DLL和EXE编码和编译在C中,所以我不能使用extern“C”。我认为我可以切换到任何程序,点击任何地方发送消息,它会调用我的GetMsgProc。但是,由于没有调用MessageBeep,所以情况并非如此。 – kaykun 2010-07-09 03:10:27
呃,反正我错了。请参阅:http://stackoverflow.com/questions/366228/def-files-c-c-dlls – Shog9 2010-07-09 03:20:07