二. 接口類的具體實現.
1.給接口類中的方法添加具體的代碼.這里為了能讓某人能看懂,寫的很簡單:
function TSample.GetRoomInfo(const RoomNo: WideString;
Account: SYSINT): WideString;
begin
if Account > 0 then
Result := '這是單賬戶客人,房間號為: ' + RoomNo
else
Result := '這是多賬戶客人.房間號為: ' + RoomNo ;
Result := RoomNo;//這個只是為了讓某人看懂,代碼大家可以自己寫.
end;
2.注冊創建COM對象
◎选择菜单“Run – Register ActiveX Server”,或者通过Windows下system/system32目录中的regsvr32.exe程序注册编译好的DLL文件。regsvr32的具体参数可以通过regsvr32/?来获得。对于进程外(EXE型)的COM对象,执行一次应用程序就注册了。
提示DLL注册成功.
三.COM對象的調用
新建一個Application,在窗體中放置兩個Edit,分別是用來傳遞 RoomNo,和Account的. 代碼如下:
implementation
uses ComObj,Project1_TLB;//這里的引用需要注意.
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
COMSvr : ISample;
begin
COMSvr := CreateComObject(CLASS_Sample) as ISample;
//也可以使用Guid 实例化
//COMSvr:=CreateComObject(StringToGuid('{330B6DA3-F7F6-49DD-B552-68769A916D66}')) as ISample;
if COMSvr <> nil then
begin
try
ShowMessage(COMSvr.GetRoomInfo(Edit1.Text,StrToInt(Edit2.Text)));
finally
COMSvr := nil;
end;
end;
end;
end.
運行就可以看到效果了.
◎ : 摘自網絡文章(更多請看):
http://www.host01.com/article/software/Delphi/20060917183817287_3.htm