当前位置: 游戏平台 > 互联网科技 > 正文

用CreateProcess读取Console程序的显示信息和返回结果

时间:2019-11-12 02:13来源:互联网科技
unit Unit1; unit Unit1; interface interface uses   COmObj,WordXP,Windows, Messages, SysUtils, Variants, Classes,Graphics, Controls, Forms,   Dialogs, Menus, StdCtrls, OleCtnrs, ComCtrls, Buttons, ToolWin; uses   Windows, Messages, SysUti

unit Unit1;

unit Unit1;

interface

interface

uses
  COmObj,WordXP,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, StdCtrls, OleCtnrs, ComCtrls, Buttons, ToolWin;

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

type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    N4: TMenuItem;
    N5: TMenuItem;
    N6: TMenuItem;
    N7: TMenuItem;
    N8: TMenuItem;
    N9: TMenuItem;
    N10: TMenuItem;
    N15: TMenuItem;
    N16: TMenuItem;
    N17: TMenuItem;
    N18: TMenuItem;
    N19: TMenuItem;
    N20: TMenuItem;
    N21: TMenuItem;
    N22: TMenuItem;
    N23: TMenuItem;
    N24: TMenuItem;
澳门皇冠金沙网站,    N25: TMenuItem;
    N26: TMenuItem;
    N27: TMenuItem;
    N28: TMenuItem;
    OpenDialog1: TOpenDialog;
    Memo1: TMemo;
    SaveDialog1: TSaveDialog;
    N11: TMenuItem;
    ColorDialog1: TColorDialog;
    Button1: TButton;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    ToolButton2: TToolButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    SpeedButton7: TSpeedButton;
    ToolBar2: TToolBar;
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    N12: TMenuItem;
    N13: TMenuItem;
    N14: TMenuItem;
    ToolButton3: TToolButton;
    SpeedButton8: TSpeedButton;
    SpeedButton9: TSpeedButton;
    SpeedButton10: TSpeedButton;
    ToolButton4: TToolButton;
    SpeedButton11: TSpeedButton;
    SpeedButton12: TSpeedButton;
    SpeedButton13: TSpeedButton;
    N29: TMenuItem;
    OleContainer1: TOleContainer;
    procedure N2Click(Sender: TObject);
    procedure N11Click(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure N9Click(Sender: TObject);
    procedure N8Click(Sender: TObject);
    procedure N10Click(Sender: TObject);
    procedure N17Click(Sender: TObject);
    procedure N18Click(Sender: TObject);
    procedure N20Click(Sender: TObject);
    procedure N21Click(Sender: TObject);
    procedure N22Click(Sender: TObject);
    procedure N24Click(Sender: TObject);
    procedure N27Click(Sender: TObject);
    procedure N28Click(Sender: TObject);
    procedure N25Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure newClick(Sender: TObject);
   // procedure OpenClick(Sender: TObject);
    procedure OpenClick(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
    procedure N29Click(Sender: TObject);
    procedure N13Click(Sender: TObject);
    procedure N14Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure ComboBox2Change(Sender: TObject);
    procedure alignnment(Sender: TObject);
    procedure SpeedButton8Click(Sender: TObject);
    procedure SpeedButton9Click(Sender: TObject);
    procedure SpeedButton10Click(Sender: TObject);
    procedure SpeedButton11Click(Sender: TObject);
    procedure SpeedButton12Click(Sender: TObject);
    procedure SpeedButton13Click(Sender: TObject);

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    OpenDialog1: TOpenDialog;
    Button2: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }

  private
    { Private declarations }
  public
    { Public declarations }
  end;

    function WinExecAndWait32(FileName:String;Visibility:Integer;var mOutputs:string):Cardinal;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

var
  Form1: TForm1;

implementation

implementation

{$R *.dfm}
 uses Unit2;
 var
   Filename:string;
 //------菜单栏-文件
procedure TForm1.N2Click(Sender: TObject);
var
      wordapp, doc: olevariant;
      strs: TStringList;
begin

{$R *.dfm}
function TForm1.WinExecAndWait32(FileName:String;Visibility:Integer;var mOutputs:string):Cardinal;
var
  sa:TSecurityAttributes;
  hReadPipe,hWritePipe:THandle;
  ret:BOOL;
  strBuff:array[0..255] of char;
  lngBytesread:DWORD;
 
  WorkDir:String;
  StartupInfo:TStartupInfo;
  ProcessInfo:TProcessInformation;
begin
  FillChar(sa,Sizeof(sa),#0);
  sa.nLength := Sizeof(sa);
  sa.bInheritHandle := True;
  sa.lpSecurityDescriptor := nil;
  ret := CreatePipe(hReadPipe, hWritePipe, @sa, 0);

   if opendialog1.Execute then    //打开文件
     begin
       strs:=TStringList.Create;
       wordapp := createOleobject('Word.application');
       try
         doc := wordapp.Documents.Open(FileName := OpenDialog1.FileName);
        memo1.Text := doc.range.Text;
        doc.Close;
       finally
         wordapp.quit;
        strs.Free;
       end;
     end;

  WorkDir:=ExtractFileDir(Application.ExeName);
  FillChar(StartupInfo,Sizeof(StartupInfo),#0);
  StartupInfo.cb:=Sizeof(StartupInfo);
  StartupInfo.dwFlags:=STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
  StartupInfo.wShowWindow:=Visibility;

    // memo1.Lines.LoadFromFile(
     //opendialog1.FileName);

  StartupInfo.hStdOutput:=hWritePipe;
  StartupInfo.hStdError:=hWritePipe;

end;

  if not CreateProcess(nil,
    PChar(FileName),               { pointer to command line string }
    @sa,                           { pointer to process security attributes }
    @sa,                           { pointer to thread security attributes }
    True,                          { handle inheritance flag }
//    CREATE_NEW_CONSOLE or          { creation flags }
    NORMAL_PRIORITY_CLASS,
    nil,                           { pointer to new environment block }
    PChar(WorkDir),                { pointer to current directory name, PChar}
    StartupInfo,                   { pointer to STARTUPINFO }
    ProcessInfo)                   { pointer to PROCESS_INF }
    then Result := INFINITE {-1} else
  begin
//    Form1.Hide;
//    FileOpen(FileName,fmShareExclusive);
//    SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);
    ret:=CloseHandle(hWritePipe);
    mOutputs:=;
    while ret do
    begin
      FillChar(strBuff,Sizeof(strBuff),#0);
      ret := ReadFile(hReadPipe, strBuff, 256, lngBytesread, nil);
      mOutputs := mOutputs + strBuff;
    end;

procedure TForm1.N11Click(Sender: TObject);
begin
    if savedialog1.Execute  then//保存文件
      memo1.Lines.SaveToFile(
      savedialog1.FileName);
end;

    Application.ProcessMessages;
    WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
    GetExitCodeProcess(ProcessInfo.hProcess, Result);
    CloseHandle(ProcessInfo.hProcess);  { to prevent memory leaks }
    CloseHandle(ProcessInfo.hThread);
//    Form1.Close;                        { exit application }
    ret := CloseHandle(hReadPipe);
  end;
end;

procedure TForm1.N6Click(Sender: TObject);
begin
   if Messagedlg('是否退出',
   Mtconfirmation,[mbYes,mbNo],0)=mrYes then //退出
     form1.Close;
end;
//---------编辑子菜单--编辑

procedure TForm1.Button1Click(Sender: TObject);
var
  Val:Cardinal;
  mOutputs:string;
  i:integer;
begin
  Val:=WinExecAndWait32(Edit1.Text,SW_SHOWNORMAL,mOutputs);
  Memo1.Lines.Add(******Result******+#13#10+IntToStr(Val));
  i:=1;
  while i<=length(mOutputs) do
  begin
    if mOutputs[i]=#10 then
    begin
      Insert(#13,mOutputs,i);
      i:=i+2;
    end
    else
      Inc(i);
  end;
  Memo1.Lines.Add(******Echo******+#13#10+mOutputs);
end;

procedure TForm1.N9Click(Sender: TObject);
begin
   Memo1.CutToClipboard;      //剪切
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  OpenDialog1.InitialDir:=ExtractFilePath(ParamStr(0));
  if OpenDialog1.Execute then
    Edit1.Text:=Opendialog1.FileName;
end;

procedure TForm1.N8Click(Sender: TObject);
begin
    Memo1.CopyToClipboard;          //拷贝
end;

end.

procedure TForm1.N10Click(Sender: TObject);
begin
   Memo1.PasteFromClipboard;              //粘贴
end;

源代码下载

procedure TForm1.N17Click(Sender: TObject);
begin
    Memo1.SelectAll;//全选
end;

Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; OpenDialog...

procedure TForm1.N18Click(Sender: TObject);
begin
    Memo1.Lines.Clear;//全清
end;
//--------------格式子菜单

procedure TForm1.N20Click(Sender: TObject);
begin
   N20.Checked:=true;    //左对齐,只要它被选中,则其他两个居中和右对齐就为False
   Memo1.Alignment:=taLeftJustify;
end;

procedure TForm1.N21Click(Sender: TObject);
begin
    N21.Checked:=true;  //居中
    Memo1.Alignment:=taCenter;
end;

procedure TForm1.N22Click(Sender: TObject);
begin
   N22.Checked:=true;  //右对齐
    Memo1.Alignment:=taRightJustify;
end;

procedure TForm1.N24Click(Sender: TObject);
begin
    if colordialog1.Execute then//字体颜色
      Memo1.Font.Color:=colordialog1.Color;
end;

procedure TForm1.N27Click(Sender: TObject);
begin
   N27.Checked:=not N27.Checked;      //粗体
   if N27.Checked then
     Memo1.Font.Style:=Memo1.Font.Style+[fsBold]
   else
     Memo1.Font.Style:=Memo1.Font.Style-[fsBold];
end;

编辑:互联网科技 本文来源:用CreateProcess读取Console程序的显示信息和返回结果

关键词: