如何向一个DOS程序发送一个字符串?(100分)

D

dengkuo

Unregistered / Unconfirmed
GUEST, unregistred user!
我想编一个程序,控制另一个DOS程序(形式类似于debug.exe,但无源代码),<br>先令其在后台执行,然后向它发送一个字符串(末尾是回车),执行后将其关闭,<br>最后截获它的执行结果。恳请各位老师指教!!
 
试试用管道吧。
 
使用DDE传输
 
试试使用Socket,应该能实现你说的功能。哦,对了,你可以建一个类,在类里面设定<br>一个静态变量来实现两个应用程序间的通信。因为,静态变量都使用同一地址空间
 
如果DOS程序是你写的,用WinSoket.如果不是,用automation 模拟 CTRL+V 往里 PASTE
 
用管道不行。<br>DOS程序不是我写的,“用automation 模拟 CTRL+V 往里 PASTE”如何实现,能否详细<br>说明一下,谢谢!!<br>
 
你找到ctrl+v的键盘码不就行了吗?
 
到处都有SendKeys之类的Unit下载,下载一个来模拟键盘事件。
 
sendkeys我试了4/5个都不行,我是想发字符串到WINDOWS的应用程序<br>比如记事本等等<br>如果知道某个程序窗口的EDIT的句柄,怎么在自己的程序上输入呢,模拟键盘怎么做<br>比如模拟回车键
 
&gt;“用管道不行。DOS程序不是我写的”<br>管道和DOS程序是不是你写的有什么关系?<br><br>下面是运行Dos命令nbtstat -a 192.168.0.15,并得到输出的例子:<br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Memo1: TMemo;<br>&nbsp; &nbsp; procedure Button1Click(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; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; hReadPipe,hWritePipe:THandle;<br>&nbsp; si:STARTUPINFO;<br>&nbsp; lsa:SECURITY_ATTRIBUTES;<br>&nbsp; pi:pROCESS_INFORMATION;<br>&nbsp; mDosScreen:String;<br>&nbsp; cchReadBuffer:DWORD;<br>&nbsp; ph:pChar;<br>&nbsp; fname:pChar;<br>&nbsp; i,j:integer;<br>begin<br>&nbsp; fname:=allocmem(255);<br>&nbsp; ph:=AllocMem(5000);<br>&nbsp; lsa.nLength :=sizeof(SECURITY_ATTRIBUTES);<br>&nbsp; lsa.lpSecurityDescriptor :=nil;<br>&nbsp; lsa.bInheritHandle :=True;<br><br>&nbsp; if CreatePipe(hReadPipe,hWritePipe,@lsa,0)=false then<br>&nbsp; begin<br>&nbsp; &nbsp; ShowMessage('Can not create pipe!');<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br><br>&nbsp; fillchar(si,sizeof(STARTUPINFO),0);<br>&nbsp; si.cb :=sizeof(STARTUPINFO);<br>&nbsp; si.dwFlags :=(STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW);<br>&nbsp; si.wShowWindow :=SW_HIDE;<br>&nbsp; si.hStdOutput :=hWritePipe;<br>&nbsp; StrPCopy(fname,'nbtstat -a 192.168.0.15');<br><br>&nbsp; if CreateProcess( nil, fname, nil, nil, true, 0, nil, nil, si, pi) = False then<br>&nbsp; begin<br>&nbsp; &nbsp; ShowMessage('can not create process');<br>&nbsp; &nbsp; FreeMem(ph);<br>&nbsp; &nbsp; FreeMem(fname);<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br><br>&nbsp; while(true) do<br>&nbsp; begin<br>&nbsp; &nbsp; if not PeekNamedPipe(hReadPipe,ph,1,@cchReadBuffer,nil,nil) then break;<br>&nbsp; &nbsp; if cchReadBuffer&lt;&gt;0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if ReadFile(hReadPipe,ph^,4096,cchReadBuffer,nil)=false then break;<br>&nbsp; &nbsp; &nbsp; ph[cchReadbuffer]:=chr(0);<br>&nbsp; &nbsp; &nbsp; Memo1.Lines.Add(ph);<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else if(WaitForSingleObject(pi.hProcess ,0)=WAIT_OBJECT_0) then break;<br>&nbsp; &nbsp; Sleep(100);<br>&nbsp; end;<br><br>&nbsp; ph[cchReadBuffer]:=chr(0);<br>&nbsp; Memo1.Lines.Add(ph);<br>&nbsp; CloseHandle(hReadPipe);<br>&nbsp; CloseHandle(pi.hThread);<br>&nbsp; CloseHandle(pi.hProcess);<br>&nbsp; CloseHandle(hWritePipe);<br>&nbsp; FreeMem(ph);<br>&nbsp; FreeMem(fname);<br>end;<br><br>end.<br>
 
感谢各位!!
 

Similar threads

S
回复
0
查看
973
SUNSTONE的Delphi笔记
S
S
回复
0
查看
791
SUNSTONE的Delphi笔记
S
顶部