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,
LVL 1
mgazzaAsked:
Who is Participating?
 
DavidBirch2dotComCommented:
I encountered a similar problem, thankfuly an expert was on hand to solve the problem

see here http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21365522.html 
(actual code is here http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20739899.html)

David
0
 
mgazzaAuthor Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.