{$WARN SYMBOL_PLATFORM OFF}
interface
uses
Windows, ActiveX, Classes, ComObj;
type
ICalculator= interface
['{214C8A93-C235-45DB-BEDB-460DA54F3B01}']
function Add(x,y:Integer):Integer;safecall;
function Mult(x,y:Integer):Integer;safecall;
end;
TCalculator = class(TComObject, ICalculator)
protected
{Declare ICalculator methods here}
function Add(x,y:Integer):Integer;safecall; // 加法運算
function Mult(x,y:Integer):Integer;safecall; // 乘法運算
end;
const
Class_Calculator: TGUID = '{E81D22BE-7203-4447-B65C-6FF4CFA7E982}';//聲明guid值這是唯一的。
implementation
uses ComServ;
function TCalculator.Add(x, y: Integer): Integer;
begin
Result:= x+y ;//
end;
function TCalculator.Mult(x, y: Integer): Integer;
begin
Result:= x*y ;
end;
initialization
TComObjectFactory.Create(ComServer, TCalculator, Class_Calculator,
'Calculator', '', ciMultiInstance, tmApartment);//初始化建立唯一的guid值的com對象
end.
―――――――
客戶端當然來利用這個dll文件。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,ActiveX;
const
Class_Calculator: TGUID='{E81D22BE-7203-4447-B65C-6FF4CFA7E982}';//跟服務端值一樣下面我們注冊對象,依這個值向注冊表尋找guid值
type
ICalculator= interface
['{214C8A93-C235-45DB-BEDB-460DA54F3B01}']
function Add(x,y:Integer):Integer;safecall;
function Mult(x,y:Integer):Integer;safecall;
end;
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
FCal:ICalculator;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses ComServ;
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit3.Text:= IntToStr(FCal.Add(StrToInt(Edit1.Text), StrToInt(Edit2.Text)));
Edit4.Text:= IntToStr(FCal.Mult(StrToInt(Edit1.Text), StrToInt(Edit2.Text)));
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
CoCreateInstance(Class_Calculator,nil,CLSCTX_INPROC_SERVER,ICalculator,FCal);//建立com對象實例
end;
end.
――――――――――――――――――――――――――――――――――――
搞定當然我們要向系統注冊這個com對象文件,要不客戶如何應用,
當然注冊這個dll依照這個guid值的來的,這是唯一的,
注冊com對象文件
Regsvr32 dll文件路徑
――――――――――――――――
對於上一篇的代碼還不夠簡單,讓大家再簡單的com接口應用
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
ipggpjj= Interface(IUnknown)
procedure pggpjj1;
nd;
Tpggpjj=class(TInterfacedObject,Ipggpjj)
public
procedure pggpjj1;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure Tpggpjj.pggpjj1;
begin
showmessage('成功');
end;
procedure TForm1.Button1Click(Sender: TObject);
var pggpjj:ipggpjj;
begin
pggpjj:=Tpggpjj.Create;
pggpjj.pggpjj1;
end;