function RunProcess(FileName: string; ShowCmd: DWORD; wait: Boolean; ProcID: PDWORD): Longword; var StartupInfo: TStartupInfo; ProcessInfo: TProcessInformation; begin FillChar(StartupInfo, SizeOf(StartupInfo), #0); StartupInfo.cb := SizeOf(StartupInfo); StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK; StartupInfo.wShowWindow := ShowCmd; if not CreateProcess(nil, @Filename[1], nil, nil, False, CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo) then Result := WAIT_FAILED else begin if wait = FALSE then begin if ProcID <> nil then ProcID^ := ProcessInfo.dwProcessId; result := WAIT_FAILED; exit; end; WaitForSingleObject(ProcessInfo.hProcess, INFINITE); GetExitCodeProcess(ProcessInfo.hProcess, Result); end; if ProcessInfo.hProcess <> 0 then CloseHandle(ProcessInfo.hProcess); if ProcessInfo.hThread <> 0 then CloseHandle(ProcessInfo.hThread); end;procedure TForm1.Button1Click(Sender: TObject); var ProcID: Cardinal; begin ProcID := 0; if OpenDialog1.Execute then RunProcess(OpenDialog1.FileName, SW_SHOW, CheckBox1.Checked, @ProcID); ShowMessage(IntToStr(ProcID)); end;
function ExecAppWait(AppName, Params: string): Boolean; var // Structure containing and receiving info about application to start ShellExInfo: TShellExecuteInfo; begin FillChar(ShellExInfo, SizeOf(ShellExInfo), 0); with ShellExInfo do begin cbSize := SizeOf(ShellExInfo); fMask := see_Mask_NoCloseProcess; Wnd := Application.Handle; lpFile := PChar(AppName); lpParameters := PChar(Params); nShow := sw_ShowNormal; end; Result := ShellExecuteEx(@ShellExInfo); if Result then while WaitForSingleObject(ShellExInfo.HProcess, 100) = WAIT_TIMEOUT do begin Application.ProcessMessages; if Application.Terminated then Break; end; end;
有具体的例子吗?
我的被调用的exe不是delphi,是Perl
用delphi的exe去调用perl的exe
procedure FindAProcess(const AFilename: string; const PathMatch: Boolean; var ProcessID: DWORD);
var
lppe: TProcessEntry32;
SsHandle: Thandle;
FoundAProc, FoundOK: boolean;
begin
ProcessID :=0;
SsHandle := CreateToolHelp32SnapShot(TH32CS_SnapProcess, 0);
FoundAProc := Process32First(Sshandle, lppe);
while FoundAProc do
begin
if PathMatch then
FoundOK := AnsiStricomp(lppe.szExefile, PChar(AFilename)) = 0
else
FoundOK := AnsiStricomp(PChar(ExtractFilename(lppe.szExefile)), PChar(ExtractFilename(AFilename))) = 0;
if FoundOK then
begin
ProcessID := lppe.th32ProcessID;
break;
end;
FoundAProc := Process32Next(SsHandle, lppe);
end;
CloseHandle(SsHandle);
end;可以用这个函数来判断某一个可执行文件是否正在运行,若在运行,则返回值ProcessID为一个大于零的整数,
就是那个进程的PID,若没有运行,则为0。
还有,在uses里面加入TlHelp32。
调用:if OpenDialog1.Execute then
begin
FindAProcess(OpenDialog1.FileName,False,PID);
end;
if PID > 0 then
showmessage(OpenDialog1.FileName+' is running!');
他会等到对方完成消息触发的操作完成sendmessage则是发送一个消息过去就不管了
然后用 WaitForSingleObject
function RunProcess(FileName: string; ShowCmd: DWORD; wait: Boolean; ProcID:
PDWORD): Longword;
var
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
begin
FillChar(StartupInfo, SizeOf(StartupInfo), #0);
StartupInfo.cb := SizeOf(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK;
StartupInfo.wShowWindow := ShowCmd;
if not CreateProcess(nil,
@Filename[1],
nil,
nil,
False,
CREATE_NEW_CONSOLE or
NORMAL_PRIORITY_CLASS,
nil,
nil,
StartupInfo,
ProcessInfo)
then
Result := WAIT_FAILED
else
begin
if wait = FALSE then
begin
if ProcID <> nil then
ProcID^ := ProcessInfo.dwProcessId;
result := WAIT_FAILED;
exit;
end;
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess, Result);
end;
if ProcessInfo.hProcess <> 0 then
CloseHandle(ProcessInfo.hProcess);
if ProcessInfo.hThread <> 0 then
CloseHandle(ProcessInfo.hThread);
end;procedure TForm1.Button1Click(Sender: TObject);
var
ProcID: Cardinal;
begin
ProcID := 0;
if OpenDialog1.Execute then
RunProcess(OpenDialog1.FileName, SW_SHOW, CheckBox1.Checked, @ProcID);
ShowMessage(IntToStr(ProcID));
end;
var
// Structure containing and receiving info about application to start
ShellExInfo: TShellExecuteInfo;
begin
FillChar(ShellExInfo, SizeOf(ShellExInfo), 0);
with ShellExInfo do begin
cbSize := SizeOf(ShellExInfo);
fMask := see_Mask_NoCloseProcess;
Wnd := Application.Handle;
lpFile := PChar(AppName);
lpParameters := PChar(Params);
nShow := sw_ShowNormal;
end;
Result := ShellExecuteEx(@ShellExInfo);
if Result then
while WaitForSingleObject(ShellExInfo.HProcess, 100) = WAIT_TIMEOUT do
begin
Application.ProcessMessages;
if Application.Terminated then Break;
end;
end;
最好
使用查找进程的方法也行。