实现功能:在现有DLL程序中向特定的EXE窗口中发送模拟键盘的消息
使用API根据窗口标题递归查找特定的窗口句柄,之后模拟调用。
注意:keybd_event函数不能在VS下使用,所以用SendInput代替使用:
int SelfFindWindows(HWND hMainWnd, char* lpName, BYTE keyvalue)
{
HWND hChildWnd = GetWindow(hMainWnd, GW_CHILD); while (hChildWnd != NULL)
{
char lpChildString[] ={};
GetWindowText(hChildWnd, lpChildString, sizeof(lpChildString)); if (strstr(lpChildString, lpName))
{
SetForegroundWindow(hChildWnd);
SetFocus(hChildWnd); /*
keybd_event(keyvalue, 0, 0, 0);
keybd_event(keyvalue, 0, KEYEVENTF_KEYUP, 0);
*/
INPUT input[];
memset(input, , sizeof(input));
//按下 向下方向键
input[].ki.wVk = keyvalue;
input[].type = INPUT_KEYBOARD;
//松开 向下方向键
input[].ki.wVk = keyvalue;
input[].type = INPUT_KEYBOARD;
input[].ki.dwFlags = KEYEVENTF_KEYUP;
//该函数合成键盘事件和鼠标事件,用来模拟鼠标或者键盘操作。事件将被插入在鼠标或者键盘处理队列里面
SendInput(, input, sizeof(INPUT));
return ;
} if (GetWindow(hChildWnd, GW_CHILD))
{
if (SelfFindWindows(hChildWnd, lpName, keyvalue))
{
return ;
}
}
else
hChildWnd = GetWindow(hChildWnd, GW_HWNDNEXT);
} return ;
} int SendKeyEventToEXE()
{
HWND hDesk = GetDesktopWindow();
HWND hWnd = GetWindow(hDesk, GW_CHILD); while (hWnd != NULL)
{
char lpString[] ={};
GetWindowText(hWnd, lpString, sizeof(lpString)); if (strstr(lpString, "Foxit Reader"))
{
SelfFindWindows(hWnd, "Reader", VK_NEXT); return ;
} hWnd = GetWindow(hWnd, GW_HWNDNEXT);
}
return ;
}
附录: