用CreatePorcess调用,然后用WaitForInputIdle来等待程序结束。
解决方案 »
- oracle 数据库连接在一段时间后关闭,重新启动应用程序又能够连接上
- webbrowser1设置socks5代理的时候 账户密码如何填写 delphi
- mysql+DELPHI执行SQL脚本
- TDBChart怎样和数据库连接?
- 高分求解:在richedit中如何实现表情字符和表情图标的相互转换?
- Com难题,高手请进!
- ListView插入数据时,标题栏闪烁很如何解决
- 我用FastReport做报表,但表的数据没有出现?我应怎样做?
- 存放查询结果的问题
- ☆☆高分求助☆☆在程序中怎样调用另一个程序?又怎么样知道那个程序运行结束了??
- 关于程序更改分辨率的问题?
- 关于word2000风格的工具栏设计问题
type
TMessageList = class(TList);var
Form1: TForm1;
MessageList: TMessageList = nil;
MessageBuffer: TEventMsg;
HookHandle: hHook = 0;
MessageCount: Word = 0;
Go: Boolean = False;implementation{$R *.DFM}procedure Stop;
begin
if Go then UnHookWindowsHookEx(HookHandle);
MessageList.Free;
Go:=False;
end;function FBack(Code: Integer; wParam, lParam: LongInt): LongInt; stdcall;
begin
case Code of
HC_SKIP:
begin
Inc(MessageCount);
if MessageCount>=MessageList.Count then Stop
else MessageBuffer:=TEventMsg(MessageList.Items[MessageCount]^);
Result:=0;
end;
HC_GETNEXT:
begin
PEventMsg(lParam)^:=MessageBuffer;
Result:=0;
end
else
Result:=CallNextHookEx(HookHandle, Code, wParam, lParam);
end;
end;procedure SetHook;
begin
MessageBuffer:=TEventMsg(MessageList.Items[0]^);
MessageCount:=0;
HookHandle:=SetWindowsHookEx(WH_JOURNALPLAYBACK, FBack, hInstance, 0);
Go:=True;
end;procedure MakeMessage(Key: byte; Mes: Cardinal);
var
MyEvent: PEventMsg;
begin
New(MyEvent);
with MyEvent^ do
begin
message:=Mes;
ParamL:=Key;
ParamH:=MapVirtualKey(Key, 0);
Time:=GetTickCount;
hWnd:=Form1.Handle;
end;
MessageList.Add(MyEvent);
end;procedure Imitation(KeyCode: Word);
begin
MakeMessage(Lo(KeyCode), WM_KEYDOWN);
MakeMessage(Lo(KeyCode), WM_KEYUP);
end;procedure SetMessages(S: string);
var
i: Integer;
KeyCode: Word;
begin
i:=1;
repeat
KeyCode:=vkKeyScan(S[i]);
Imitation(KeyCode); // simulate key presses
Inc(i);
until i>Length(S);
end;function SendStr(S: string): Integer;
begin
try
MessageList:=TMessageList.Create;
SetMessages(S); // set messages
SetHook; // set hook
except
end;
Result:=0;
end;procedure TForm1.Button1Click(Sender: TObject);
var
Handle: hWnd;
ProcessInfo: TProcessInformation;
StartInfo: TStartUpInfo;
APath: string;
begin
APath:=Edit1.Text;
if CreateProcess(
nil,
PChar(APath),
nil,
nil,
False,
0,
nil,
nil,
StartInfo,
ProcessInfo) then
begin
WaitForInputIdle(ProcessInfo.hProcess, INFINITE);
Handle:=FindWindow('winword', nil);
SetForegroundWindow(Handle);
SendStr('hello');
end;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
if OpenDialog1.Execute then
Edit1.Text:=OpenDialog1.FileName;
end;
var
SI: TStartupInfo;
PI: TProcessInformation;
CmdLine: String;
begin
//Fill record with zero byte values
FillChar(SI, SizeOf(SI), 0);
//Set mandatory record field
SI.cb := SizeOf(SI);
//Ensure Windows mouse cursor reflects launch progress
SI.dwFlags := StartF_ForceOnFeedback;
//Set up command line
CmdLine := Cmd;
if Length(Params) > 0 then
CmdLine := CmdLine + #32 + Params;
//Try and launch child process. Raise exception on failure
Win32Check(CreateProcess(
nil, PChar(CmdLine), nil, nil, False, 0, nil, nil, SI, PI));
//Wait until process has started its main message loop
WaitForInputIdle(PI.hProcess, Infinite);
//Close process and thread handles
CloseHandle(PI.hThread);
CloseHandle(PI.hProcess);
end;