console 程序的返回值 |
尚未結案
|
jesselian
一般會員 發表:5 回覆:8 積分:2 註冊:2004-03-31 發送簡訊給我 |
|
RaynorPao
版主 發表:139 回覆:3622 積分:7025 註冊:2002-08-12 發送簡訊給我 |
引言: 各位大大, 如果我用exec or spawnl or shellexecute or anything simpliar 去运行一个console app, 我怎么动态的得到它的返回值,即 它每返回一个数值我就可以得到一个.jesselian 你好: 先試試看這篇文章中的做法是否可行? class="code"> 備註: (1)你問問題的用詞,造成了誤解!!一般我們說傳回值,通常指的是一個數字, 用來識別執行的結果是否正確,或者錯誤!! (2)如果想要得到執行的結果,只需要這樣子寫就可以了 WinExec("cmd.exe /c ping.exe tw.yahoo.com > C:\\ping.txt", SW_HIDE); Memo1->Lines->Clear(); Memo1->Lines->LoadFromFile("C:\\ping.txt"); -- Enjoy Researching & Developing -- 發表人 - RaynorPao 於 2004/05/24 00:32:56
------
-- 若您已經得到滿意的答覆,請適時結案!! -- -- 欲知前世因,今生受者是;欲知來世果,今生做者是 -- -- 一切有為法,如夢幻泡影,如露亦如電,應作如是觀 -- |
jesselian
一般會員 發表:5 回覆:8 積分:2 註冊:2004-03-31 發送簡訊給我 |
引言:RaynorPao 大大, 我试了你的方法似乎不行. 同时我也试过了axsoft的方法http://delphi.ktop.com.tw/topic.php?topic_id=27232 它总是在CONSOLE APP结束的时候一次性返回所有的返回值. 我是想,如果我用ping -t www.google.com, 在另一个MEMO中,我能不断的得到返回值. 你觉得可能实现嘛?引言: 各位大大, 如果我用exec or spawnl or shellexecute or anything simpliar 去运行一个console app, 我怎么动态的得到它的返回值,即 它每返回一个数值我就可以得到一个.jesselian 你好: 先試試看這篇文章中的做法是否可行? src="http://delphi.ktop.com.tw/loadfile.php?TOPICID=10003806&CC=223734"> -- Enjoy Researching & Developing -- |
pwipwi
版主 發表:68 回覆:629 積分:349 註冊:2004-04-08 發送簡訊給我 |
|
jesselian
一般會員 發表:5 回覆:8 積分:2 註冊:2004-03-31 發送簡訊給我 |
|
richtop
資深會員 發表:122 回覆:646 積分:468 註冊:2003-06-10 發送簡訊給我 |
jesselian 您好: 改寫自DevPHP獲取console mode的執行結果程式部分,讓它能一行一行的顯示。
抱歉此為Delphi版本,供您參考!
// following codes are borrowed from the source codes of DevPHP. function getDOSOutput(Cmd, WorkDir: String): string; var tsi: TStartupInfo; tpi: TProcessInformation; nRead: DWORD; aBuf: Array[0..101] of char; sa: TSecurityAttributes; hOutputReadTmp, hOutputRead, hOutputWrite, hInputWriteTmp, hInputRead, hInputWrite, hErrorWrite: THandle; FOutput : String; /// added by Rich perLine : String; len : integer; /// ===== begin FOutput := ''; perLine := ''; sa.nLength := SizeOf(TSecurityAttributes); sa.lpSecurityDescriptor := nil; sa.bInheritHandle := True; CreatePipe(hOutputReadTmp, hOutputWrite, @sa, 0); DuplicateHandle(GetCurrentProcess(), hOutputWrite, GetCurrentProcess(), @hErrorWrite, 0, true, DUPLICATE_SAME_ACCESS); CreatePipe(hInputRead, hInputWriteTmp, @sa, 0); DuplicateHandle(GetCurrentProcess(), hOutputReadTmp, GetCurrentProcess(), @hOutputRead, 0, false, DUPLICATE_SAME_ACCESS); DuplicateHandle(GetCurrentProcess(), hInputWriteTmp, GetCurrentProcess(), @hInputWrite, 0, false, DUPLICATE_SAME_ACCESS); CloseHandle(hOutputReadTmp); CloseHandle(hInputWriteTmp); FillChar(tsi, SizeOf(TStartupInfo), 0); tsi.cb := SizeOf(TStartupInfo); tsi.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW; tsi.hStdInput := hInputRead; tsi.hStdOutput := hOutputWrite; tsi.hStdError := hErrorWrite; CreateProcess(nil, PChar(Cmd), @sa, @sa, true, 0, nil, PChar(WorkDir), tsi, tpi); CloseHandle(hOutputWrite); CloseHandle(hInputRead ); CloseHandle(hErrorWrite); Application.ProcessMessages; repeat if (not ReadFile(hOutputRead, aBuf, 16, nRead, nil)) or (nRead = 0) then begin if GetLastError = ERROR_BROKEN_PIPE then Break else MessageDlg('Pipe read error, could not execute file', mtError, [mbOK], 0); end; aBuf[nRead] := #0; FOutput := FOutput PChar(@aBuf[0]); ///// 我增加的部分 perLine := perLine PChar(@aBuf[0]); len := Pos(chr(10),perLine); if ( len>0 ) then begin Form1.ListBox1.Items.Add(Copy(perLine,1,len-1)); // added by Rich. perLine := Copy(perLine, len 1, Length(perLine)-len); end; ///// Application.ProcessMessages; until False; Result := FOutput; end; procedure TForm1.Button3Click(Sender: TObject); var WorkDir : string; begin ListBox1.Items.Clear; WorkDir := '.'; Memo1.Text := getDOSOutput('ping www.tnee.tnit.edu.tw', WorkDir); // OK! end;RichTop 敬上 =====***** 把數學當工具,可以解決問題;將數學變能力,能夠發現並解決問題! =====##### |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |