怎么编程读取java程序创建的dos窗口中显示的内容?

76liujing 2005-07-04 02:33:19
需要从一个java程序中取数据,java程序以dos窗口方式运行。
请问编程如何实现呢?任何语言实现都可以。
...全文
185 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
tkdyzc 2005-07-05
  • 打赏
  • 举报
回复
1-取得命令行参数的个数,paramcount函数
2-呼叫paramstr(0),传回执行行档的档名(含路径)
3-呼叫Paramstr(n),传回第n各参数的内容
实例:
procedure tform1.formcreate(sender:tobject);
var
x:integer;
begin
memo1.lines.clear;
if paramcount=0 then
memo1.lines.add('??我??.');
else
begin
memo1.lines.add('档名'+paramstr(0));
for x:=1 to paramcount do
memo1.lines.add(paramstr(x));
end;
end;
aiirii 2005-07-04
  • 打赏
  • 举报
回复
或这里:

http://cache.baidu.com/c?word=delphi%2Cvar%2Ccmd%3B%2E%3Bexe%2Cpipe&url=http%3A//www%2Eit26%2Ecom/programme/msg48/qa22%2Eshtml%2Ehtm&b=0&a=26&user=baidu
aiirii 2005-07-04
  • 打赏
  • 举报
回复
类似如下:
http://delphi.ktop.com.tw/topic.asp?TOPIC_ID=50574

改寫自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;
76liujing 2005-07-04
  • 打赏
  • 举报
回复
zhmnsw(糖醋鼻子) :
愿闻其详?
糖醋鼻子 2005-07-04
  • 打赏
  • 举报
回复
直接获取命令行信息嘛
干吗要截取DOS窗口呢?
76liujing 2005-07-04
  • 打赏
  • 举报
回复
hugoon(hugoon) :像是有话说?
hugoon 2005-07-04
  • 打赏
  • 举报
回复
呵呵,这个问题...

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