unit fmMain;

interface

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

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    Edit2: TEdit;
    btnCoCreate: TButton;
    btnFactory: TButton;
    Edit3: TEdit;
    Edit4: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure btnCoCreateClick(Sender: TObject);
    procedure btnFactoryClick(Sender: TObject);
  private
    { Private declarations }
    lStart : Longint;
    lEnd : Longint;

    procedure GetEndTime;
    procedure ShowRunTime(aEdit : TEdit); 
    procedure GetStartTime; 
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses ActiveX;

procedure TForm1.Button1Click(Sender: TObject);
var
  iun : IUnknown;
  aIntf : IDemoCOM;
  aIS : ISecond;
  aObj : TObject;
begin
  iun := CreateCOMObject(CLASS_DemoCOM);
  OleCheck(iun.QueryInterface(IID_IDemoCOM, aIntf));
  Edit1.Text := aIntf.GetSystemTime;
  aIS := aIntf as ISecond;
  Edit2.Text := aIS.GetGreeting;
end;

const ITIMES = 1000;

procedure TForm1.btnCoCreateClick(Sender: TObject);
var
  iCount : Integer;
  aIntf : IDemoCOM;
begin
  GetStartTime;
  for iCount := 0 to ITIMES - 1 do    // Iterate
  begin
    aIntf := CoDemoCOM.Create;
    Self.Edit1.Text := aIntf.GetSystemTime;
    aIntf := nil;
  end;    // for
  GetEndTime;
  ShowRunTime(Edit3);
end;

const
  IID_Factory: TGUID = '{00000001-0000-0000-C000-000000000046}';

procedure TForm1.btnFactoryClick(Sender: TObject);
var
  iCount : Integer;
  aIntf : IDemoCOM;
  aFactory : IClassFactory;
begin
  GetStartTime;
  OleCheck(CoGetClassObject(CLASS_DemoCOM, CLSCTX_INPROC_SERVER or CLSCTX_LOCAL_SERVER,
           nil, IID_Factory, aFactory));
  for iCount := 0 to ITIMES - 1 do    // Iterate
  begin
    aFactory.CreateInstance(nil, IID_IDemoCOM, aIntf);
    Self.Edit2.Text := aIntf.GetSystemTime;
    aIntf := nil;
  end;    // for
  GetEndTime;
  ShowRunTime(Edit4);
end;

procedure TForm1.GetEndTime;
begin
  lEnd := GetTickCount;
end;

procedure TForm1.GetStartTime;
begin
  lStart := GetTickCount;
end;

procedure TForm1.ShowRunTime(aEdit : TEdit);
begin
  aEdit.Text := FloatToStr((lEnd - lStart) / 1000.0) + '¬í';
end;

end.
