执行外部程序并等待程序结束(winrar)(100分)

  • 主题发起人 主题发起人 zbsfg
  • 开始时间 开始时间
Z

zbsfg

Unregistered / Unconfirmed
GUEST, unregistred user!
执行外部程序并等待程序结束<br><br>winrar要单独输入参数,我用可以的但程序什么时候结束不能知道,如下面的代码:<br>RarText:=' x &nbsp;'+ZIPFROM+' '+UNZIPTO;<br>if ShellExecute(0, 'open','winrar',pchar(RarText),nil, SW_SHOW)&lt;=32 then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; messagebox(0,'调用WINRAR进行解压缩时失败!'+chr(13)+'原因:可能是本机未安装WINRAR','出错',mb_ok+mb_iconstop);<br>&nbsp; &nbsp; &nbsp; abort;<br>&nbsp; &nbsp;end; <br><br>我改用CreateProcess,但不能参数输在什么地方,下面的代码只能解出一个目录,文件没有解出<br>&nbsp; &nbsp;RarText:=' x &nbsp;'+ZIPFROM+' '+UNZIPTO;<br><br>&nbsp; &nbsp;if CreateProcess(PChar('winrar'),PChar(RarText),nil,nil,True,NORMAL_PRIORITY_CLASS,nil,nil,StartUpInfo,ProcessInfo) then<br>&nbsp; &nbsp; &nbsp; &nbsp; WaitForSingleObject(ProcessInfo.hProcess,INFINITE);<br><br>CreateProcess的命令参数输在什么地方?命令和参数必须分别输入,winrar是这样的,我试过.<br>
 
把 exe 文件和参数组合成一个串,放到第二个参数里:<br>TempStr:=' x &nbsp;'+ZIPFROM+' '+UNZIPTO;<br>RarText:='WinRAR.exe'+ TempStr;<br>CreateProcess(nil,PChar(RarText),.... 试试。
 
用WaitForSingal()
 
用下面这个函数执行你的命令行代码:<br>如 WinExecAndWait('winrar -x a.rar', 1);<br><br>function WinExecAndWait(Path: string; Visibility: Word): DWORD;<br><br>&nbsp; function CreateProcessAndWait(const AppPath, AppParams: string;<br>&nbsp; &nbsp; Visibility: Word): DWORD;<br>&nbsp; var<br>&nbsp; &nbsp; SI: TStartupInfo;<br>&nbsp; &nbsp; PI: TProcessInformation;<br>&nbsp; &nbsp; Proc: THandle;<br>&nbsp; &nbsp; WaitResult: DWORD;<br>&nbsp; begin<br>&nbsp; &nbsp; FillChar(SI, SizeOf(SI), 0);<br>&nbsp; &nbsp; SI.cb := SizeOf(SI);<br>&nbsp; &nbsp; SI.wShowWindow := Visibility;<br>&nbsp; &nbsp; SI.dwFlags := STARTF_USESHOWWINDOW;<br>&nbsp; &nbsp; if not CreateProcess(PChar(AppPath), PChar(AppParams), nil, nil, False,<br>&nbsp; &nbsp; &nbsp; NORMAL_PRIORITY_CLASS, nil, nil, SI, PI) then<br>&nbsp; &nbsp; &nbsp; raise Exception.CreateFmt('不能执行,请输入或选择可执行文件的名称和路径.错误信息为“%s”',<br>&nbsp; &nbsp; &nbsp; &nbsp; [SysErrorMessage(GetLastError)]);<br>&nbsp; &nbsp; Proc := PI.hProcess;<br>&nbsp; &nbsp; CloseHandle(PI.hThread);<br>&nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp; Application.ProcessMessages;<br>&nbsp; &nbsp; &nbsp; WaitResult := WaitForSingleObject(Proc, 200);<br>&nbsp; &nbsp; until (WaitResult &lt;&gt; WAIT_TIMEOUT) or (WaitResult = WAIT_FAILED);<br>&nbsp; &nbsp; GetExitCodeProcess(Proc, Result);<br>&nbsp; &nbsp; CloseHandle(Proc);<br>&nbsp; end;<br><br>var<br>&nbsp; P: Integer;<br>&nbsp; Params: string;<br>begin<br>&nbsp; Params := Path;<br>&nbsp; P := Pos(' ', Path); // assume params start at first space<br>&nbsp; Delete(Path, P, Length(Path));<br>&nbsp; Result := CreateProcessAndWait(Path, Params, Visibility);<br>end;
 
//WinExecAndWait('winrar -x a.rar', 1);<br>winrar不支持在命令后直接加入参数,必须在单独提供参数,不能在一条命令行中提供<br>
 
放弃使用WINRAR,改用压缩控件
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部