如何用DELPHI编程实现键盘模拟功能
如何用DELPHI编程实现键盘模拟功能
我将一“根证书”封装到EXE程序,运行这个EXE文件时,
将这个“根证书”从EXE释放出来后,直接用下列代码
//打开证书
WinExec('rundll32.exe cryptext.dll,CryptExtOpenCER TestRoot.cer',SW_SHOWNORMAL);
运行时必须用“鼠标操作”如图:
我现在问题是想通过“键盘模拟”方面的知识来实现这个操作,不再用“鼠标操作”就
是所为“自动安装”
[解决办法]
获取窗口句柄,发送消息。
或是用CreatProcess创建进程来代替WinExec
可以参考下:
- Delphi(Pascal) code
{根据进程ID获取窗口句柄}function GetHwndFromProcess(ProcessId: DWORD): HWND; function _EnumWindowsProc(P_HWND: Cardinal; lParam: Cardinal): Boolean; stdcall; var PID: DWORD; begin GetWindowThreadProcessId(P_HWND, @PID); if PCardinal(lParam)^ <> PID then Result := True else begin Result := False; PCardinal(lParam+4)^ := P_HWND; end; end;var Buffer: array[0..1] of Cardinal;begin Result := 0; Buffer[0] := ProcessId; Buffer[1] := 0; EnumWindows(@_EnumWindowsProc, Integer(@Buffer)); if Buffer[1] > 0 then Result := Buffer[1];end;procedure TForm1.Button1Click(Sender: TObject);var si:TStartupInfo; pi:TProcessInformation; hwin:HWND;begin FillChar(si, Sizeof(si), #0);//必须将si结构的成员初始化为0 si.cb := Sizeof(si); si.dwFlags := STARTF_USESHOWWINDOW; si.wShowWindow := SW_hide; if CreateProcess(pchar('rundll32.exe'),pchar('cryptext.dll,CryptExtOpenCER TestRoot.cer'), nil, nil, false, CREATE_NEW_CONSOLE, nil, nil, si, pi) then begin WaitForInputIdle(pi.hProcess, INFINITE); hwin:=GetHwndFromProcess(pi.dwProcessId);//窗口句柄 end;end;
[解决办法]
鼠标操作可以通过SetCursorPos设定鼠标位置和mouse_event模拟鼠标事件来实现,不过缺点是无法判断鼠标操作结果
另外也可以可以考虑用sendmessage来实现键盘和鼠标事件,取得句柄也可以用findwindow(通过窗体标题寻找窗口)或者getwindowfrompoint通过鼠标位置寻找窗口
[解决办法]
用tab切换焦点然后enter就行了吧