如何获取执行DOS命令的返回结果

时间:2022-09-02 20:29:19
WinExec(PChar('cmd.exe   /c   '+'PIng 192.168.0.1  '),SW_HIDE);
如何获取该命令的返回结果?

8 个解决方案

#1


Value Meaning
0 The system is out of memory or resources.
ERROR_BAD_FORMAT The .EXE file is invalid (non-Win32 .EXE or error in .EXE image).
ERROR_FILE_NOT_FOUND The specified file was not found.
ERROR_PATH_NOT_FOUND The specified path was not found.

var 
  s : String ;
  begin
  s := WinExec(PChar('cmd.exe  /c  '+'PIng 192.168.0.1  '),SW_HIDE); 
  ShowMessage(s);
  end;

#2


好像不行啊!!!

#3


WinExec的结果可以另存为文件,再分析文件。
我以前也问过这个问题。

#4


引用 3 楼 atsoftworld 的回复:
WinExec的结果可以另存为文件,再分析文件。 
我以前也问过这个问题。

如何另存为文件啊,请指点一下,谢谢

#5


If the function succeeds, the return value is greater than 31.
If the function fails, the return value is one of the following error values: 

Value Meaning
0 The system is out of memory or resources.
ERROR_BAD_FORMAT The .EXE file is invalid (non-Win32 .EXE or error in .EXE image).
ERROR_FILE_NOT_FOUND The specified file was not found.
ERROR_PATH_NOT_FOUND The specified path was not found.

#6


我以前做过的:

unit recon;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
{$R *.dfm}
function CreateDOSProcessRedirected(const CommandLine, InputFile,OutputFile, ErrMsg :string):boolean;
const
  ROUTINE_ID = '[function: CreateDOSProcessRedirected ]';
var
  OldCursor : TCursor;
  pCommandLine : array[0..MAX_PATH] of char;
  pInputFile,pOutPutFile : array[0..MAX_PATH] of char;
  StartupInfo : TStartupInfo;
  ProcessInfo : TProcessInformation;
  SecAtrrs : TSecurityAttributes;
  hAppProcess,hAppThread,hInputFile,hOutputFile : THandle;
