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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling an external program 1

Status
Not open for further replies.

jrdoner

Programmer
Jul 17, 2003
4
I seem to recall many years ago using a procedure that allowed a Delphi application to execute an external program, which, when closed, returned control to the original program. But, I can't remember the name of that procedure. Anybody out there know which procedure it is?

Thanks in advance for any help.

John R. Doner
 
This should fulfill your requirement:
Code:
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;
 
Some more example of how to do this in the FAQ.

Steve: Delphi a feersum engin indeed.
 
You can also try this function which I got from Julian V Moss at edgequest:

Code:
function ShellExec(apchOperation, apchFileName, apchParameters, apchDirectory: PChar;
  awrdShowCmd: Word; ablnWait: Boolean): LongInt;
var
  lblnOK: Boolean;
  lseiInfo: TShellExecuteInfo;
begin
  FillChar(lseiInfo, SizeOf(lseiInfo), Chr(0));
  lseiInfo.cbSize := SizeOf(lseiInfo);
  lseiInfo.fMask := SEE_MASK_NOCLOSEPROCESS;
  lseiInfo.lpVerb := apchOperation;
  lseiInfo.lpFile := apchFileName;
  lseiInfo.lpParameters := apchParameters;
  lseiInfo.lpDirectory := apchDirectory;
  lseiInfo.nShow := awrdShowCmd;

  lblnOK := Boolean(ShellExecuteEx(@lseiInfo));
  if lblnOK and ablnWait then
    while WaitForSingleObject(lseiInfo.hProcess, 100) = WAIT_TIMEOUT do
      Application.ProcessMessages;

  Result := lseiInfo.hInstApp;    
end;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top