Link to home
Start Free TrialLog in
Avatar of mgazza
mgazzaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

cmd execute

hi i want to write data the stdin of a cmd program

i currently use
createpipe( hread, hwrite...
createprocess etc

when should i write the data to hwrite ?

regards mark,
ASKER CERTIFIED SOLUTION
Avatar of DavidBirch2dotCom
DavidBirch2dotCom

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mgazza

ASKER

thats what i allready have :/

i need to write to stdin

just cant write to the programs input buffer duno if its possable
its just that i cant send post data to php.exe


the propper way to ex cmd

function CMDExecute(Fcommand:string;var stdout:TMemoryStream): integer;
Const
BufSize= 4194304;
var
  SecAttr: TSecurityAttributes;
  StartupInfo:TStartupInfo;
  ProcessInfo:TProcessInformation;
  WriteHandle, ReadHandle: THandle;
  ReadBuf: Array[0..1023]of char;
  BytesRead, BytesTotal: DWORD;
  GetExitCodeResult: DWORD;
begin
  SecAttr.nLength := SizeOf( SecAttr );
  SecAttr.lpSecurityDescriptor := nil;
  SecAttr.bInheritHandle := True;

  if not CreatePipe(ReadHandle, WriteHandle, @SecAttr, BufSize) then
  begin
    Result :=GetLastError;
    Exit;
  end;

  FillChar(StartupInfo,SizeOf(StartupInfo), 0);
  with StartupInfo do
  begin
    cb:= SizeOf(StartupInfo);
    dwFlags:= STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
    hStdOutput:= WriteHandle;
    hStdError:= WriteHandle;
    wShowWindow:= SW_Hide;
  end;

  FillChar(ReadBuf, SizeOf(ReadBuf), 0);

  if not CreateProcess(nil, PChar(FCommand), nil, nil,
     True, DETACHED_PROCESS or NORMAL_PRIORITY_CLASS {or
CREATE_SEPARATE_WOW_VDM},
     nil, nil, StartupInfo, ProcessInfo) then
      Result:= GetLastError
  else
    begin
      WaitForSingleObject(ProcessInfo.hProcess, 60000);
      GetExitCodeProcess(ProcessInfo.hProcess, GetExitCodeResult);
      Result := GetExitCodeResult;
      if Result = STILL_ACTIVE then TerminateProcess(ProcessInfo.hProcess,0);
      CloseHandle(ProcessInfo.hProcess);
      CloseHandle(ProcessInfo.hThread);
      PeekNamedPipe(ReadHandle, @ReadBuf, sizeof(ReadBuf), @BytesRead, @BytesTotal,nil);
      while BytesRead <> 0 do begin
        BytesRead := 0;
        ReadFile(ReadHandle, ReadBuf, sizeof(ReadBuf), BytesRead, nil);
        stdout.Write(ReadBuf[0], BytesRead);
        PeekNamedPipe(ReadHandle, @ReadBuf, sizeof(ReadBuf), @BytesRead, @BytesTotal,nil);
      end;
    end;
  CloseHandle(WriteHandle);
end;

regards mark