Djole
Djole

Reputation: 142

Pipe communication and command parameters

My intention was to create anonymous pipes and handles of input and output one to pass as command parameters to child process, with inheritable option. Here is excerpt from Pascal (Lazarus) code (without button that initiates pipe write ...).

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;

And child process code (I intentionally didn't use separate thread, just for beginning).

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 process started with caption that displays correct handles, but when I hit button (I din't initiate sent from parent), readfile exits with error code- invalid handle (6). I thought that child inherits parent's pipe handles, so I can use it freely, but I obviously got something wrong.

Any help

Upvotes: 0

Views: 96

Answers (1)

Harry Johnston
Harry Johnston

Reputation: 36328

Only handles that are inheritable are inherited.

You can make your pipe handles inheritable either by passing a SECURITY_ATTRIBUTES structure to CreatePipe(), or by calling SetHandleInformation() to set the HANDLE_FLAG_INHERIT flag.

Upvotes: 1

Related Questions