线程键盘钩子实例-游戏窗口中呼出外挂(VC、SDK)
///////////////////////
//GameDll.cpp
//生成DLL文件
//////////////////////
#include <windows.h> #include "resource.h" HHOOK hkGame; int CALLBACK DlgProc(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam) { switch(uMsg) { case WM_CLOSE: { MessageBox(0,"关闭对话框",0,0); EndDialog(hDlg,0); } break; } return 0; } LRESULT CALLBACK KeyboardProc( int code, // hook code WPARAM wParam, // virtual-key code LPARAM lParam // keystroke-message information ) { if (code<0) return CallNextHookEx(hkGame,code,wParam,lParam); if (wParam==VK_F10 && lParam&0x40000000) { MessageBox(0,"F10被按下",0,0); DialogBox(GetModuleHandle("GameDll.dll"),MAKEINTRESOURCE(IDD_DIALOG1),FindWindow(NULL,"MyGame"),DlgProc); //第一个参数也可以写作:(HMODULE)LoadLibrar("GameDll.dll") return 1; } return 0; } extern "C" __declspec(dllexport) void StartHook() { DWORD TID=GetWindowThreadProcessId(FindWindow(NULL,"MyGame"),NULL); if (!TID) { MessageBox(0,"没打开游戏!",0,MB_OK); return; } hkGame=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,GetModuleHandle("GameDll.dll"),TID); if (!hkGame) MessageBox(0,"HOOK失败!",0,MB_OK); }
// EXE.cpp : Defines the entry point for the application. //注入器 #include "stdafx.h" #include "resource.h" #pragma comment(lib,"GameDll.lib") __declspec(dllimport) void StartHook(); int CALLBACK DlgProc(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam) { switch (uMsg) { case WM_CLOSE: EndDialog(hDlg,0); break; case WM_COMMAND: { if (LOWORD(wParam)==IDB_HOOK) { StartHook(); } } break; } return 0; } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // TODO: Place code here. return DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,DlgProc); }