begin
     Result := False;
     if not FileExists(InputFile)    then
       raise Exception.CreateFmt(ROUTINE_ID + #10 + #10
                +'Input file * %s *' + #10 +'does not exist' + #10 + #10
                + ErrMsg, [InputFile]);
     OldCursor := Screen.Cursor;
     Screen.Cursor := crHourglass;
     StrPCopy(pCommandLine, CommandLine);
     StrPCopy(pInputFile, InputFile);
     StrPCopy(pOutPutFile, OutputFile);
     TRY
          FillChar(SecAtrrs, SizeOf(SecAtrrs), #0);
          SecAtrrs.nLength := SizeOf(SecAtrrs);
          SecAtrrs.lpSecurityDescriptor := nil;
          SecAtrrs.bInheritHandle := True;
          hInputFile := CreateFile(
             pInputFile,
             GENERIC_READ or GENERIC_WRITE,
             FILE_SHARE_READ or FILE_SHARE_WRITE,
             @SecAtrrs,
             OPEN_ALWAYS,
             FILE_ATTRIBUTE_NORMAL or FILE_FLAG_WRITE_THROUGH,
             0 );
          if hInputFile = INVALID_HANDLE_VALUE
          then
              raise Exception.CreateFmt(ROUTINE_ID + #10 + #10 +
                   'WinApi function CreateFile returned an' +
                   'invalid handle value' + #10 +
                   'for the input file * %s *' + #10 + #10 +
                   ErrMsg, [InputFile]);
          hOutputFile := CreateFile(
             pOutPutFile,
             GENERIC_READ or GENERIC_WRITE,
             FILE_SHARE_READ or FILE_SHARE_WRITE,
             @SecAtrrs,
             CREATE_ALWAYS,
             FILE_ATTRIBUTE_NORMAL or FILE_FLAG_WRITE_THROUGH,
             0 );
         if hOutputFile = INVALID_HANDLE_VALUE
         then
             raise Exception.CreateFmt(ROUTINE_ID + #10 + #10 +
                    'WinApi function CreateFile returned an' +
                    'invalid handle value' + #10 +
                    'for the output file * %s *' + #10 + #10 +
                    ErrMsg, [OutputFile]);
        FillChar(StartupInfo, SizeOf(StartupInfo), #0);
        StartupInfo.cb := SizeOf(StartupInfo);
        StartupInfo.dwFlags := STARTF_USESHOWWINDOW or
        STARTF_USESTDHANDLES;
        StartupInfo.wShowWindow := SW_HIDE;
        StartupInfo.hStdOutput := hOutputFile;
        StartupInfo.hStdInput := hInputFile;
        Result := CreateProcess(nil,
                      pCommandLine,
                      nil,
                      nil,
                      True,
                      HIGH_PRIORITY_CLASS,
                      nil,
                      nil,
                      StartupInfo,
                      ProcessInfo);
        if Result  then
        begin
            WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
            hAppProcess := ProcessInfo.hProcess;
            hAppThread := ProcessInfo.hThread;
        end else
            raise Exception.Create(ROUTINE_ID + #10 + #10 +
            'Function failure' + #10 + #10 +
            ErrMsg);
        FINALLY
            if hOutputFile <> 0 then CloseHandle(hOutputFile);
            if hInputFile <> 0 then CloseHandle(hInputFile);
            if hAppThread <> 0 then CloseHandle(hAppThread);
            if hAppProcess <> 0 then CloseHandle(hAppProcess);
            Screen.Cursor:= OldCursor;
        END;
end; { CreateDOSProcessRedirected }
procedure TForm1.Button1Click(Sender: TObject);
var
  t:tstringlist;
begin
t:=tstringlist.create;
t.SaveToFile('e:\temp\ttt1.txt');
CreateDOSProcessRedirected('net share','e:\temp\ttt1.txt','e:\temp\ttt2.txt','');
t.LoadFromFile('e:\temp\ttt2.txt');
deletefile( 'e:\temp\ttt1.txt');
deletefile( 'e:\temp\ttt2.txt');
memo1.Lines:=t;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
   WinExec(PChar('cmd.exe  /c  '+'PIng 192.168.0.1  '),SW_HIDE); 
   while not FileExists('tmp.txt') do  ;
     memo1.Lines.LoadFromFile('tmp.txt');
end;

end.

#7


有二个小问题
1.procedure TForm1.Button1Click(Sender: TObject);
是我的测试
2.WinExec(PChar('cmd.exe  /c  '+'PIng 192.168.0.1  '),SW_HIDE); 
要改为
WinExec(PChar('cmd.exe  /c  '+'PIng 192.168.0.1 >tmp.txt '),SW_HIDE); 

#8


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
 hReadPipe,hWritePipe:THandle;
 si:STARTUPINFO;
 lsa:SECURITY_ATTRIBUTES;
 pi:PROCESS_INFORMATION;
 cchReadBuffer:DWORD;
 ph:PChar;
 fname:PChar;
  ExitCode:DWORD;
begin
 fname:=allocmem(255);
 ph:=AllocMem(5000);
 lsa.nLength :=sizeof(SECURITY_ATTRIBUTES);
 lsa.lpSecurityDescriptor :=nil;
 lsa.bInheritHandle :=True;

 if CreatePipe(hReadPipe,hWritePipe,@lsa,0)=false then
 begin
   ShowMessage('Can not create pipe!');
   exit;
 end;
 fillchar(si,sizeof(STARTUPINFO),0);
 si.cb :=sizeof(STARTUPINFO);
 si.dwFlags :=(STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW);
 si.wShowWindow :=SW_HIDE;
 si.hStdInput := GetStdHandle(STD_INPUT_HANDLE);
 si.hStdOutput :=hWritePipe;
 SI.hStdError := hWritePipe;
 StrPCopy(fname, Edit1.Text);

 if CreateProcess( nil, fname, nil, nil, true, 0, nil, nil, si, pi) = False  then
 begin
   ShowMessage('can not create process');
   FreeMem(ph);
   FreeMem(fname);
   Exit;
 end;

 while(true) do
 begin
   if not PeekNamedPipe(hReadPipe,ph,1,@cchReadBuffer,nil,nil) then break;
   if cchReadBuffer<>0 then
   begin
     if ReadFile(hReadPipe,ph^,4096,cchReadBuffer,nil)=false then break;
     ph[cchReadbuffer]:=chr(0);
     Memo1.Lines.Add(ph);
   end
   else if(WaitForSingleObject(pi.hProcess ,0)=WAIT_OBJECT_0) then break;
   Sleep(100);
 end;

 ph[cchReadBuffer]:=chr(0);
 Memo1.Lines.Add(ph);
 GetExitCodeProcess(pi.hProcess,ExitCode);
 Memo1.Lines.Add('ExitCode ' + inttostr(ExitCode));
 CloseHandle(hReadPipe);
 CloseHandle(pi.hThread);
 CloseHandle(pi.hProcess);
 CloseHandle(hWritePipe);
 FreeMem(ph);
 FreeMem(fname);
end;

end.

#1


Value Meaning
0 The system is out of memory or resources.
ERROR_BAD_FORMAT The .EXE file is invalid (non-Win32 .EXE or error in .EXE image).
ERROR_FILE_NOT_FOUND The specified file was not found.
ERROR_PATH_NOT_FOUND The specified path was not found.

var 
  s : String ;
  begin
  s := WinExec(PChar('cmd.exe  /c  '+'PIng 192.168.0.1  '),SW_HIDE); 
  ShowMessage(s);
  end;

#2


好像不行啊!!!

#3


WinExec的结果可以另存为文件,再分析文件。
我以前也问过这个问题。

#4


引用 3 楼 atsoftworld 的回复:
WinExec的结果可以另存为文件,再分析文件。 
我以前也问过这个问题。

如何另存为文件啊,请指点一下,谢谢

#5


If the function succeeds, the return value is greater than 31.
If the function fails, the return value is one of the following error values: 

Value Meaning
0 The system is out of memory or resources.
ERROR_BAD_FORMAT The .EXE file is invalid (non-Win32 .EXE or error in .EXE image).
ERROR_FILE_NOT_FOUND The specified file was not found.
ERROR_PATH_NOT_FOUND The specified path was not found.

#6


我以前做过的:

unit recon;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
{$R *.dfm}
function CreateDOSProcessRedirected(const CommandLine, InputFile,OutputFile, ErrMsg :string):boolean;
const
  ROUTINE_ID = '[function: CreateDOSProcessRedirected ]';
var
  OldCursor : TCursor;
  pCommandLine : array[0..MAX_PATH] of char;
  pInputFile,pOutPutFile : array[0..MAX_PATH] of char;
  StartupInfo : TStartupInfo;
  ProcessInfo : TProcessInformation;
  SecAtrrs : TSecurityAttributes;
  hAppProcess,hAppThread,hInputFile,hOutputFile : THandle;
begin
     Result := False;
     if not FileExists(InputFile)    then
       raise Exception.CreateFmt(ROUTINE_ID + #10 + #10
                +'Input file * %s *' + #10 +'does not exist' + #10 + #10
                + ErrMsg, [InputFile]);
     OldCursor := Screen.Cursor;
     Screen.Cursor := crHourglass;
     StrPCopy(pCommandLine, CommandLine);
     StrPCopy(pInputFile, InputFile);
     StrPCopy(pOutPutFile, OutputFile);
     TRY
          FillChar(SecAtrrs, SizeOf(SecAtrrs), #0);
          SecAtrrs.nLength := SizeOf(SecAtrrs);
          SecAtrrs.lpSecurityDescriptor := nil;
          SecAtrrs.bInheritHandle := True;
          hInputFile := CreateFile(
             pInputFile,
             GENERIC_READ or GENERIC_WRITE,
             FILE_SHARE_READ or FILE_SHARE_WRITE,
             @SecAtrrs,
             OPEN_ALWAYS,
             FILE_ATTRIBUTE_NORMAL or FILE_FLAG_WRITE_THROUGH,
             0 );
          if hInputFile = INVALID_HANDLE_VALUE
          then
              raise Exception.CreateFmt(ROUTINE_ID + #10 + #10 +
                   'WinApi function CreateFile returned an' +
                   'invalid handle value' + #10 +
                   'for the input file * %s *' + #10 + #10 +
                   ErrMsg, [InputFile]);
          hOutputFile := CreateFile(
             pOutPutFile,
             GENERIC_READ or GENERIC_WRITE,
             FILE_SHARE_READ or FILE_SHARE_WRITE,
             @SecAtrrs,
             CREATE_ALWAYS,
             FILE_ATTRIBUTE_NORMAL or FILE_FLAG_WRITE_THROUGH,
             0 );
         if hOutputFile = INVALID_HANDLE_VALUE
         then
             raise Exception.CreateFmt(ROUTINE_ID + #10 + #10 +
                    'WinApi function CreateFile returned an' +
                    'invalid handle value' + #10 +
                    'for the output file * %s *' + #10 + #10 +
                    ErrMsg, [OutputFile]);
        FillChar(StartupInfo, SizeOf(StartupInfo), #0);
        StartupInfo.cb := SizeOf(StartupInfo);
        StartupInfo.dwFlags := STARTF_USESHOWWINDOW or
        STARTF_USESTDHANDLES;
        StartupInfo.wShowWindow := SW_HIDE;
        StartupInfo.hStdOutput := hOutputFile;
        StartupInfo.hStdInput := hInputFile;
        Result := CreateProcess(nil,
                      pCommandLine,
                      nil,
                      nil,
                      True,
                      HIGH_PRIORITY_CLASS,
                      nil,
                      nil,
                      StartupInfo,
                      ProcessInfo);
        if Result  then
        begin
            WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
            hAppProcess := ProcessInfo.hProcess;
            hAppThread := ProcessInfo.hThread;
        end else
            raise Exception.Create(ROUTINE_ID + #10 + #10 +
            'Function failure' + #10 + #10 +
            ErrMsg);
        FINALLY
            if hOutputFile <> 0 then CloseHandle(hOutputFile);
            if hInputFile <> 0 then CloseHandle(hInputFile);
            if hAppThread <> 0 then CloseHandle(hAppThread);
            if hAppProcess <> 0 then CloseHandle(hAppProcess);
            Screen.Cursor:= OldCursor;
        END;
end; { CreateDOSProcessRedirected }
procedure TForm1.Button1Click(Sender: TObject);
var
  t:tstringlist;
begin
t:=tstringlist.create;
t.SaveToFile('e:\temp\ttt1.txt');
CreateDOSProcessRedirected('net share','e:\temp\ttt1.txt','e:\temp\ttt2.txt','');
t.LoadFromFile('e:\temp\ttt2.txt');
deletefile( 'e:\temp\ttt1.txt');
deletefile( 'e:\temp\ttt2.txt');
memo1.Lines:=t;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
   WinExec(PChar('cmd.exe  /c  '+'PIng 192.168.0.1  '),SW_HIDE); 
   while not FileExists('tmp.txt') do  ;
     memo1.Lines.LoadFromFile('tmp.txt');
end;

end.

#7


有二个小问题
1.procedure TForm1.Button1Click(Sender: TObject);
是我的测试
2.WinExec(PChar('cmd.exe  /c  '+'PIng 192.168.0.1  '),SW_HIDE); 
要改为
WinExec(PChar('cmd.exe  /c  '+'PIng 192.168.0.1 >tmp.txt '),SW_HIDE); 

#8


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
 hReadPipe,hWritePipe:THandle;
 si:STARTUPINFO;
 lsa:SECURITY_ATTRIBUTES;
 pi:PROCESS_INFORMATION;
 cchReadBuffer:DWORD;
 ph:PChar;
 fname:PChar;
  ExitCode:DWORD;
begin
 fname:=allocmem(255);
 ph:=AllocMem(5000);
 lsa.nLength :=sizeof(SECURITY_ATTRIBUTES);
 lsa.lpSecurityDescriptor :=nil;
 lsa.bInheritHandle :=True;

 if CreatePipe(hReadPipe,hWritePipe,@lsa,0)=false then
 begin
   ShowMessage('Can not create pipe!');
   exit;
 end;
 fillchar(si,sizeof(STARTUPINFO),0);
 si.cb :=sizeof(STARTUPINFO);
 si.dwFlags :=(STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW);
 si.wShowWindow :=SW_HIDE;
 si.hStdInput := GetStdHandle(STD_INPUT_HANDLE);
 si.hStdOutput :=hWritePipe;
 SI.hStdError := hWritePipe;
 StrPCopy(fname, Edit1.Text);

 if CreateProcess( nil, fname, nil, nil, true, 0, nil, nil, si, pi) = False  then
 begin
   ShowMessage('can not create process');
   FreeMem(ph);
   FreeMem(fname);
   Exit;
 end;

 while(true) do
 begin
   if not PeekNamedPipe(hReadPipe,ph,1,@cchReadBuffer,nil,nil) then break;
   if cchReadBuffer<>0 then
   begin
     if ReadFile(hReadPipe,ph^,4096,cchReadBuffer,nil)=false then break;
     ph[cchReadbuffer]:=chr(0);
     Memo1.Lines.Add(ph);
   end
   else if(WaitForSingleObject(pi.hProcess ,0)=WAIT_OBJECT_0) then break;
   Sleep(100);
 end;

 ph[cchReadBuffer]:=chr(0);
 Memo1.Lines.Add(ph);
 GetExitCodeProcess(pi.hProcess,ExitCode);
 Memo1.Lines.Add('ExitCode ' + inttostr(ExitCode));
 CloseHandle(hReadPipe);
 CloseHandle(pi.hThread);
 CloseHandle(pi.hProcess);
 CloseHandle(hWritePipe);
 FreeMem(ph);
 FreeMem(fname);
end;

end.