如何用DELPHI编程实现键盘模拟功能
我将一“根证书”封装到EXE程序,运行这个EXE文件时,
将这个“根证书”从EXE释放出来后,直接用下列代码
//打开证书
WinExec('rundll32.exe cryptext.dll,CryptExtOpenCER TestRoot.cer',SW_SHOWNORMAL);
运行时必须用“鼠标操作”如图:我现在问题是想通过“键盘模拟”方面的知识来实现这个操作,不再用“鼠标操作”就
是所为“自动安装”
我将一“根证书”封装到EXE程序,运行这个EXE文件时,
将这个“根证书”从EXE释放出来后,直接用下列代码
//打开证书
WinExec('rundll32.exe cryptext.dll,CryptExtOpenCER TestRoot.cer',SW_SHOWNORMAL);
运行时必须用“鼠标操作”如图:我现在问题是想通过“键盘模拟”方面的知识来实现这个操作,不再用“鼠标操作”就
是所为“自动安装”
或是用CreatProcess创建进程来代替WinExec
可以参考下:{根据进程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;
另外也可以可以考虑用sendmessage来实现键盘和鼠标事件,取得句柄也可以用findwindow(通过窗体标题寻找窗口)或者getwindowfrompoint通过鼠标位置寻找窗口
var
Btn1,Btn2,Btn3,Ook,Yyes,Ook0:HWND;
begin
H1:=findwindow(nil,'证书');
keybd_event(16,0,0,0);
keybd_event(73,0,0,0);
但是到最后的如图
Delphi编程乐园
群号:32307764