Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If an exe is launched in my Delphi

Status
Not open for further replies.

TBaz

Technical User
Jun 6, 2000
65
ES
If an exe is launched in my Delphi app in the Form Create
section using:

ShellExecute(0, 'Open', PChar('Other.exe'), PChar(''), PChar(''), SW_Hide);

Is it possible to close it down when my app is closed?

I just noticed in my task manager that the exe was running about thirty times! :)

Thanks...

TDK_Man
 
Doh!

Sorry, I'm used to forums where you can't post until you've entered a topic title - and I forgot again! :(

Oh for an edit button...

TDK_Man
 
Option 1. Send the other applicaton WM_CLOSE message. Finding the handle to applications main window may be a little tricky. FindWindow is one way.

Option 2. Start the application using CreateProcess, from which you can get the process handle (I think this is nto the handle to send a WM_CLOSE messsage to, but you could try). When you want to close the application call TerminateProcess on the process handle. This is a ugly termination that does not close DLLs that the process is using. See windows help on TerminateProcess.

Below is a function I wrote which suspends processing of the invoking application until the new process has finished. Note the freeing of resources in it.

function SHCompleteProcess(const ACommandLine:string; var AExitCode:cardinal;
AWaitInterval:dword=INFINITE):dword;
var
LProcessHandle:THandle;
StartupInfo:TStartupInfo;
ProcessInformation:TProcessInformation;
begin
{Initiate process}
FillChar(StartupInfo,Sizeof(StartupInfo),0);
FillChar(ProcessInformation,Sizeof(ProcessInformation),0);

if not CreateProcess(nil,pchar(ACommandLine),
nil,nil,false,CREATE_DEFAULT_ERROR_MODE+CREATE_NEW_CONSOLE+CREATE_NEW_PROCESS_GROUP,nil,
nil,StartupInfo,ProcessInformation)then begin
Result:=PROCESS_NOT_START;
Exit;
end;

try
{Wait for it to complete then get exit code}
LProcessHandle:=ProcessInformation.hProcess;
Result:=WaitForSingleObject(LProcessHandle,AWaitInterval);
GetExitCodeProcess(LProcessHandle,AExitCode);
finally
CloseHandle(ProcessInformation.hProcess);
CloseHandle(ProcessInformation.hThread);
end;
end;

Good luck
Simon
 
Option 1 worked great - I forgot about that. Many thanks!

TDK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top