以下是DLL中的代码:
DxfWGDlg *pGameDlg = NULL;
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
if(wParam == VK_HOME && ((lParam&(1<<31))==0))
{
::AfxMessageBox("Home按下");
if (pGameDlg == NULL)
{
pGameDlg = new DxfWGDlg();
pGameDlg->Create(IDD_DIALOG1);
pGameDlg->ShowWindow(SW_SHOW);
}
}
return ::CallNextHookEx(g_hhook, nCode, wParam ,lParam);
}
HMODULE WINAPI ModuleFromAddress(PVOID pv)
{
MEMORY_BASIC_INFORMATION mbi;
if(::VirtualQuery(pv,&mbi,sizeof(mbi))!=0)
return (HMODULE)mbi.AllocationBase;
else
return NULL;
}
bool ThreadList(DWORD Pid)
{
HANDLE hThreadSnap;
THREADENTRY32 th32;
hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,NULL);
DWORD index = 0;
bool bFound = false;
if (hThreadSnap == INVALID_HANDLE_VALUE)
{
return false;
}
th32.dwSize = sizeof(THREADENTRY32);
if (!Thread32First(hThreadSnap, &th32))
{
CloseHandle(hThreadSnap);
return false;
}
do
{
if ( Pid == th32.th32OwnerProcessID)
{
if(!bFound )bFound=true;
if(!(g_hhook = ::SetWindowsHookEx(WH_KEYBOARD, KeyboardProc,ModuleFromAddress(KeyboardProc),th32.th32ThreadID)))
{
bFound = false;
break;
}
}
}while(Thread32Next(hThreadSnap, &th32));
CloseHandle(hThreadSnap);
return bFound;
}
bool SetHook()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
if (g_hhook == NULL)
{
DWORD Pid,ThreadId;
ThreadId = GetWindowThreadProcessId(::FindWindow(NULL,"计算器"),&Pid);
if(Pid == 0)
{
AfxMessageBox("获取进程失败");
return FALSE;
}
ThreadList(Pid);
//ThreadId = GetWindowThreadProcessId(::FindWindow(NULL,"计算器"),NULL);
//原先这里用的这种方式但是不行,后来在在网上搜了下改成现在的遍历线程的方法还是失败
if (g_hhook != NULL)
{
return TRUE;
}
}
return FALSE;
}
最后发现其实钩子函数根本执行不进去的,很困扰啊,求教解决方法!!
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课