[WINDOWS开发]钩子的使用,windows信息的获取
钩子的使用。
很多时候,我们需要获取windows信息。类似于Spy++的消息获取。
1 创建DLL工程。(以下均以C++ BUILDER作为开发工具操作)
关注1:SetWindowsHookEx。参考百度百科:http://baike.baidu.com/link?url=5hwGRs-C78II5Ih87ZSH33_ZG7FByHPQ8h8fZ6IGoRRJdlQxElP-169CfENNrV751C73AliUO0StR8wFwM4-aK
关注2:现在测试,只能通过DLL使用SetWindowsHookEx。
以下为源码:
//--------------------------------------------------------------------------- #include <windows.h> #include <winuser.h> #include <tchar.h> #include <stdio.h> //--------------------------------------------------------------------------- // Important note about DLL memory management when your DLL uses the // static version of the RunTime Library: // // If your DLL exports any functions that pass String objects (or structs/ // classes containing nested Strings) as parameter or function results, // you will need to add the library MEMMGR.LIB to both the DLL project and // any other projects that use the DLL. You will also need to use MEMMGR.LIB // if any other projects which use the DLL will be performing new or delete // operations on any non-TObject-derived classes which are exported from the // DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling // EXE's to use the BORLNDMM.DLL as their memory manager. In these cases, // the file BORLNDMM.DLL should be deployed along with your DLL. // // To avoid using BORLNDMM.DLL, pass string information using "char *" or // ShortString parameters. // // If your DLL uses the dynamic version of the RTL, you do not need to // explicitly add MEMMGR.LIB as this will be done implicitly for you //--------------------------------------------------------------------------- #pragma argsused HINSTANCE hinDLL = NULL; HHOOK g_hHook = NULL; BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved) { hinDLL = hinstDLL; return 1; } //--------------------------------------------------------------------------- extern "C" __declspec(dllexport) int __stdcall DllTest() { return -3; } extern "C" __declspec(dllexport) int __stdcall FreeHook() { if( g_hHook!=NULL ) { UnhookWindowsHookEx( g_hHook ) ; g_hHook = NULL ; } else { return -1; } return 1; } LRESULT CALLBACK GetWndHookProc(int code, WPARAM wParam, LPARAM lParam) { if(code<0) return CallNextHookEx(g_hHook,code,wParam,lParam); if(code!=HC_ACTION) return CallNextHookEx(g_hHook,code,wParam,lParam); LPCWPSTRUCT pmsg=(CWPSTRUCT FAR *)lParam; switch(pmsg->message) { case 49728: //WM_HTML_GETOBJECT { char buf[256]; FILE* ff = fopen("d:\\SaveLog.txt", "a"); wsprintf(buf, "WM_HTML_GETOBJECT HWND = 0x%08X\r\n", pmsg->hwnd); fputs(buf, ff); fclose(ff); } break; default: break; } return CallNextHookEx(g_hHook, code, wParam, lParam); } extern "C" __declspec(dllexport) int __stdcall StartHook2(DWORD threadid) { if( g_hHook!=NULL ) { UnhookWindowsHookEx( g_hHook ) ; g_hHook = NULL ; } g_hHook = ::SetWindowsHookEx(WH_CALLWNDPROC,(HOOKPROC)GetWndHookProc, hinDLL, threadid); if(g_hHook==NULL) return GetLastError(); return 1; }
2 添加Form工程,调试
以下为代码
if(hdll) FreeLibrary(hdll); hdll=LoadLibrary("HookTest.dll"); if(hdll==NULL) { ShowMessage("Error"); return; } int __stdcall (*StartHook2)( DWORD threadid); //定义函数原型 StartHook2 = (int __stdcall (*)( DWORD threadid))::GetProcAddress(hdll,"StartHook2"); ShowMessage(StartHook2(0));