关于CREATEPROCESS()(50分)

  • 主题发起人 主题发起人 geyufly
  • 开始时间 开始时间
G

geyufly

Unregistered / Unconfirmed
GUEST, unregistred user!
谁能给我提供一些CREATEPROCESS的例子,例如如何调用WIN2000下的NET SEND ????谢谢
 
下面是一个执行启动另一进程并等待该程序结束的 函数<br>function WinExec32(FileName:String; Wind_State : integer;Wait_Flag:Boolean):integer;<br>var<br>&nbsp; AppName:array[0..512] of char;<br>&nbsp; CurDir:array[0..255] of char;<br>&nbsp; WorkDir:String;<br>&nbsp; StartupInfo:TStartupInfo;<br>&nbsp; ProcessInfo:TProcessInformation;<br>begin<br>&nbsp; StrPCopy(AppName,FileName);<br>&nbsp; GetDir(0,WorkDir);<br>&nbsp; StrPCopy(CurDir,WorkDir);<br>&nbsp; FillChar(StartupInfo,Sizeof(StartupInfo),#0);<br>&nbsp; StartupInfo.cb := Sizeof(StartupInfo);<br>&nbsp; StartupInfo.dwFlags := STARTF_USESHOWWINDOW;<br>&nbsp; StartupInfo.wShowWindow := Wind_State;<br>&nbsp; {Case Wind_State of<br>&nbsp; &nbsp;0: 窗口隐藏<br>&nbsp; &nbsp;1: 窗口正常显示<br>&nbsp; &nbsp;2: 窗口最小化显示,焦点在工具栏处的窗口TITLE上<br>&nbsp; &nbsp;3: 窗口最大化显示<br>&nbsp; &nbsp;4: 窗口正常显示,但无焦点<br>&nbsp; &nbsp;5: 窗口正常显示,但有焦点<br>&nbsp; &nbsp;6: 窗口最小化显示,焦点不在工具栏处的窗口TITLE<br>&nbsp; &nbsp;......................<br>&nbsp; &nbsp;....................<br>&nbsp; &nbsp;参照 windows.pas<br>&nbsp; SW_HIDE = 0;<br>&nbsp; SW_SHOWNORMAL = 1;<br>&nbsp; SW_NORMAL = 1;<br>&nbsp; SW_SHOWMINIMIZED = 2;<br>&nbsp; SW_SHOWMAXIMIZED = 3;<br>&nbsp; SW_MAXIMIZE = 3;<br>&nbsp; SW_SHOWNOACTIVATE = 4;<br>&nbsp; SW_SHOW = 5;<br>&nbsp; SW_MINIMIZE = 6;<br>&nbsp; SW_SHOWMINNOACTIVE = 7;<br>&nbsp; SW_SHOWNA = 8;<br>&nbsp; SW_RESTORE = 9;<br>&nbsp; SW_SHOWDEFAULT = 10;<br>&nbsp; SW_MAX = 10;}<br><br>&nbsp; if not CreateProcess(nil,<br>&nbsp; &nbsp; AppName, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ 命令行字符串 }<br>&nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { pointer to process security attributes }<br>&nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { pointer to thread security attributes }<br>&nbsp; &nbsp; false, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { handle inheritance flag }<br>&nbsp; &nbsp; CREATE_NEW_CONSOLE or &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ creation flags }<br>&nbsp; &nbsp; NORMAL_PRIORITY_CLASS,<br>&nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { pointer to new environment block }<br>&nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { pointer to current directory name }<br>&nbsp; &nbsp; StartupInfo, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { pointer to STARTUPINFO }<br>&nbsp; &nbsp; ProcessInfo) then Result := -1 { pointer to PROCESS_INF }<br>&nbsp; else begin<br>&nbsp; &nbsp; if Wait_Flag then<br>&nbsp; &nbsp; &nbsp; &nbsp;WaitforSingleObject(ProcessInfo.hProcess,INFINITE);<br>// &nbsp; &nbsp;if GetExitCodeProcess(ProcessInfo.hProcess,Result) then showmessage('调用成功完成')<br>// &nbsp; &nbsp;else showmessage('调用发生错误');<br>&nbsp; end;<br>end;<br>
 
如果我要调用带有命令行参数的怎么办,请指点。
 
这个 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 &lt;&gt; 0 then begin<br>raise Exception.Create('Error executing program. Error Code:; '+<br>IntToStr(WERetVal));<br>end;<br>
 
“AppName {命令行字符串}”中包括了命令行参数!<br>
 
接受答案了.
 
后退
顶部