急问:管道……(问题解决就奉送家产200分!)(0分)

  • 主题发起人 主题发起人 SuperLunatic
  • 开始时间 开始时间
S

SuperLunatic

Unregistered / Unconfirmed
GUEST, unregistred user!
用Delphi写了一个多线程的 Chile.exe :是画3个不同的图。
又写一个 Parent.exe:利用管道调用 Chile.exe !
我现在在Parent里建立了管道,子进程。请问我怎么从管道发消息,让子进程响应。
var
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
SecurityAttributes: TSecurityAttributes;
ChildInput,MyOutput: THANDLE;
s:string;
Bytes: Cardinal;

Application.ProcessMessages;
SecurityAttributes.nLength := sizeof(SECURITY_ATTRIBUTES);
SecurityAttributes.lpSecurityDescriptor := nil;
SecurityAttributes.bInheritHandle := True;
// 建立管道
if not CreatePipe(ChildInput, MyOutput, @SecurityAttributes, 0) then
RaiseLastWin32Error;

FillChar(StartupInfo,SizeOf(StartupInfo),0);
with StartupInfo do
begin
cb := SizeOf(StartupInfo);
dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
wShowWindow := SW_Normal;
hStdInput := GetStdHandle(STD_INPUT_HANDLE);
hStdOutput := MyOutput;
hStdError := MyOutput;
end;

//创建子进程
If CreateProcess(nil,'child.exe',nil,nil,
True,0,nil,nil,StartupInfo,ProcessInfo) Then
Begin
CloseHandle(MyOutput);
repeat
ReadFile(ChildInput, s, 1, Bytes, nil);
until Bytes = 0;
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
End
Else ShowMessage('创建失败: '+
SysErrorMessage(GetLastError));
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
hReadPipe,hWritePipe:THandle;
si:STARTUPINFO;
lsa:SECURITY_ATTRIBUTES;
pi:PROCESS_INFORMATION;
mDosScreen:String;
cchReadBuffer:DWORD;
ph:PChar;
fname:PChar;
i,j:integer;
begin
fname:=allocmem(255);
ph:=AllocMem(5000);
lsa.nLength :=sizeof(SECURITY_ATTRIBUTES);
lsa.lpSecurityDescriptor :=nil;
lsa.bInheritHandle :=True;

if CreatePipe(hReadPipe,hWritePipe,@lsa,0)=false then
begin
ShowMessage('Can not create pipe!');
exit;
end;

fillchar(si,sizeof(STARTUPINFO),0);
si.cb :=sizeof(STARTUPINFO);
si.dwFlags :=(STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW);
si.wShowWindow :=SW_HIDE;
si.hStdOutput :=hWritePipe;
StrPCopy(fname,'nbtstat -a 192.168.0.15');

if CreateProcess( nil, fname, nil, nil, true, 0, nil, nil, si, pi) = False then
begin
ShowMessage('can not create process');
FreeMem(ph);
FreeMem(fname);
Exit;
end;

while(true) do
begin
if not PeekNamedPipe(hReadPipe,ph,1,@cchReadBuffer,nil,nil) then break;
if cchReadBuffer<>0 then
begin
if ReadFile(hReadPipe,ph^,4096,cchReadBuffer,nil)=false then break;
ph[cchReadbuffer]:=chr(0);
Memo1.Lines.Add(ph);
end
else if(WaitForSingleObject(pi.hProcess ,0)=WAIT_OBJECT_0) then break;
Sleep(100);
end;

ph[cchReadBuffer]:=chr(0);
Memo1.Lines.Add(ph);
CloseHandle(hReadPipe);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
CloseHandle(hWritePipe);
FreeMem(ph);
FreeMem(fname);
end;

end.
 
请问你:
我的不是调用DOS程序,我从管道发消息之后,子进程接收,然后根据不同的信息执行不同的
语句。结果不返回,而是显示在子进程的窗口上。
这样我怎么接收和发送?
接收写在子进程里的话,我怎么找到从Parent发来的消息?
谢谢!!
 
后退
顶部