已经有人写了,最近看到一个构件,贴出来:<br>unit process;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;<br><br>type<br><br><br> TShowWindow = ( swHide , swMaximize , swMinimize , swRestore , swShow ,<br> swShowDefault , swShowMaximized , swShowMinimized ,<br> swShowMinNoActive , swShowNA , swShowNoActivate , swShowNormal );<br><br> TProcessEvent = procedure ( Sender : TObject; ExitCode : DWord ) of object;<br><br> TProcess = class(TComponent)<br> private<br> FCommand : string;<br> FShowWindow : TShowWindow;<br> FDirectory : string;<br> FWait : Boolean;<br> FOnFinished : TProcessEvent;<br> protected<br> public<br> constructor Create( AOwner : TComponent );<br> procedure Execute;<br> published<br> property Command : string read FCommand write FCommand;<br> property Directory : string read FDirectory write FDirectory;<br> property ShowWindow : TShowWindow read FShowWindow write FShowWindow;<br> property Wait : Boolean read FWait write FWait;<br> property OnFinished : TProcessEvent read FOnFinished write FOnFinished;<br> end;<br><br>procedure Register;<br><br>implementation<br><br>const ShowWindowValues : array [ 0..11 ] of integer =<br> ( sw_Hide , sw_Maximize , sw_Minimize , sw_Restore , sw_Show ,<br> sw_ShowDefault , sw_ShowMaximized , sw_ShowMinimized ,<br> sw_ShowMinNoActive , sw_ShowNA , sw_ShowNoActivate , sw_ShowNormal );<br><br>type<br><br> TProcessThread = class( TThread )<br> private<br> FProcess : TProcess;<br> protected<br> procedure Execute; override;<br> public<br> constructor CreateThread( Process : TProcess );<br> end;<br><br><br>procedure Register;<br>begin<br> RegisterComponents('Samples', [TProcess]);<br>end;<br><br>constructor TProcess.Create( AOwner : TComponent );<br>begin<br> inherited Create( AOwner );<br> FShowWindow := swShowNormal;<br> FWait := False; <br>end;<br><br>procedure TProcess.Execute;<br>begin<br> TProcessThread.CreateThread( Self );<br>end;<br><br>//------------------------------------------------------------------------------<br>// TProcessThread<br><br>constructor TProcessThread.CreateThread( Process : TProcess );<br>begin<br> inherited Create( True );<br> FProcess := Process;<br> FreeOnTerminate := True;<br> Resume;<br>end;<br><br>procedure TProcessThread.Execute;<br>var<br> StartupInfo : TStartupInfo;<br> ProcessInfo : TProcessInformation;<br> ExitCode : DWord;<br> Directory : PChar;<br>begin<br> FillChar( StartupInfo , SizeOf( StartupInfo ) , 0 );<br> with StartupInfo do<br> begin<br> cb := SizeOf( StartupInfo );<br> dwFlags := startf_UseShowWindow;<br> wShowWindow := ShowWindowValues[ Ord( FProcess.ShowWindow ) ];<br> end;<br> ExitCode := 0;<br> FProcess.Directory := Trim( FProcess.Directory );<br> if Length( FProcess.Directory ) = 0 then<br> Directory := nil<br> else<br> Directory := PChar( FProcess.Directory );<br><br> if CreateProcess( nil , PChar( FProcess.Command ) , nil , nil , False ,<br> NORMAL_PRIORITY_CLASS , nil , Directory ,<br> StartupInfo , ProcessInfo ) then<br> begin<br> if FProcess.Wait then<br> begin<br> WaitForSingleObject( ProcessInfo.hProcess , Infinite );<br> GetExitCodeProcess( ProcessInfo.hProcess , ExitCode );<br> if Assigned( FProcess.FOnFinished ) then<br> FProcess.FOnFinished( FProcess , ExitCode );<br> end;<br> CloseHandle( ProcessInfo.hProcess );<br> CloseHandle( ProcessInfo.hThread );<br> end;<br>end;<br><br>end.<br>