delphi中的dll使用方法
点击次数:51 次 发布日期:2008-11-09 08:37:38 作者:源代码网
|
源代码网推荐 源代码网推荐 2. 确认 DLL export 出来的函式的原型, 以目前的情况而言, 通常只拿得到 C 源代码网推荐 源代码网推荐 语言的函数原型,这时要注意 C 与 object Pascal 相对应的型别, 如果需要, 在 源代码网推荐 源代码网推荐 interface 一节定义所需的资料类别 源代码网推荐 源代码网推荐 3. 在 implementation 节中宣告欲使用的函式, 语法大致如下: 源代码网推荐 源代码网推荐 procedure ProcName(Argu...); far; external "DLL档名"; 源代码网推荐 源代码网推荐 index n; 源代码网推荐 源代码网推荐 function FuncName(Argr...): DataType; far; 源代码网推荐 源代码网推荐 external "DLL档名"; index n; 源代码网推荐 源代码网推荐 宣告时, index n 如果不写, 便是参考资料中所谓 import by name 的方式, 此 源代码网推荐 源代码网推荐 时, 由於需要从 DLL 的 name table 中找出这个函式, 因此, 连结执行速度比 源代码网推荐 源代码网推荐 import by ordinal稍慢一些, 此外, 还有一种 by new name, 由於我没用过, 您可 源代码网推荐 源代码网推荐 以查一参考资料, 大意是可以 import 後改用另一个程式命名呼叫这个函式 源代码网推荐 源代码网推荐 4. 然後, 呼叫与使用就与一般的Delphi 没有两样 源代码网推荐 源代码网推荐 5. 上述是直接写到呼叫DLL函式的程式单元中, 此外,也可以将DLL的呼叫宣告集 源代码网推荐 源代码网推荐 中到一个程式单元(Import unit), Delphi 内附的 WinTypes, WinProcs是一个例子, 源代码网推荐 源代码网推荐 您可以参考一下,同时观察一下 C 与 Pascal 互相对应的资料型态 源代码网推荐 源代码网推荐 6. 除了上述的 static import 的方式, 另外有一种 dynamic import 的写法, 源代码网推荐 源代码网推荐 先宣告一个程序类型(procedural-type),程式执行时, 以 LoadLibrary() API Load 源代码网推荐 源代码网推荐 进来後, 再以 GetProcAddress() API 取得函式的位址的方式来连结呼叫, 在Object 源代码网推荐 源代码网推荐 Pascal Language Guide P.132-133 有一个例子, 您可以参考看看 源代码网推荐 源代码网推荐 如果要举个例子, 以下是从我以前的程式节录出来的片断: 源代码网推荐 源代码网推荐 (* for CWindows 3.1 *) 源代码网推荐 源代码网推荐 unit Ime31; 源代码网推荐 源代码网推荐 interface 源代码网推荐 源代码网推荐 uses 源代码网推荐 源代码网推荐 SysUtils, WinTypes, WinProcs, Dialogs; 源代码网推荐 源代码网推荐 type 源代码网推荐 源代码网推荐 (* 必要的资料型态宣告 *) 源代码网推荐 源代码网推荐 tDateNTime = record 源代码网推荐 源代码网推荐 wYear, wMonth, wDay: word; 源代码网推荐 源代码网推荐 wHour, wMin, wSec: word; 源代码网推荐 源代码网推荐 end; 源代码网推荐 源代码网推荐 TImePro = record 源代码网推荐 源代码网推荐 hWndIme: HWnd; { IME handle } 源代码网推荐 源代码网推荐 dtInstDate: tDateNTime; { Date and time of installation } 源代码网推荐 源代码网推荐 wVersion: word; { the version of IME } 源代码网推荐 源代码网推荐 szDescription: array[0..49] of byte; { Description of IME module} 源代码网推荐 源代码网推荐 szName: array[0..79] of byte; { Module name of the IME } 源代码网推荐 源代码网推荐 szOptions: array[0..29] of byte; { options of IME at startup} 源代码网推荐 源代码网推荐 fEnable: boolean; { IME status; True=activated,False=deactivated } 源代码网推荐 源代码网推荐 end; 源代码网推荐 源代码网推荐 pTImePro = ^TImePro; 源代码网推荐 源代码网推荐 function SetIme(const sImeFileName: string): boolean; far; 源代码网推荐 源代码网推荐 implementation 源代码网推荐 源代码网推荐 (* begin 呼叫 winnls.dll export 函数的宣告 *) 源代码网推荐 源代码网推荐 function ImpSetIme(hWndIme: HWND; lpImePro: pTImePro): boolean;far; external "winnls.dll"; 源代码网推荐 源代码网推荐 (* end 呼叫 winnls.dll export 函数的宣告 *) 源代码网推荐 源代码网推荐 (* -------------------------------------------------- *) 源代码网推荐 源代码网推荐 (* SetIme(const sImeFileName: string): boolean; 源代码网推荐 源代码网推荐 (* ====== 源代码网推荐 源代码网推荐 (* 切换到某一特定的输入法 源代码网推荐 源代码网推荐 (* 源代码网推荐 源代码网推荐 (* 传入引数: 源代码网推荐 源代码网推荐 (* sImeFileName: 输入法 IME 档名, 例: phon.ime; 源代码网推荐 源代码网推荐 (* 空字串: 英数输入法 源代码网推荐 源代码网推荐 (* 源代码网推荐 源代码网推荐 (* 传回值: 源代码网推荐 源代码网推荐 (* True: 切换成功 源代码网推荐 源代码网推荐 (* False: 失败 源代码网推荐 源代码网推荐 (* -------------------------------------------------- *) 源代码网推荐 源代码网推荐 function SetIme(const sImeFileName: string): boolean; 源代码网推荐 源代码网推荐 var 源代码网推荐 源代码网推荐 pImePro: pTImePro; 源代码网推荐 源代码网推荐 begin 源代码网推荐 源代码网推荐 Result := False; 源代码网推荐 源代码网推荐 if MaxAvail < SizeOf(TImePro) then 源代码网推荐 源代码网推荐 begin 源代码网推荐 源代码网推荐 MessageDlg("记忆体不足", mtWarning, [mbOk], 0); 源代码网推荐 源代码网推荐 Exit; 源代码网推荐 源代码网推荐 end 源代码网推荐 源代码网推荐 else 源代码网推荐 源代码网推荐 begin 源代码网推荐 源代码网推荐 New(pImePro); 源代码网推荐 源代码网推荐 try 源代码网推荐 源代码网推荐 if sImeFileName = "" then (* 空字串, 还原到英数输入法 *) 源代码网推荐 源代码网推荐 pImePro^.szName[0] := 0 源代码网推荐 源代码网推荐 else 源代码网推荐 源代码网推荐 StrPCopy(@pImePro^.szName, sImeFileName); 源代码网推荐 源代码网推荐 Result := ImpSetIme(0, pImePro); (* 呼叫 ImpSetIme *) 源代码网推荐 源代码网推荐 finally 源代码网推荐 源代码网推荐 Dispose(pImePro); 源代码网推荐 源代码网推荐 end; { of try } 源代码网推荐 源代码网推荐 end; 源代码网推荐 源代码网推荐 end; { of SetIme } 源代码网推荐 源代码网推荐 end. { of Unit Win31 } 源代码网推荐 源代码网供稿. |
