在WINDOWS下跑DOS程式,並讀出DOS執行結果畫面 |
|
japhenchen
高階會員 發表:51 回覆:444 積分:184 註冊:2003-07-23 發送簡訊給我 |
碰過很多人在問,WINDOWS跑DOS程式時,怎麼把DOS視窗輸出的結果讀出來,我看了半天似乎沒人提到比較好的解決方案(還是我眼拙沒發現?),以下就提供一個好方法,利用PIPE~!!
procedure RunDosInMemo(DosApp:String;AMemo:TMemo); const ReadBuffer = 2400; var Security : TSecurityAttributes; ReadPipe,WritePipe : THandle; start : TStartUpInfo; ProcessInfo : TProcessInformation; Buffer : Pchar; BytesRead : DWord; Apprunning : DWord; begin With Security do begin nlength := SizeOf(TSecurityAttributes); binherithandle := true; lpsecuritydescriptor := nil; end; if Createpipe (ReadPipe, WritePipe, @Security, 0) then begin Buffer := AllocMem(ReadBuffer 1); FillChar(Start,Sizeof(Start),#0); start.cb := SizeOf(start); start.hStdOutput := WritePipe; start.hStdInput := ReadPipe; start.dwFlags := STARTF_USESTDHANDLES STARTF_USESHOWWINDOW; start.wShowWindow := SW_HIDE; if CreateProcess(nil,PChar(DosApp),@Security,@Security,true,NORMAL_PRIORITY_CLASS, nil,nil,start,ProcessInfo) then begin repeat Apprunning := WaitForSingleObject(ProcessInfo.hProcess,100); Application.ProcessMessages; until (Apprunning <> WAIT_TIMEOUT); Repeat BytesRead := 0; ReadFile(ReadPipe,Buffer[0],ReadBuffer,BytesRead,nil); Buffer[BytesRead]:= #0; OemToAnsi(Buffer,Buffer); AMemo.Text := AMemo.text String(Buffer); until (BytesRead < ReadBuffer); end; FreeMem(Buffer); CloseHandle(ProcessInfo.hProcess); CloseHandle(ProcessInfo.hThread); CloseHandle(ReadPipe); CloseHandle(WritePipe); end; end;RunDosInMemo('chkdsk.exe c:\',Memo1); 藏私の禁止 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |