执行DOS下EXE文件问题(100分)

  • 主题发起人 主题发起人 sazi
  • 开始时间 开始时间
S

sazi

Unregistered / Unconfirmed
GUEST, unregistred user!
我想在VB中调用DOS下的EXE文件,并且等结束后进行下面的工作,<br>可是用WINEXEC不知道它什么时候结束,也不知道如何结束它。<br>用CREATEPROCESS,参数太多,总是在第三个参数的地方报错,无论我用NULL还是0,<br>不知道什么问题,<br>能否告诉我怎么做最好?<br>谢谢!
 
function FileExecuteWait(const FileName, Params, StartDir: string;<br>&nbsp; InitialState: TExecState): Integer;<br>{$IFDEF WIN32}<br>var<br>&nbsp; Info: TShellExecuteInfo;<br>&nbsp; ExitCode: DWORD;<br>begin<br>&nbsp; FillChar(Info, SizeOf(Info), 0);<br>&nbsp; Info.cbSize := SizeOf(TShellExecuteInfo);<br>&nbsp; with Info do begin<br>&nbsp; &nbsp; fMask := SEE_MASK_NOCLOSEPROCESS;<br>&nbsp; &nbsp; Wnd := Application.Handle;<br>&nbsp; &nbsp; lpFile := PChar(FileName);<br>&nbsp; &nbsp; lpParameters := PChar(Params);<br>&nbsp; &nbsp; lpDirectory := PChar(StartDir);<br>&nbsp; &nbsp; nShow := ShowCommands[InitialState];<br>&nbsp; end;<br>&nbsp; if ShellExecuteEx(@Info) then begin<br>&nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp; Application.ProcessMessages;<br>&nbsp; &nbsp; &nbsp; GetExitCodeProcess(Info.hProcess, ExitCode);<br>&nbsp; &nbsp; until (ExitCode &lt;&gt; STILL_ACTIVE) or Application.Terminated;<br>&nbsp; &nbsp; Result := ExitCode;<br>&nbsp; end<br>&nbsp; else Result := -1;<br>end;<br>
 
我服你,这是DFW上现成的东西。其实我不是不知道有这个东西,只是我声明后参数设的不对而已
 
<br>呵呵!给你一个功能更好的!<br>不仅能完成你所说的功能,而且能得到DOS程序的输出信息!<br>其中的CREATEPROCESS可以参考!给分吧!<br>其在用的是PIPE来得到输出信息!<br><br>function RunDOS(const Prog, CommandLine, Dir: string; out ExitCode: DWORD):<br>&nbsp; string;<br>var<br>&nbsp; HRead, HWrite: THandle;<br>&nbsp; StartInfo: TStartupInfo;<br>&nbsp; ProceInfo: TProcessInformation;<br>&nbsp; b: Boolean;<br>&nbsp; sa: TSecurityAttributes;<br>&nbsp; inS: THandleStream;<br>&nbsp; sRet: TStrings;<br>begin<br>&nbsp; Result := '';<br>&nbsp; FillChar(sa, SizeOf(sa), 0);<br>&nbsp; //设置允许继承,否则在NT和2000下无法取得输出结果<br>&nbsp; sa.nLength := SizeOf(sa);<br>&nbsp; sa.bInheritHandle := True;<br>&nbsp; sa.lpSecurityDescriptor := nil;<br>&nbsp; b := CreatePipe(HRead, HWrite, @sa, 0);<br>&nbsp; if not b then<br>&nbsp; &nbsp; raise Exception.Create(SysErrorMessage(GetLastError));<br>&nbsp; FillChar(StartInfo, SizeOf(StartInfo), 0);<br>&nbsp; StartInfo.cb := SizeOf(StartInfo);<br>&nbsp; StartInfo.wShowWindow := SW_HIDE;<br>&nbsp; //使用指定的句柄作为标准输入输出的文件句柄,使用指定的显示方式<br>&nbsp; StartInfo.dwFlags := STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW;<br>&nbsp; StartInfo.hStdError := HWrite;<br>&nbsp; StartInfo.hStdInput := GetStdHandle(STD_INPUT_HANDLE); //HRe;<br>&nbsp; StartInfo.hStdOutput := HWrite;<br><br>&nbsp; b := CreateProcess(PChar(Prog), //lpApplicationName: PChar<br>&nbsp; &nbsp; PChar(CommandLine), //lpCommandLine: PChar<br>&nbsp; &nbsp; nil, //lpProcessAttributes: PSecurityAttributes<br>&nbsp; &nbsp; nil, //lpThreadAttributes: PSecurityAttributes<br>&nbsp; &nbsp; True, //bInheritHandles: BOOL<br>&nbsp; &nbsp; CREATE_NEW_CONSOLE,<br>&nbsp; &nbsp; nil,<br>&nbsp; &nbsp; PChar(Dir),<br>&nbsp; &nbsp; StartInfo,<br>&nbsp; &nbsp; ProceInfo);<br><br>&nbsp; CheckResult(b);<br>&nbsp; repeat<br>&nbsp; &nbsp; Application.ProcessMessages;<br>&nbsp; until WaitForSingleObject(ProceInfo.hProcess, 1) = 0;<br><br>&nbsp; GetExitCodeProcess(ProceInfo.hProcess, ExitCode);<br><br>&nbsp; inS := THandleStream.Create(HRead);<br>&nbsp; if inS.Size &gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; sRet := TStringList.Create;<br>&nbsp; &nbsp; sRet.LoadFromStream(inS);<br>&nbsp; &nbsp; Result := sRet.Text;<br>&nbsp; &nbsp; sRet.Free;<br>&nbsp; end;<br>&nbsp; inS.Free;<br>&nbsp; CloseHandle(HRead);<br>&nbsp; CloseHandle(HWrite);<br>end;<br>
 
to BeginDelphi<br>怎么有问题!<br>win2000+delphi6
 
[red]up[/red]
 
后退
顶部