这是一个dll文件里的#include <windows.h>char GameName[]="war3.exe";
HHOOK m_hKbid=NULL;
LRESULT CALLBACK KeyboardProc(
int code, // hook code
WPARAM wParam, // virtual-key code
LPARAM lParam // keystroke-message information
)
{
return 1;
}HWND m_hWnd=FindWindow(0,"HTest.txt");
DWORD m_proId=NULL; //试过0
m_proId=GetWindowThreadProcessId(m_hWnd,NULL); //。出错地点。/*HWND m_hWnd=FindWindow(0,"HTest.txt"); //注释起来的这样也试过。
DWORD m_proId=NULL;
GetWindowThreadProcessId(m_hWnd,&m_proId);*/ // 。出错地点。void SetHook()
{
m_hKbid=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,GetModuleHandle("WarHook"),m_proId);
}现在的错误:
m_proId' : missing storage-class or type specifiers
m_proId' : redefinition; different basic types使用注释了的那三句时的错误:
GetWindowThreadProcessId' : missing storage-class or type specifiers
GetWindowThreadProcessId' : redefinition; different type modifiers
HHOOK m_hKbid=NULL;
LRESULT CALLBACK KeyboardProc(
int code, // hook code
WPARAM wParam, // virtual-key code
LPARAM lParam // keystroke-message information
)
{
return 1;
}HWND m_hWnd=FindWindow(0,"HTest.txt");
DWORD m_proId=NULL; //试过0
m_proId=GetWindowThreadProcessId(m_hWnd,NULL); //。出错地点。/*HWND m_hWnd=FindWindow(0,"HTest.txt"); //注释起来的这样也试过。
DWORD m_proId=NULL;
GetWindowThreadProcessId(m_hWnd,&m_proId);*/ // 。出错地点。void SetHook()
{
m_hKbid=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,GetModuleHandle("WarHook"),m_proId);
}现在的错误:
m_proId' : missing storage-class or type specifiers
m_proId' : redefinition; different basic types使用注释了的那三句时的错误:
GetWindowThreadProcessId' : missing storage-class or type specifiers
GetWindowThreadProcessId' : redefinition; different type modifiers
=======
这个出现了两次了。
赋值放到函数体内就可以了。现在的问题是,FindWindow() 这个函数怎么使用? 我获取不到句柄。
如果知道进程是war3.exe,或者知道了进程ID,怎么得到句柄?
=========
FindWindow要用窗口类名或是窗口标题
SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,GetModuleHandle("WarHook"),m_proId);
的最后一个参数是线程ID,不是进程ID,看清楚了在用
HHOOK m_hKeybd=0;
DWORD m_dPid=0;
DWORD m_dTid=0;void CWwwaDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(!m_hWar)
{
//m_hWar=::FindWindow(NULL,"Warcraft III");
m_hWar=::FindWindow(NULL,"试验.txt - 记事本");
//m_hWar=::FindWindow(NULL,"Notepad");
}
HWND hh=::GetForegroundWindow();
if(hh==m_hWar)
{
if(m_hKeybd==0)
{
m_dTid=::GetWindowThreadProcessId(m_hWar,&m_dPid);
char debug[20]={0};
sprintf(debug,"%d",&m_dTid);
keybd_event(VK_LMKUO,0,0,0);
keybd_event(VK_RMKUO,0,0,0);
m_hKeybd=SetWindowsHookEx(WH_KEYBOARD_LL,LowLevelKeyboardProc,GetModuleHandle("wwwa.exe"),m_dTid);
}
}
else
{
if(m_hKeybd)
{
UnhookWindowsHookEx(m_hKeybd);
m_hKeybd=0;
}
}
CDialog::OnTimer(nIDEvent);
}
m_dTid=::GetWindowThreadProcessId(m_hWar,&m_dPid);
进程ID可以取到。
但是为什么窗口句柄却一直是三个问号?
我真的很好奇了,昨天是句柄可以取到,进程和线程取不到。
C++真头疼啊。m_hKeybd=SetWindowsHookEx(WH_KEYBOARD_LL,LowLevelKeyboardProc,GetModuleHandle("wwwa.exe"),m_dTid);这一句,是否哪里错了?
我是在Dlg窗口的类里面直接写的钩子以及回调函数,但是钩子好像没安装成功了。
以前取不到线程ID的时候,也就是线程=0的时候,可以成功安装。
wwwa.exe是我程序的可执行进程名。