線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1784
推到 Plurk!
推到 Facebook!

如何在delphi下跑DOS批次檔,並讀出DOS執行結果畫面

尚未結案
yx_huang77
一般會員


發表:46
回覆:64
積分:22
註冊:2004-03-19

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-04-01 17:52:47 IP:220.142.xxx.xxx 未訂閱
我想利用delphi撰寫一支執行dos批次檔的備份程式 有參考過下列的寫法,但DOS視窗輸出的結果有時不會出現不知識什原因 碰過很多人在問,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('c:\kk.bat ',Memo1); kk.bat內容如下 REM @ECHO OFF D: XCOPY D:\930401\HOP3\*.* D:\test\ /s REM '做完了'
HomeSound
中階會員


發表:44
回覆:178
積分:94
註冊:2002-08-31

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-04-03 23:13:39 IP:61.30.xxx.xxx 未訂閱
問題好像發錯區了,請參考: http://delphi.ktop.com.tw/topic.php?TOPIC_ID=19897 --==多看.多學.多聽==--
------
--==多看.多學.多聽==--
系統時間:2024-05-18 22:02:03
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!