怎么知道XCOPY运行完?(100分)

  • 主题发起人 主题发起人 天堂之令
  • 开始时间 开始时间

天堂之令

Unregistered / Unconfirmed
GUEST, unregistred user!
我使用winexec('xcopy c:/a c:/b', SW_Normal);<br>我怎么知道XCOPY运行完?我怎么用程序退出XCOPY?(此时WINDOS95任务栏内<br>有XCOPY。EXE)。<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 致<br>礼!<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
 
change it to createprocess, it should work fine:)
 
使用CreateProcess API函数,可以设置参数使XCopy不出现的任务栏上<br>再用GetProcessStats(是不是这个我忘了)取得Xcopy任务的状态,应<br>用程序就可以控制它了,还可以KillProcess<br>不好意思,函数记不太清楚了
 
呵呵, 对, 用createprocess, 并建立一个特殊的windowname, 用findwindow来<br>找它, 如果没有, 就是运行完了. 用terminateprocess来关闭xcopy.
 
给你一个使用Createprocess的函数,它可以运行外部程序或者命令,并 <br>等待运行结束: <br>&nbsp;<br>function WinExecAndWait32(FileName:String; Visibility : integer):integer; <br>var <br>&nbsp; zAppName:array[0..512] of char; <br>&nbsp; zCurDir:array[0..255] of char; <br>&nbsp; WorkDir:String; <br>&nbsp; StartupInfo:TStartupInfo; <br>&nbsp; ProcessInfo:TProcessInformation; <br>begin <br>&nbsp; StrPCopy(zAppName,FileName); <br>&nbsp; GetDir(0,WorkDir); <br>&nbsp; StrPCopy(zCurDir,WorkDir); <br>&nbsp; FillChar(StartupInfo,Sizeof(StartupInfo),#0); <br>&nbsp; StartupInfo.cb := Sizeof(StartupInfo); <br>&nbsp; StartupInfo.dwFlags := STARTF_USESHOWWINDOW; <br>&nbsp; StartupInfo.wShowWindow := Visibility; <br>&nbsp; if not CreateProcess(nil, <br>&nbsp; &nbsp; zAppName, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ pointer to command line string } <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; WaitforSingleObject(ProcessInfo.hProcess,INFINITE); <br>&nbsp; &nbsp; GetExitCodeProcess(ProcessInfo.hProcess,Result); <br>&nbsp; end; <br>end; <br>
 
amo的做法最标准,关键就是WaitforSingleObject
 
这些东西可能有用<br><br>1. 如何执行外部程序[并一直等到它结束]<br><br>㈠ 编程语言Delphi 1.0,操作系统 Window3.1<br><br>以下是一个执行外部程序ARJ.EXE的例子的部分代码。<br><br>var<br>&nbsp; sCommandLine: string;<br>&nbsp; bCreateProcess: boolean;<br>&nbsp; lpStartupInfo: TStartupInfo;<br>&nbsp; lpProcessInformation: TProcessInformation;<br>begin<br>&nbsp; sCommandLine := 'ARJ.EXE /?';<br>&nbsp; bCreateProcess := CreateProcessA(nil, PChar(sCommandLine),<br>&nbsp; &nbsp; nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil,<br>&nbsp; &nbsp; lpStartupInfo, lpProcessInformation);<br>&nbsp; if bCreateProcess then<br>&nbsp; &nbsp; WaitForSingleObject(lpProcessInformation.hProcess, INFINITE);<br>end;<br><br>㈡ 编程语言Delphi3.0,操作系统 Window95 <br>同样是上面的例子的部分代码 <br>var<br>&nbsp; &nbsp;pWindowsList: pointer;<br>&nbsp; &nbsp;hActiveWindow: HWnd;<br>&nbsp; &nbsp;hExeHandle: THandle;<br>begin<br>&nbsp; &nbsp;pWindowsList := DisableTaskWindows(0);<br>&nbsp; &nbsp;hActiveWindow := GetActiveWindow;<br>&nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; hExeHandle := WinExec('arj.exe /?',SW_SHOWNORMAL);<br>&nbsp; &nbsp; &nbsp; while GetModuleUsage(hExeHandle) &lt;&gt; 0 do<br>&nbsp; &nbsp; &nbsp; Application.ProcessMessages;<br>&nbsp; &nbsp;finally<br>&nbsp; &nbsp; &nbsp; EnableTaskWindows(pWindowsList);<br>&nbsp; &nbsp; &nbsp; SetActiveWindow(hActiveWindow);<br>&nbsp; &nbsp;end;<br>end;<br><br><br><br>2. 如果执行的是 MSDOS 外部程序,如何能让它的窗口不显示出来呢? <br>[ 接上例 ]:<br>TStartupInfo 这个结构中有一个 sShowWindow 栏位, 将之设为 SW_HIDE即可,<br>同时, dwFlags 标志中至少需含有 STARTF_USESHOWWINDOW, 否则CreateProcess<br>时, sShowWindow 栏位的设定会无效, 以下是修改过的程式:<br><br>var<br>&nbsp; &nbsp;sCommandLine: string;<br>&nbsp; &nbsp;bCreateProcess: boolean;<br>&nbsp; &nbsp;lpStartupInfo: TStartupInfo;<br>&nbsp; &nbsp;lpProcessInformation: TProcessInformation;<br>begin<br>&nbsp; &nbsp;// sCommandLine 的内容请视您的情况修改<br>&nbsp; &nbsp;sCommandLine :='Xcopy d:/temp/temp1/*.* d:/temp/temp2 /v/y';<br>&nbsp; &nbsp;lpStartupInfo.dwFlags := STARTF_USESHOWWINDOW;<br>&nbsp; &nbsp;lpStartupInfo.wShowWindow := SW_HIDE;<br>&nbsp; &nbsp;bCreateProcess := CreateProcess(nil, PChar(sCommandLine),nil,nil,True,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HIGH_PRIORITY_CLASS, nil, nil,lpStartupInfo, lpProcessInformation);<br>&nbsp; &nbsp;if bCreateProcess then<br>&nbsp; &nbsp; &nbsp; &nbsp; WaitForSingleObject(lpProcessInformation.hProcess, INFINITE);<br>end;<br>
 
ToZeng:第二个例子编译不过去,第三个例子没有结果 why???
 
用不着那么复杂,看看是否取回了返回值,就可以了<br>函数没有执行完,是没有返回值得
 
接受答案了.
 
后退
顶部