1. File---->New---->ActiveX------>ActiveXLibrary---->OK------>Save:ComSrv.dpr
library ComSrv;
uses
ComServ,
ComCls in 'ComCls.pas',
Calc in 'Calc.pas',
ComSrv_TLB in 'ComSrv_TLB.pas';
exports
DllGetClassObject,
DllCanUnloadNow,
DllRegisterServer,
DllUnregisterServer;
{$R *.TLB}
{$R *.RES}
begin
end.
2. File---->New---->ActiveX------>ComObject----->OK----Save:Calc.pas
unit Calc;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
Windows, ActiveX, Classes, ComObj, ComCls;
type
TCalc = class(TComObject,ITest)
protected
public
function CalcHe(one,two: integer): integer;
end;
const
Class_Calc: TGUID = '{471E8DCB-C4F8-49F8-A5C8-036E9B605430}';
implementation
uses ComServ;
{ TCalc }
function TCalc.CalcHe(one, two: integer): integer;
begin
Result := one + two;
end;
initialization
TComObjectFactory.Create(ComServer, TCalc, Class_Calc,
'Calc', '', ciMultiInstance, tmApartment);
end.
3. File---->New---->Unit---->OK----->Save:Calc.pas
unit Calc;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
Windows, ActiveX, Classes, ComObj, ComCls;
type
TCalc = class(TComObject,ITest)
protected
public
function CalcHe(one,two: integer): integer;
end;
const
Class_Calc: TGUID = '{471E8DCB-C4F8-49F8-A5C8-036E9B605430}';
implementation
uses ComServ;
{ TCalc }
function TCalc.CalcHe(one, two: integer): integer;
begin
Result := one + two;
end;
initialization
TComObjectFactory.Create(ComServer, TCalc, Class_Calc,
'Calc', '', ciMultiInstance, tmApartment);
end.
4.注册:Run----->Register ActiveX Server
服务器至此OK了,下面建立客户端:见文件:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCls, ComObj;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
Test: ITest;
i: integer;
begin
Test := CreateComObject(Class_Calc) as ITest;
i := Test.CalcHe(10,15);
showmessage(inttostr(i));
end;
end.