Registriert seit: 18. Mai 2003
634 Beiträge
|
Re: Externes von mir gestartes Programm beenden? Hilfe!
8. Jan 2004, 17:51
Ich würde es so machen (aber wahrscheinlich nur weil ich es anders nicht kann ):
Delphi-Quellcode:
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;
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;
function KillProcess(PID: DWord): Bool;
var
hProcess: THandle;
begin
hProcess := OpenProcess(PROCESS_TERMINATE, False, PID);
Result := TerminateProcess(hProcess, 0);
end;
procedure TForm1.Button1Click(Sender: TObject); //starten
begin
RunProcess(Paramstr(0), SW_SHOW, FALSE, @ProcID);
end;
procedure TForm1.Button2Click(Sender: TObject); //beenden
begin
KillProcess(ProcID);
end;
P.S.: Meine Meinung zu C/C++: siehe Unten
|
|
Zitat
|