管道通信和命令参数
Pipe communication and command parameters
我的意图是创建匿名管道和输入和输出句柄,作为命令参数传递给 child 进程,具有可继承的选项。这是 Pascal (Lazarus) 代码的摘录(没有启动管道写入的按钮...)。
procedure TForm2.Button1Click(Sender: TObject);
var pi: tprocessinformation;
si: tstartupinfo;
h1, h2: thandle;
begin
createpipe(h1, h2, nil, 300); // --getlasterror returns 0
caption:= inttostr(h1)+ ' '+ inttostr(h2); // just to check
si.cb:= sizeof(si);
zeromemory(@si, sizeof(si));
createprocess(nil, pchar('ChildProject.exe '+ caption), nil, nil, true, 0, nil, nil, si, pi);
end;
和child处理代码(我故意没有使用单独的线程,只是为了开始)。
procedure TForm3.Button2Click(Sender: TObject);
var d: dword;
hin, hout: thandle;
begin
if paramcount= 2 then
begin
hout:= strtoint(paramstr(1));
hin:= strtoint(paramstr(2));
caption:= inttostr(hout)+ ' '+ inttostr(hin);
end;
readfile(hin, a, 8, d, nil);
label1.caption:= inttostr(d)+ ' '+ inttostr(getlasterror);
end;
Child 进程以显示正确句柄的标题开始,但是当我点击按钮时(我没有启动从 parent 发送),readfile 退出并显示错误代码 - 无效句柄 (6) .
我以为child继承了parent的管道句柄,所以我可以随意使用它,但我显然错了。
任何帮助
只有可继承的句柄才会被继承。
您可以通过将 SECURITY_ATTRIBUTES
结构传递给 CreatePipe() 或调用 SetHandleInformation() 来设置 HANDLE_FLAG_INHERIT
标志来使您的管道句柄可继承。
我的意图是创建匿名管道和输入和输出句柄,作为命令参数传递给 child 进程,具有可继承的选项。这是 Pascal (Lazarus) 代码的摘录(没有启动管道写入的按钮...)。
procedure TForm2.Button1Click(Sender: TObject);
var pi: tprocessinformation;
si: tstartupinfo;
h1, h2: thandle;
begin
createpipe(h1, h2, nil, 300); // --getlasterror returns 0
caption:= inttostr(h1)+ ' '+ inttostr(h2); // just to check
si.cb:= sizeof(si);
zeromemory(@si, sizeof(si));
createprocess(nil, pchar('ChildProject.exe '+ caption), nil, nil, true, 0, nil, nil, si, pi);
end;
和child处理代码(我故意没有使用单独的线程,只是为了开始)。
procedure TForm3.Button2Click(Sender: TObject);
var d: dword;
hin, hout: thandle;
begin
if paramcount= 2 then
begin
hout:= strtoint(paramstr(1));
hin:= strtoint(paramstr(2));
caption:= inttostr(hout)+ ' '+ inttostr(hin);
end;
readfile(hin, a, 8, d, nil);
label1.caption:= inttostr(d)+ ' '+ inttostr(getlasterror);
end;
Child 进程以显示正确句柄的标题开始,但是当我点击按钮时(我没有启动从 parent 发送),readfile 退出并显示错误代码 - 无效句柄 (6) . 我以为child继承了parent的管道句柄,所以我可以随意使用它,但我显然错了。
任何帮助
只有可继承的句柄才会被继承。
您可以通过将 SECURITY_ATTRIBUTES
结构传递给 CreatePipe() 或调用 SetHandleInformation() 来设置 HANDLE_FLAG_INHERIT
标志来使您的管道句柄可继承。