listing z wiersza poleceń do aplikacji

0

Witajcie drodzy użytkownicy. (tym, którzy to czytają teraz, czyli wieczorem, życzę smacznej kolacji).

Chciałbym się dowiedzieć, jak w Delphi, w programie mającym Formę, wyświetlić w np. komponencie TMemo, listing z cmd.
W praktyce wyglądać to ma tak, przykład:

  • klikam button, wykonuje się ShellExecute z cmd i poleceniem dajmy na to: ipconfig /flushdns
    Chcę zobaczyć wynik na komponencie TMemo.

Będę wielce rad i wdzięczny za nakierowanie mnie na coś.

0
function TRunForm.RunIt(const Exe,Param,Dir:String):Boolean;
var SA:TSecurityAttributes;
var SI:TStartupInfo;
var PI:TProcessInformation;
var Res:Cardinal;
var InH,OutH,ErrH,MyInH,MyOutH,MyErrH:THandle;
var L:DWord;
var F:Boolean;
var S:String;
var MSG:PChar;
const PipeSize=16000;
begin
  SA.nLength:=sizeof(SA);
  SA.bInheritHandle:=true;
  SA.lpSecurityDescriptor:=nil;
  CreatePipe(InH,MyInH,@SA,PipeSize);
  CreatePipe(MyOutH,OutH,@SA,PipeSize);
  CreatePipe(MyErrH,ErrH,@SA,PipeSize);
  try
    FillChar(SI,SizeOf(SI),0);
    FillChar(PI,SizeOf(PI),0);
    SI.cb:=SizeOf(SI);
    SI.wShowWindow:=SW_HIDE;
    SI.hStdInput:=InH;
    SI.hStdOutput:=OutH;
    SI.hStdError:=ErrH;
    SI.dwFlags:=STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
    F:=CreateProcess
    (
      nil,
      PChar(Exe+' '+Param),
      nil,
      nil,
      true,
      NORMAL_PRIORITY_CLASS,
      nil,
      PChar(Dir),
      SI,
      PI
    );
    if F then
    begin
      WaitforSingleObject(PI.hProcess,INFINITE);
      SetLength(S,PipeSize);
      L:=0;
      ReadFile(MyOutH,S[1],PipeSize,L,nil);
      SetLength(S,L);
      Memo.Lines.Add(Trim(S)+#13#10);
      SetLength(S,0);
      GetExitCodeProcess(PI.hProcess,Res);
      Result:=(Res=0);
      if not Result then Memo.Lines.Add('Error Code '+IntToStr(Res));
      Exit;
    end;
    Res:=GetLastError;
    FormatMessage
    (
      FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_FROM_SYSTEM,
      nil,
      Res,
      ((SUBLANG_DEFAULT)shl(10))or(LANG_NEUTRAL), // Default language
      @Msg,
      0,
      nil
    );
    Memo.Lines.Add(StrPas(Msg));
    LocalFree(Integer(Msg));
    Result:=false;
  finally
    CloseHandle(InH);
    CloseHandle(OutH);
    CloseHandle(ErrH);
    CloseHandle(MyInH);
    CloseHandle(MyOutH);
    CloseHandle(MyErrH);
  end;
end;
0

Ja od siebie mogę polecić jeszcze jako alternatywną metodę komponent TDosCommand. Po więcej informacji polecam poszukać w google'ach.

1 użytkowników online, w tym zalogowanych: 0, gości: 1