Using CreateProcess() to launch another application
   


A useful function to launch another executable, batch file, PIF file, etc. in Delphi on a 32-bit platform. Simply pass the name of the executable and the Visibility type (SW_SHOWNORMAL, SW_HIDE, etc. You can find a list of them in Win32.hlp under 'ShowWindow()'). If you want your program to wait until the launched application is finished, see WinExecAndWait32().

Added
CloseHandle() calls to close the handles since they are no longer needed. Thanks to Kevin S. Gallagher for this addition.

function WinExec32(FileName: string; Visibility: integer): boolean;
var
  zAppName: array[0..512] of char;
  zCurDir : array[0..255] of char;
  WorkDir : string;
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
begin
  FillChar(zAppName, Sizeof(zAppName), #0);
  FillChar(zCurDir, Sizeof(zCurDir), #0);
  StrPCopy(zAppName, FileName); 
  GetDir(0, WorkDir);
  StrPCopy(zCurDir, WorkDir);
  FillChar(StartupInfo, Sizeof(StartupInfo), #0);
  StartupInfo.cb := Sizeof(StartupInfo);
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  StartupInfo.wShowWindow := Visibility;
  Result := CreateProcess(nil,
                          zAppName, 
{ pointer to command line string }
                          nil{ pointer to process security attributes } 
                          nil{ pointer to thread security attributes }
 
                          False, { handle inheritance flag }

                          CREATE_NEW_CONSOLE or { creation flags }

                          NORMAL_PRIORITY_CLASS,
                          nil{ pointer to new environment block }

                          nil{ pointer to current directory name }

                          StartupInfo, { pointer to STARTUPINFO }

                          ProcessInfo); { pointer to PROCESS_INF }

  CloseHandle(ProcessInfo.hProcess);
  CloseHandle(ProcessInfo.hThread);
end;



Blue Orb Software

[email protected]