Recent

Author Topic: I get an Error while communication vie ssh (plink.exe)  (Read 3250 times)

kluesi

  • Newbie
  • Posts: 1
I get an Error while communication vie ssh (plink.exe)
« on: November 22, 2018, 10:41:31 pm »
Hi

I use the following procedure to execute a process:

function GetDosOutput(CommandLine: string; Work: string = 'C:\'): string;
var
  SA: TSecurityAttributes;
  SI: TStartupInfo;
  PI: TProcessInformation;
  StdOutPipeRead, StdOutPipeWrite: THandle;
  WasOK: Boolean;
  Buffer: array[0..255] of AnsiChar;
  BytesRead: Cardinal;
  WorkDir: string;
  Handle: Boolean;
begin
  Result := '';
  with SA do begin
    nLength := SizeOf(SA);
    bInheritHandle := True;
    lpSecurityDescriptor := nil;
  end;
  CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SA, 0);
  try

    with SI do
    begin
      FillChar(SI, SizeOf(SI), 0);
      cb := SizeOf(SI);
      dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
      wShowWindow := SW_HIDE;
      hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdin
      hStdOutput := StdOutPipeWrite;
      hStdError := StdOutPipeWrite;
    end;
    WorkDir := Work;
    Handle := CreateProcess(nil, PChar('cmd.exe /C ' + CommandLine),
                            nil, nil, True, 0, nil,
                            PChar(WorkDir), SI, PI);
    CloseHandle(StdOutPipeWrite);
    if Handle then
      try
        repeat
          WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil);
          if BytesRead > 0 then
          begin
            Buffer[BytesRead] := #0;
            Result := Result + Buffer;
          end;
        until not WasOK or (BytesRead = 0);
        WaitForSingleObject(PI.hProcess, INFINITE);
      finally
        CloseHandle(PI.hThread);
        CloseHandle(PI.hProcess);
      end;
  finally
    CloseHandle(StdOutPipeRead);
  end;
end;     
-----------------------------------------------------------
I tried this CommandLine:

plink.exe  -batch -ssh -2 -l XXXXXXX -pw YYYYYYYY -m c:\Users\XXXXXX\command.sh SERVER

The file command.sh contains only one line:

echo "Hi" 1>&2;

I don't know exactly why I have to redirect the output, but it works. I started the programm with Debugger and everything is ok. If I start the xyz.exe I get the following Error Message:

Unable to read from standard input: Das Handle ist ungültig.

What do I wrong

Thank Kluesi

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: I get an Error while communication vie ssh (plink.exe)
« Reply #1 on: November 27, 2018, 08:48:56 pm »
CreateProcess should return a BOOL (alias of LongBool), not a Boolean. Boolean can only hold boolean with ordinal value of 0 and 1, while BOOL can hold 32-bit unsigned integer (0 = false, anything else is true). When assigned to the former, non-0 value will be converted into 1, I believe. Hence, you get invalid handle error.

 

TinyPortal © 2005-2018