在client.cpp中包含wgdlg.h
CDLGWG * GameForm = NULL; HWND GameHwnd = NULL; //钩子回调函数 LRESULT CALLBACK GameProc(int nCode,WPARAM wParam,LPARAM lParam) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); PMSG pMsg=(PMSG)lParam; if (pMsg->message==WM_KEYUP) { if (pMsg->wParam==VK_HOME) { if (GameForm==NULL) { GameHwnd = pMsg->hwnd; GameForm=new CDLGWG(); GameForm->Create(IDD_DIALOG_WG); GameForm->ShowWindow(TRUE); } else if (GameForm->IsWindowVisible()) { GameForm->ShowWindow(FALSE); } else { GameForm->ShowWindow(TRUE); } } } return CallNextHookEx(0,nCode,wParam,lParam); } //安装钩子 void StartHook() { AFX_MANAGE_STATE(AfxGetStaticModuleState()); HWND hGame = FindWindow(_T("TianLongBaBu WndClass"),NULL); if (hGame==0) { MessageBox(NULL,_T("未找到游戏"),_T("提示"),NULL); return; } DWORD tid=GetWindowThreadProcessId(hGame,NULL); ::SetWindowsHookEx(WH_GETMESSAGE,GameProc,::GetModuleHandle(_T("GameDll.dll")),tid); } ; GameDll.def : 声明 DLL 的模块参数。 LIBRARY EXPORTS ; 此处可以是显式导出 StartHook //调用钩子处导出函数声明 #pragma comment(lib,"GameDll.lib") _declspec(dllimport) void StartHook();
//退出时不保证游戏崩溃
在dll头文件中类的重写属性中添加退出事件
int CGameDllApp::ExitInstance()
{
if (GameHwnd!=0)
{
delete GameForm;
GameForm= NULL;
}
return CWinApp::ExitInstance();
}