这个 process Execute 封装了createProcess()win32API 函数。用于调用另一个<br>应用程序。commanline参数指定了要调用的应用程序,另一个参数包含了SW_XXX<br>常量,用于控制窗口的显示方式,有SW_HIDE,SW_SHOW,SW_SHOWNRMAL等。<br>具体请见帮助。<br>function ProcessExecute(CommandLine: TCommandLine; cShow: Word): Integer;<br>{ This method encapsulates the call to CreateProcess() which creates<br>a new process and its primary thread. This is the method used in<br>Win32 to execute another application, This method requires the use<br>of the TStartInfo and TProcessInformation structures. These structures<br>are not documented as part of the Delphi 4 online help but rather<br>the Win32 help as STARTUPINFO and PROCESS_INFORMATION.<br><br>The CommandLine parameter specifies the pathname of the file to<br>execute.<br><br>The cShow parameter specifies one of the SW_XXXX constants which<br>specifies how to display the window. This value is assigned to the<br>sShowWindow field of the TStartupInfo structure. }<br>var<br>Rslt: LongBool;<br>StartUpInfo: TStartUpInfo; // documented as STARTUPINFO<br>ProcessInfo: TProcessInformation; // documented as PROCESS_INFORMATION<br>begin<br>{ Clear the StartupInfo structure }<br>FillChar(StartupInfo, SizeOf(TStartupInfo), 0);<br>{ Initialize the StartupInfo structure with required data.<br>Here, we assign the SW_XXXX constant to the wShowWindow field<br>of StartupInfo. When specifying a value to this field the<br>STARTF_USESSHOWWINDOW flag must be set in the dwFlags field.<br>Additional information on the TStartupInfo is provided in the Win32<br>online help under STARTUPINFO. }<br>with StartupInfo do<br>begin<br>cb := SizeOf(TStartupInfo); // Specify size of structure<br>dwFlags := STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK;<br>wShowWindow := cShow<br>end;<br><br>{ Create the process by calling CreateProcess(). This function<br>fills the ProcessInfo structure with information about the new<br>process and its primary thread. Detailed information is provided<br>in the Win32 online help for the TProcessInfo structure under<br>PROCESS_INFORMATION. }<br>Rslt := CreateProcess(PChar(CommandLine), nil, nil, nil, False,<br>NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo);<br>{ If Rslt is true, then the CreateProcess call was successful.<br>Otherwise, GetLastError will return an error code representing the<br>error which occurred. }<br>if Rslt then<br>with ProcessInfo do<br>begin<br>{ Wait until the process is in idle. }<br>WaitForInputIdle(hProcess, INFINITE);<br>CloseHandle(hThread); // Free the hThread handle<br>CloseHandle(hProcess);// Free the hProcess handle<br>Result := 0; // Set Result to 0, meaning successful<br>end<br>else Result := GetLastError; // Set result to the error code.<br>end;<br><br>调用:<br>var WERetVal: Word;<br><br>WERetVal := ProcessExecute(FCommandLine, sw_ShowNormal);<br>if WERetVal <> 0 then begin<br>raise Exception.Create('Error executing program. Error Code:; '+<br>IntToStr(WERetVal));<br>end;<br>