c语言安装全局消息钩子的源代码
1 #include <stdio.h> 2 #include <conio.h> 3 #include <windows.h> 4 #define DEF_DLL_NAME "keyHook.dll" 5 #define DEF_FUN_NAME "keyboardPrc@12" 6 7 8 9 int APIENTRY WinMain(HINSTANCE hInstance, 10 HINSTANCE hPrevInstance, 11 LPSTR lpCmdLine, 12 int nCmdShow) 13 { 14 int hwndDOS = GetForegroundWindow(); 15 ShowWindow(hwndDOS, SW_HIDE); 16 HMODULE holl=NULL; 17 HOOKPROC process = NULL; 18 19 holl = LoadLibraryA(DEF_DLL_NAME); 20 if(holl==NULL){ 21 printf("导入库失败"); 22 } 23 process = (HOOKPROC)GetProcAddress(holl,DEF_FUN_NAME); 24 if(process==NULL){ 25 printf("导入函数失败"); 26 } 27 SetWindowsHookEx(WH_KEYBOARD,process,holl,0); 28 _getch(); 29 30 return 0; 31 }
上面是主函数的代码,下面是keyHook.dll的代码
注意dll与主文件一定要放在同一个文件夹里,效果是键盘会失灵,打开任务管理器把进程关掉就行
#include <stdio.h> #include <windows.h> __declspec(dllexport) LRESULT CALLBACK keyboardPrc(int code,WPARAM wParam,LPARAM lParam){ return (long)1; }
窗口是隐藏的,会有一闪而过。