日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Delphi DLL使用接口和调用的方法

發(fā)布時間:2024/5/14 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Delphi DLL使用接口和调用的方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Delphi對接口采用引用計數(shù)的方法管理對象生命周期,但是DLL中輸出的對象可能不是被Delphi調(diào)用,其引用計數(shù)不一定正確,因此DLL中接口對象的生命周期不由Delphi編譯器自動生成的代碼管理,而是程序員自己控制,所以上面的工廠包括構(gòu)造和解析兩個接口對象的生命周期管理方法。

所有接口對象應該集成自下面的接口,而不應該繼承自Delphi自帶的TInterfacedObject:

如果在DLL的函數(shù)中使用了字符串,還有記得在DLL和調(diào)用DLL的工程中單元的首行加入ShareMem,使用ShareMem后在發(fā)布執(zhí)行執(zhí)行程序是記得包含borlndmm.dll。

(一)DLL源碼:

接口定義單元uInf.pas;

unit uInf;


interface


type


? TIntObject = class(TObject, IInterface)
? protected
? ? function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
? ? function _AddRef: Integer; stdcall;
? ? function _Release: Integer; stdcall;
? end;


? ITest = interface
? ? ['{0FA49A71-48BF-40CD-9D77-63B233C4F717}']
? ? function GetMethod: Integer;
? end;

implementation


function TIntObject.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
? if GetInterface(IID, Obj) then
? ? Result := 0
? else
? ? Result := E_NOINTERFACE;
end;


function TIntObject._AddRef: Integer;
begin
? Result := -1;
end;


function TIntObject._Release: Integer;
begin
? Result := -1
end;


end.

接口實現(xiàn)單元uDo.pas

unit uDo;


interface


uses Classes, Dialogs, SysUtils, uInf;


type
? TTest = class(TIntObject, ITest)
? public
? ? function GetMethod: Integer;
? public
? ? constructor Create;
? ? destructor Destroy; override;
? end;


function GetITest: ITest;


var
? gTest: TTest;
??
implementation



{ TTest }


constructor TTest.Create;
begin


end;


destructor TTest.Destroy;
begin


? inherited;
end;


function TTest.GetMethod: Integer;
begin
? ShowMessage('TTest.GetMethod');
end;


function GetITest: ITest;
begin
? if not Assigned(gTest) then
? ? gTest := TTest.Create;
? Result := gTest;
end;


initialization
? GetITest();


finalization
? if Assigned(gTest) then
? ? FreeAndNil(gTest);


end.


執(zhí)行程序在調(diào)用DLL接口時引用接口單元uInf.pas

Proc = function: IInterface;

加載:

var
? P: Proc;
begin
? P := nil;
? myDLLHandle := loadlibrary('Project2.dll');
? P := GetProcAddress(myDLLHandle, 'GetITest');
? FTestObj := ITest(P);
end;

調(diào)用:

FTestObj.GetMethod();

釋放:

? Pointer(FTestObj) := nil;
? FreeLibrary(myDLLHandle);

總結(jié)

以上是生活随笔為你收集整理的Delphi DLL使用接口和调用的方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。