Konuyu Oyla:
  • Derecelendirme: 3/5 - 1 oy
  • 1
  • 2
  • 3
  • 4
  • 5
Delphi ile cmd komut göndermek ve yanıt almak
#1
Merhaba,

Nette rastladığım faydalı olabileceğini düşündüğüm bir fonksiyon...


function GetCommand(CommandLine: string): 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);
      hStdOutput := StdOutPipeWrite;
      hStdError := StdOutPipeWrite;
    end;
    WorkDir := GetCurrentDir;
    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;

WWW
Cevapla


Bu Konudaki Yorumlar
Delphi ile cmd komut göndermek ve yanıt almak - Yazar: yhackup - 16-09-2016, Saat: 17:27
Delphi ile cmd komut göndermek ve yanıt almak - Yazar: engerex - 16-09-2016, Saat: 21:02
Delphi ile cmd komut göndermek ve yanıt almak - Yazar: yhackup - 16-09-2016, Saat: 22:15
Delphi ile cmd komut göndermek ve yanıt almak - Yazar: QuAdR - 25-03-2017, Saat: 14:39
Delphi ile cmd komut göndermek ve yanıt almak - Yazar: QuAdR - 26-03-2017, Saat: 02:44
Delphi ile cmd komut göndermek ve yanıt almak - Yazar: yhackup - 26-03-2017, Saat: 14:43
Delphi ile cmd komut göndermek ve yanıt almak - Yazar: yhackup - 14-07-2021, Saat: 11:34

Konu ile Alakalı Benzer Konular
Konular Yazar Yorumlar Okunma Son Yorum
  Delphi 7 Unrar mcuyan 12 485 19-03-2024, Saat: 10:30
Son Yorum: frmman
Lightbulb Delphi 7zip Password lü Dosya Sıkıştırma ve Açma + Bonus RAR5 Desteği frmman 6 262 16-03-2024, Saat: 17:55
Son Yorum: delphi.developer
  delphi 12 ile TFileStream çalışmıyor aegean 5 425 05-03-2024, Saat: 22:23
Son Yorum: aegean
  Delphi ile geliştirdiğiniz uygulamalar neler? - İki Ödüllü Mr.Developer 20 12.683 01-01-2024, Saat: 22:46
Son Yorum: aegean
  Delphi 7 ye göre MB Döviz kurları nasıl alırız maydin60 7 964 31-12-2023, Saat: 02:18
Son Yorum: maydin60



Konuyu Okuyanlar: 1 Ziyaretçi