⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rundosthrd.pas

📁 改写的sql2000管理器
💻 PAS
字号:
unit RunDosThrd;

interface

uses
  Windows, SysUtils, Classes;

const
  READCOUNT = 1;

type
  TRunDosThread = class(TThread)
  private
    { Private declarations }
    Security : TSecurityAttributes;
    ReadPipe,WritePipe : Thandle;
    StartInfo : TStartUpInfo;
    ProcessInfo : TProcessInformation;
    ProcessInformation: TProcessInformation;
    Buf: PChar;
    Command_line: string;
    DosApp: string;   
    procedure UpdateShow;
  protected
    procedure Execute; override;
  public
    constructor Create(CreateSuspended:boolean; AppName: string);
  end;

var
  aRunDosThread: TRunDosThread;

implementation

uses  GlobalPara, Main;

{ Important: Methods and properties of objects in visual components can only be
  used in a method called using Synchronize, for example,

      Synchronize(UpdateCaption);

  and UpdateCaption could look like,

    procedure NewRoutingThread.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }

{ NewRoutingThread }

constructor TRunDosThread.Create(CreateSuspended:boolean; AppName: string);
begin
  DosApp := AppName;
  FreeOnTerminate := True;
  inherited Create(CreateSuspended);
end;

procedure TRunDosThread.UpdateShow;
var
  s: string;
  i: integer;
begin
  s := string(Buf);
  i:=0;
  if Length(s)=0 then Exit;
  while True do begin
    i:=i+1;
    if s[i] = #10 then begin
       Insert(#13,s,i);
       i:=i+1;
    end;
    if i>=Length(s) then Break;
  end;

  MainForm.redtLog.Text := MainForm.redtLog.Text + s;
  MainForm.redtLog.Refresh;
end;

procedure TRunDosThread.Execute;
var
  dwRead: DWORD;
  len: integer;
begin
  { Place thread code here }

  // 创建与Spawn.exe通讯的可继承的匿名管道
  with Security do begin
    nlength := SizeOf(TSecurityAttributes);
    binherithandle := True;
    lpsecuritydescriptor := nil;
  end;
  if not Createpipe (ReadPipe, WritePipe, @Security, 0) then begin
     PostMessage(MainForm.Handle,WM_CREATEPIPE_ERROR,0,0);
     Exit;
  end;

  // 准备Spawn的命令行,在命令行给出写管道句柄和要Spawn执行的命令
  FmtStr(command_line, 'spawn -h %d %s',[WritePipe, DosApp]);
  // 子进程以隐藏方式运行
  with StartInfo do begin
    cb := SizeOf(TStartUpInfo);
    dwFlags :=STARTF_USESHOWWINDOW;
    wShowWindow := SW_HIDE;
  end;

  // 创建Spawn子进程
  if not CreateProcess( nil, PChar(Command_line), nil, nil, TRUE,
                    0, nil, nil, StartInfo, ProcessInfo) then begin
     PostMessage(MainForm.Handle,WM_CREATEPROCESS_ERROR,0,0);
     Exit;
  end;

  // 读管道,并显示Spawn从管道中返回的输出信息
  if (not ReadFile(ReadPipe, len, Sizeof(integer), dwRead, nil)) or (dwRead = 0) then Exit;

  while (len<>0) do begin
     Buf := AllocMem(len + 1);
     try
       ZeroMemory(buf,len+1);
       if (not ReadFile(ReadPipe, buf[0], len, dwRead, nil)) or (dwRead = 0) then Exit;
       buf[dwRead]:= #0;
       // 将结果显示在Memo中,并刷新对话框
       Synchronize(UpdateShow);

       if (not ReadFile(ReadPipe, len, Sizeof(integer), dwRead, nil)) or (dwRead = 0) then Exit;

     finally
       FreeMem(Buf);
     end;
  end;

  // 等待Spawn结束
  WaitForSingleObject(ProcessInfo.hProcess, 20000);

  // 关闭管道句柄
  CloseHandle(ProcessInfo.hProcess);
  CloseHandle(ProcessInfo.hThread);
  CloseHandle(ReadPipe);
  CloseHandle(WritePipe);
  //PostMessage(MainFrm.Handle,WM_FINISHED_COMMAND,0,0);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -