Mega Code Archive

 
Categories / Delphi / Examples
 

ExecuteTerminat an application

Title: Execute/Terminat an application Question: How can I execute/treminate a external application from within my own application. Answer: Use the API Call 'ExecuteProcess' to execute an applicaiotn, to close the same app use 'TerminateProcess'. This is an example on how to do this. var MyHandle : THandle; MyStartupInfo: TStartupInfo; MyProcessInfo: TProcessInformation; procedure TMyForm.ExecuteApp(MyPath: String); begin FillChar(MyStartupInfo, SizeOf(MyStartupInfo), 0); // Clear the data in MyStartupInfo. MyStartupInfo.cb:=SizeOf(MyStartupInfo); CreateProcess(PChar(MyPath), nil, nil, nil, False, DETACHED_PROCESS, nil, nil, MyStartupInfo, MyProcessInfo); MyHandle:=MyProcessInfo.hProcess; // Assigns the handle on the executed app to MyHandle, this // handle is needed when terminating the app. end; procedure TMyForm.CloseApp(MyHandle: THandle); begin TerminateProcess(MyHandle, 0); end;