exports
myproc name 'myproc' index 1,
myfunction name 'myfuntion' index 2 risdent;//输出信息始终保持在内存中{risdent}
//——————————初始化工作
begin
{dll的初始化工作 }
saveexit := exitproc; { 保存原来的退出过程指针 }
exitproc := @libexit; { 安装新的退出过程 }
end.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
表10.1 exitcode的取值与意义 :取 值 意 义
—————————————————————
wep_system_exit windows关闭
wep_free_dllx dlls被卸出
━━━━━━━━━━━━━━━━━━━━━
//--------------调用dll
1。静态调用
在静态调用一个dlls中的过程或函数时,external指示增加到过程或函数的声明语句中。
被调用的过程或函数必须采用远调用模式。这可以使用far过程指示或一个{$f +}编译指示。
delphi全部支持传统windows动态链接库编程中的三种调用方式,它们是:
● 通过过程/函数名
● 通过过程/函数的别名
● 通过过程/函数的顺序号
//————————————————————静态调用举例
unit windows
interface
function findwindowsex(parent,child:hwnd;classname,windowsname:pchar):hwnd;stdcall;
const
user32='user32.dll'
implementation
function findwindowex; external user32 name 'findwindowexa'
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
2. 动态调用
2.1 动态调用中的api函数
动态调用中使用的windows api函数主要有三个,即:loadlibrary,getprocaddress和freelibrary。
1.loadlibrary: 把指定库模块装入内存
语法为: function loadlibrary(libfilename: pchar): thandle;
libfilename指定了要装载dlls的文件名,如果libfilename没有包含一个路径,则windows按下述顺序进行查找:
(1)当前目录;
(2)windows目录(包含win.com的目录)。函数getwindowdirectory返回这一目录的路径;
(3)windows系统目录(包含系统文件如gdi.exe的目录)。函数getsystemdirectory返回这一目录的路径;
(4)包含当前任务可执行文件的目录。利用函数getmodulefilename可以返回这一目录的路径;
(5)列在path环境变量中的目录;
(6)网络的映象目录列表。
如果函数执行成功,则返回装载库模块的实例句柄。否则,返回一个小于hinstance_error的错误代码。错误代码的意义如下表:
表10.2 loadlibrary返回错误代码的意义
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
错误代码 意 义
——————————————————————————————————————
0 系统内存不够,可执行文件被破坏或调用非法