Com对象代码:
library SumSvr;
uses
ComServ,
USum in 'USum.pas',
UISum in 'UISum.pas',
SumSvr_TLB in 'SumSvr_TLB.pas';
exports
DllGetClassObject,
DllCanUnloadNow,
DllRegisterServer,
DllUnregisterServer;
{$R *.TLB}
{$R *.RES}
begin
end.
unit UISum;
interface
type
ISum = Interface
['{A39177C7-EA39-4719-B24F-AED9A00D3856}']
function Sum(const a,b:integer):integer;
end;
const
Class_Sum: TGUID = '{A39177C7-EA39-4719-B24F-AED9A00D3856}';
implementation
end.
unit USum;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
Windows, ActiveX, Classes, ComObj,UISum;
type
TSum = class(TComObject,ISum)
public
function Sum(const a,b:integer):integer;
end;
implementation
uses ComServ;
{ TSum }
function TSum.Sum(const a, b: integer): integer;
begin
result:=a+b;
end;
initialization
TComObjectFactory.Create(ComServer, TSum, Class_Sum,
'Sum', '', ciMultiInstance, tmApartment);
end.
客户程序:
procedure TForm1.Button1Click(Sender: TObject);
var
FInts:ISum;
begin
FInts := CreateComObject(Class_Sum) as ISum;//创建COM
showmessage(inttostr(FInts.Sum(1,2)));
if FInts<>nil then FInts:=nil;
end;