[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

cmd execute

Posted on 2005-05-17
2
Medium Priority
?
908 Views
Last Modified: 2013-11-18
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,
0
Comment
Question by:mgazza
2 Comments
 
LVL 7

Accepted Solution

by:
DavidBirch2dotCom earned 150 total points
ID: 14021053
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
 
LVL 1

Author Comment

by:mgazza
ID: 14022190
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question