2012년 1월 9일 월요일

Hide command-line command runs.


unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls , Generics.Collections ;

type
  TCmdProc = class
  private
  FCMDText : string;
    FRun: boolean;
    FProcHandleList : TList;
    FRunCount: integer;
    function GetIsRun: boolean;
    procedure SetRun(const Value: boolean);
    procedure RunExecute;
    procedure KillProcess( hProcess: int64);
  public

  constructor Create;
    destructor Destroy; override;
  property Run : boolean read GetIsRun write SetRun;
  property CMDText : string read FCMDText write FCMDText;
    property RunCount : integer read FRunCount write FRunCount;

  end;


  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Memo1: TMemo;
    Label1: TLabel;
    Button2: TButton;
    Memo2: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }

    FCmdProc : TCmdProc;

  public
    { Public declarations }


  end;


var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
memo2.Text := '';
  FCmdProc.FCMDText := Memo1.Lines.Text;
  FCmdProc.RunCount :=StrToIntDef(edit1.Text, 10);
FCmdProc.Run := true;
end;

{ TCmdProc }

constructor TCmdProc.Create;
begin
FRun := false;
  FCmdText := '';

  FProcHandleList := TList.Create;

end;

destructor TCmdProc.Destroy;
begin
FProcHandleList.Clear;
  FreeAndNil(FProcHandleList);

  inherited;
end;

function TCmdProc.GetIsRun: boolean;
begin
result := FRun;
end;

procedure TCmdProc.KillProcess(hProcess: int64);
begin
 if TerminateProcess(hProcess, 0) = FALSE then begin
   form1.memo2.lines.add( InttoStr(hProcess) + ' : KillProcess error !')
 end else begin
   form1.memo2.lines.add( InttoStr(hProcess) + ' : KillProcess successfully !');
 end;
end;

procedure TCmdProc.RunExecute;
var
StartInfo  : TStartupInfo;
  ProcInfo   : TProcessInformation;
  CreateOK   : Boolean;
  i : integer;
begin
  FillChar(StartInfo, SizeOf(TStartupInfo),#0);
  FillChar(ProcInfo,  SizeOf(TProcessInformation),#0);
  StartInfo.cb := SizeOf(TStartupInfo);
  StartInfo.dwFlags := STARTF_USESHOWWINDOW;
  StartInfo.wShowWindow := SW_HIDE;

for I := 1 to FRunCount do begin
    if CreateProcess(nil,PChar(CMDText), nil, nil, False, CREATE_NEW_CONSOLE , nil, nil, StartInfo, ProcInfo) then begin
      if Assigned(FProcHandleList) then begin
        FProcHandleList.Add(InttoStr(ProcInfo.hProcess));
        form1.Memo2.Lines.Add(IntToStr(ProcInfo.hProcess) + ' is Run ');
      end;
    end else begin
form1.Memo2.Lines.Add('CMD Run Fail ');
    end;
  end;

end;

procedure TCmdProc.SetRun(const Value: boolean);
var
  hProc : String;
begin

if Value then begin
    RunExecute;
  end else begin

  while FProcHandleList.Count <> 0 do begin
    hProc := FProcHandleList.Items[0];
  KIllProcess(StrToIntDef(hProc, 0));
      FProcHandleList.Remove(hProc);
    end;
  end;


end;

procedure TForm1.Button2Click(Sender: TObject);
begin
FCmdProc.Run := false;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
FCmdProc := TCmdProc.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
if Assigned(FCmdProc) then begin
  FreeAndNil(FCmdProc);
  end;
end;

end.