winexec(100分)

  • 主题发起人 主题发起人 lsys
  • 开始时间 开始时间
L

lsys

Unregistered / Unconfirmed
GUEST, unregistred user!
例如我在DOS提示状态输入:dir <br>我用delphi中需要取得结果:<br>1、用winexec(pchar(dir &gt;c:/aa.txt),sw_hide);<br>2、在读取c:/aa.txt;<br>有没有一步到位的办法
 
有个什么控件可以做到的,你查一下以前的贴子, UltraEdit那样的.
 
标准办法:<br>function TForm1.RunCaptured(const _dirName, _exeName, _cmdLine: string): Boolean; <br>var <br>&nbsp; start: TStartupInfo; <br>&nbsp; procInfo: TProcessInformation; <br>&nbsp; tmpName: string; <br>&nbsp; tmp: Windows.THandle; <br>&nbsp; tmpSec: TSecurityAttributes; <br>&nbsp; res: TStringList; <br>&nbsp; return: Cardinal; <br>begin <br>&nbsp; Result := False; <br>&nbsp; try <br>&nbsp; &nbsp; { Set a temporary file } <br>&nbsp; &nbsp; tmpName := 'Test.tmp'; <br>&nbsp; &nbsp; FillChar(tmpSec, SizeOf(tmpSec), #0); <br>&nbsp; &nbsp; tmpSec.nLength := SizeOf(tmpSec); <br>&nbsp; &nbsp; tmpSec.bInheritHandle := True; <br>&nbsp; &nbsp; tmp := Windows.CreateFile(PChar(tmpName), <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Generic_Write, File_Share_Write, <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@tmpSec, Create_Always, File_Attribute_Normal, 0); <br>&nbsp; &nbsp; try <br>&nbsp; &nbsp; &nbsp; FillChar(start, SizeOf(start), #0); <br>&nbsp; &nbsp; &nbsp; start.cb &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= SizeOf(start); <br>&nbsp; &nbsp; &nbsp; start.hStdOutput &nbsp;:= tmp; <br>&nbsp; &nbsp; &nbsp; start.dwFlags &nbsp; &nbsp; := StartF_UseStdHandles or StartF_UseShowWindow; <br>&nbsp; &nbsp; &nbsp; start.wShowWindow := SW_Minimize; <br>&nbsp; &nbsp; &nbsp; { Start the program } <br>&nbsp; &nbsp; &nbsp; if CreateProcess(nil, PChar(_exeName + ' ' + _cmdLine), nil, nil, True, <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0, nil, PChar(_dirName), start, procInfo) then <br>&nbsp; &nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; &nbsp; SetPriorityClass(procInfo.hProcess, Idle_Priority_Class); <br>&nbsp; &nbsp; &nbsp; &nbsp; WaitForSingleObject(procInfo.hProcess, Infinite); <br>&nbsp; &nbsp; &nbsp; &nbsp; GetExitCodeProcess(procInfo.hProcess, return); <br>&nbsp; &nbsp; &nbsp; &nbsp; Result := (return = 0); <br>&nbsp; &nbsp; &nbsp; &nbsp; CloseHandle(procInfo.hThread); <br>&nbsp; &nbsp; &nbsp; &nbsp; CloseHandle(procInfo.hProcess); <br>&nbsp; &nbsp; &nbsp; &nbsp; Windows.CloseHandle(tmp); <br>&nbsp; &nbsp; &nbsp; &nbsp; { Add the output } <br>&nbsp; &nbsp; &nbsp; &nbsp; res := TStringList.Create; <br>&nbsp; &nbsp; &nbsp; &nbsp; try <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res.LoadFromFile(tmpName); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Memo1.Lines.AddStrings(res); <br>&nbsp; &nbsp; &nbsp; &nbsp; finally <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res.Free; <br>&nbsp; &nbsp; &nbsp; &nbsp; end; <br>&nbsp; &nbsp; &nbsp; &nbsp; Windows.DeleteFile(PChar(tmpName)); <br>&nbsp; &nbsp; &nbsp; end <br>&nbsp; &nbsp; &nbsp; else <br>&nbsp; &nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; &nbsp; Application.MessageBox(PChar(SysErrorMessage(GetLastError())), <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'RunCaptured Error', MB_OK); <br>&nbsp; &nbsp; &nbsp; end; <br>&nbsp; &nbsp; except <br>&nbsp; &nbsp; &nbsp; Windows.CloseHandle(tmp); <br>&nbsp; &nbsp; &nbsp; Windows.DeleteFile(PChar(tmpName)); <br>&nbsp; &nbsp; &nbsp; raise; <br>&nbsp; &nbsp; end; <br>&nbsp; finally <br>&nbsp; end; <br>end; <br><br><br>// 例子: <br><br>procedure TForm1.Button1Click(Sender: TObject); <br>begin <br>&nbsp; RunCaptured('C:/', 'cmd.exe', '/c dir'); <br>end;
 
这个问题本来很简单,但被楼上写成这样长,我还能再写吗?!
 
不长,确实是标准的写法。使用CreateProcess
 
接受答案了.
 
后退
顶部