中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Delphi > 综合文章
delphi中的dll使用方法
作者:未知 时间:2005-08-07 20:57 出处:编程爱好者网站 责编:chinaitpower
              摘要:delphi中的dll使用方法
1. 所需动态连结的 DLL 须置放在与执行档同一目录或Windows System 目录

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 } 

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有