求详细代码,在windows界面下,能直接访问dos界面,并返回结果.达到重定向的效果~~谢谢!!

解决方案 »

  1.   

    去盒子 www.2ccc.com 搜索一下Dos应该有的
      

  2.   

    真的需要直接访问DOS界面?如果是图形界面怎么办?如果需要接收DOS程序的标准输出,可以用管道。
    有个控件,很好用的
    http://www.google.cn/search?complete=1&hl=zh-CN&q=StdIORedirect&meta=
      

  3.   

    用命名管道
    俺写的
    procedure RunDosCommand(Command: string; Output: Tstringlist; TimeOut: integer);
    var
      hReadPipe: THandle;
      hWritePipe: THandle;
      SI: TStartUpInfo;
      PI: TProcessInformation;
      SA: TSecurityAttributes;
    //  SD : TSecurityDescriptor;
      BytesRead: DWORD;
      Dest: array[0..1023] of char;
      CmdLine: array[0..512] of char;
      TmpList: TStringList;
      Avail, ExitCode, wrResult: DWORD;
      osVer: TOSVERSIONINFO;
      tmpstr: AnsiString;
      all, sEnd: string;
      TC: integer;
    begin  begin
    //    InitializeSecurityDescriptor(@SD, SECURITY_DESCRIPTOR_REVISION);
    //    SetSecurityDescriptorDacl(@SD, True, nil, False);
        SA.nLength := SizeOf(SA);
        SA.lpSecurityDescriptor := nil; //@SD;
        SA.bInheritHandle := True;
        CreatePipe(hReadPipe, hWritePipe, @SA, 0);
      end;  try    FillChar(SI, SizeOf(SI), 0);
        SI.cb := SizeOf(TStartUpInfo);
        SI.wShowWindow := SW_HIDE;
        SI.dwFlags := STARTF_USESHOWWINDOW;
        SI.dwFlags := SI.dwFlags or STARTF_USESTDHANDLES;
        SI.hStdOutput := hWritePipe;
        SI.hStdError := hWritePipe;
        StrPCopy(CmdLine, 'CMD.EXE /c ' + Command);    TC := 0;
        if CreateProcess(nil, CmdLine, nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil, SI, PI) then
          begin
            ExitCode := 0;        while ExitCode = 0 do
              begin            wrResult := WaitForSingleObject(PI.hProcess, 500);
                inc(Tc, 500);            BytesRead := 1024;            while BytesRead > 0 do
                  if PeekNamedPipe(hReadPipe, @Dest[0], 1024, @Avail, nil, nil) then
                    begin
                      if Avail > 0 then
                        begin                      try
                            FillChar(Dest, SizeOf(Dest), 0);
                            ReadFile(hReadPipe, Dest[0], Avail, BytesRead, nil);
                            TmpStr := Copy(Dest, 0, BytesRead);
                            all := all + TmpStr;                      finally                      end;                    end
                      else
                        Break;
                    end
                  else
                    Break;
                if wrResult <> WAIT_TIMEOUT then
                  begin
                    ExitCode := 1;
                //  Output.Add('&Ocirc;&Euml;&ETH;&ETH;&Iacute;ê&sup3;&Eacute;&pound;&iexcl;')
                  end            else
                  begin
                    if TC > TimeOut then
                      if TerminateProcess(PI.hProcess, 0) then
                        begin
                          sEnd := '&Ocirc;&Euml;&ETH;&ETH;&sup3;&not;&Ecirc;±&pound;&not;&Ccedil;&iquest;&Ouml;&AElig;&frac12;á&Ecirc;&oslash;&pound;&iexcl;'
                        end
                      else
                        begin
                          sEnd := '&Ocirc;&Euml;&ETH;&ETH;&sup3;&not;&Ecirc;±&pound;&not;&Ccedil;&iquest;&Ouml;&AElig;&frac12;á&Ecirc;&oslash;&Ecirc;§°&Uuml;&pound;&iexcl;'
                        end;              end;
              end;
            GetExitCodeProcess(PI.hProcess, ExitCode);
            CloseHandle(PI.hProcess);
            CloseHandle(PI.hThread);
          end
        else
          Output.Add('&Ocirc;&Euml;&ETH;&ETH;&sup2;&raquo;&Auml;&Uuml;&frac12;&oslash;&ETH;&ETH;&pound;&iexcl;');    Output.Text := all;
        if sEnd <> '' then
          Output.Add(sEnd);
        all := '';
        tmpstr := '';
        sEnd := '';
      finally
        CloseHandle(hReadPipe);
        CloseHandle(hWritePipe);
      end;
    end;
      

  4.   

    procedure RunDosCommand(Command: string; Output: Tstringlist; TimeOut: integer);
    var
      hReadPipe: THandle;
      hWritePipe: THandle;
      SI: TStartUpInfo;
      PI: TProcessInformation;
      SA: TSecurityAttributes;
    //  SD : TSecurityDescriptor;
      BytesRead: DWORD;
      Dest: array[0..1023] of char;
      CmdLine: array[0..512] of char;
      TmpList: TStringList;
      Avail, ExitCode, wrResult: DWORD;
      osVer: TOSVERSIONINFO;
      tmpstr: AnsiString;
      all, sEnd: string;
      TC: integer;
    begin  begin
    //    InitializeSecurityDescriptor(@SD, SECURITY_DESCRIPTOR_REVISION);
    //    SetSecurityDescriptorDacl(@SD, True, nil, False);
        SA.nLength := SizeOf(SA);
        SA.lpSecurityDescriptor := nil; //@SD;
        SA.bInheritHandle := True;
        CreatePipe(hReadPipe, hWritePipe, @SA, 0);
      end;  try    FillChar(SI, SizeOf(SI), 0);
        SI.cb := SizeOf(TStartUpInfo);
        SI.wShowWindow := SW_HIDE;
        SI.dwFlags := STARTF_USESHOWWINDOW;
        SI.dwFlags := SI.dwFlags or STARTF_USESTDHANDLES;
        SI.hStdOutput := hWritePipe;
        SI.hStdError := hWritePipe;
        StrPCopy(CmdLine, 'CMD.EXE /c ' + Command);    TC := 0;
        if CreateProcess(nil, CmdLine, nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil, SI, PI) then
          begin
            ExitCode := 0;        while ExitCode = 0 do
              begin            wrResult := WaitForSingleObject(PI.hProcess, 500);
                inc(Tc, 500);            BytesRead := 1024;            while BytesRead > 0 do
                  if PeekNamedPipe(hReadPipe, @Dest[0], 1024, @Avail, nil, nil) then
                    begin
                      if Avail > 0 then
                        begin                      try
                            FillChar(Dest, SizeOf(Dest), 0);
                            ReadFile(hReadPipe, Dest[0], Avail, BytesRead, nil);
                            TmpStr := Copy(Dest, 0, BytesRead);
                            all := all + TmpStr;                      finally                      end;                    end
                      else
                        Break;
                    end
                  else
                    Break;
                if wrResult <> WAIT_TIMEOUT then
                  begin
                    ExitCode := 1;
                //  Output.Add('运行完成!')
                  end            else
                  begin
                    if TC > TimeOut then
                      if TerminateProcess(PI.hProcess, 0) then
                        begin
                          sEnd := '运行超时,强制结束!'
                        end
                      else
                        begin
                          sEnd := '运行超时,强制结束失败!'
                        end;              end;
              end;
            GetExitCodeProcess(PI.hProcess, ExitCode);
            CloseHandle(PI.hProcess);
            CloseHandle(PI.hThread);
          end
        else
          Output.Add('运行不能进行!');    Output.Text := all;
        if sEnd <> '' then
          Output.Add(sEnd);
        all := '';
        tmpstr := '';
        sEnd := '';
      finally
        CloseHandle(hReadPipe);
        CloseHandle(hWritePipe);
      end;
    end;