将dos内部命令结果输出到Console Window(100分)

  • 主题发起人 MaxWoods
  • 开始时间
M

MaxWoods

Unregistered / Unconfirmed
GUEST, unregistred user!
[blue]<br>unit Console;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls,ShellAPI;<br><br>type<br>&nbsp; TConsoleForm = class(TForm)<br>&nbsp; &nbsp; btOpen: TButton;<br>&nbsp; &nbsp; btClose: TButton;<br>&nbsp; &nbsp; btRun: TButton;<br>&nbsp; &nbsp; btOutput: TButton;<br>&nbsp; &nbsp; procedure btOpenClick(Sender: TObject);<br>&nbsp; &nbsp; procedure btCloseClick(Sender: TObject);<br>&nbsp; &nbsp; procedure btOutputClick(Sender: TObject);<br>&nbsp; &nbsp; procedure btRunClick(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; ConsoleForm: TConsoleForm;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TConsoleForm.btOpenClick(Sender: TObject);<br>begin<br>&nbsp; if not AllocConsole then Application.MessageBox('Can''t allocate console!','<br>Console',MB_OK OR MB_ICONINFORMATION)<br>&nbsp; else SetConsoleTitle('Console Demo');<br>end;<br><br>procedure TConsoleForm.btCloseClick(Sender: TObject);<br>begin<br>&nbsp; if not FreeConsole then Application.MessageBox('Can''t free console!','Conso<br>le',MB_OK OR MB_ICONINFORMATION);<br>end;<br><br>procedure TConsoleForm.btOutputClick(Sender: TObject);<br>var OutText:pChar;nWrite:Cardinal;sHandle:Cardinal;<br>begin<br>&nbsp;OutText:='Hello,console!'#$A#$D;<br>&nbsp;sHandle:=GetStdHandle(STD_OUTPUT_HANDLE);<br>&nbsp;if (sHandle&lt;&gt;0) then<br>&nbsp;begin<br>&nbsp; &nbsp;WriteConsole(sHandle,OutText,StrLen(OutText),nWrite,nil);<br>&nbsp;end;<br>end;<br><br>procedure TConsoleForm.btRunClick(Sender: TObject);<br>begin<br>&nbsp; //这里要运行dos的dir命令,并将结果输出到"Open"按钮打开的Console <br>&nbsp; //Window,怎么写<br>end;<br><br>end.[/blue]
 
在网络探索者1.1(qsilence@sina.com)中挖来的一个函数:<br>RunDosCommand('ping.exe ' + 参数, RichEdit1.Lines);<br>=============================================================<br>procedure RunDosCommand(Command : string; Output : TStrings);<br>var<br>&nbsp; hReadPipe : THandle;<br>&nbsp; hWritePipe : THandle;<br>&nbsp; SI : TStartUpInfo;<br>&nbsp; PI : TProcessInformation;<br>&nbsp; SA : TSecurityAttributes;<br>// &nbsp;SD : TSecurityDescriptor;<br>&nbsp; BytesRead : DWORD;<br>&nbsp; Dest : array[0..1023] of char;<br>&nbsp; CmdLine : array[0..512] of char;<br>&nbsp; TmpList : TStringList;<br>&nbsp; Avail, ExitCode, wrResult : DWORD;<br>&nbsp; osVer : TOSVERSIONINFO;<br>&nbsp; tmpstr :AnsiString;<br>begin<br>&nbsp; osVer.dwOSVersionInfoSize := Sizeof(TOSVERSIONINFO);<br>&nbsp; GetVersionEX(osVer);<br><br>&nbsp; if osVer.dwPlatformId = VER_PLATFORM_WIN32_NT then<br>&nbsp; begin<br>// &nbsp; &nbsp;InitializeSecurityDescriptor(@SD, SECURITY_DESCRIPTOR_REVISION);<br>// &nbsp; &nbsp;SetSecurityDescriptorDacl(@SD, True, nil, False);<br>&nbsp; &nbsp; SA.nLength := SizeOf(SA);<br>&nbsp; &nbsp; SA.lpSecurityDescriptor := nil;//@SD;<br>&nbsp; &nbsp; SA.bInheritHandle := True;<br>&nbsp; &nbsp; CreatePipe(hReadPipe, hWritePipe, @SA, 0);<br>&nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; CreatePipe(hReadPipe, hWritePipe, nil, 1024);<br>&nbsp; try<br>&nbsp; &nbsp; Screen.Cursor := crHourglass;<br>&nbsp; &nbsp; FillChar(SI, SizeOf(SI), 0);<br>&nbsp; &nbsp; SI.cb := SizeOf(TStartUpInfo);<br>&nbsp; &nbsp; SI.wShowWindow := SW_HIDE;<br>&nbsp; &nbsp; SI.dwFlags := STARTF_USESHOWWINDOW;<br>&nbsp; &nbsp; SI.dwFlags := SI.dwFlags or STARTF_USESTDHANDLES;<br>&nbsp; &nbsp; SI.hStdOutput := hWritePipe;<br>&nbsp; &nbsp; SI.hStdError := hWritePipe;<br>&nbsp; &nbsp; StrPCopy(CmdLine, Command);<br>&nbsp; &nbsp; if CreateProcess(nil, CmdLine, nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil, SI, PI) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp;ExitCode := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp;while ExitCode = 0 do<br>&nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wrResult := WaitForSingleObject(PI.hProcess, 500);<br>// &nbsp; &nbsp; &nbsp; &nbsp;if PeekNamedPipe(hReadPipe, nil, 0, nil, @Avail, nil) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if PeekNamedPipe(hReadPipe, @Dest[0], 1024, &nbsp;@Avail, nil, nil) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if Avail &gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TmpList := TStringList.Create;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FillChar(Dest, SizeOf(Dest), 0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ReadFile(hReadPipe, Dest[0], Avail, BytesRead, nil);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TmpStr := Copy(Dest,0 , BytesRead-1);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TmpList.Text := TmpStr;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Output.AddStrings(TmpList);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;finally<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TmpList.Free;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if wrResult &lt;&gt; WAIT_TIMEOUT then ExitCode := 1;<br>&nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp;GetExitCodeProcess(PI.hProcess, ExitCode);<br>&nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(PI.hProcess);<br>&nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(PI.hThread);<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp;finally<br>&nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(hReadPipe);<br>&nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(hWritePipe);<br>&nbsp; &nbsp; &nbsp; &nbsp;Screen.Cursor := crDefault;<br>&nbsp; &nbsp;end;<br>end;<br>
 
顶部