使用晚绑定调用DLL(DLL)

本文详细介绍了如何使用Delphi创建和调用DLL文件的过程。首先展示了DLL的创建方法,包括必要的单元引入和导出函数设置。接着,通过具体的接口实现代码示例解释了如何定义和实现DLL中的函数。最后,提供了调用DLL的步骤及代码,涵盖了加载DLL、获取函数指针及释放资源等关键环节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:创建DLL

View Code
library DemoDll;

{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters.
}

uses
SysUtils,
Classes,
UntFunction
in ' UntFunction.pas ' ;

{ $R *.res }

exports
TestSum name
' TestSum ' ;

begin
end .

二:接口实现

View Code
unit UntFunction;

interface
function TestSum(x,y:Integer):Integer; stdcall ;

implementation
function TestSum(x,y:Integer):Integer; stdcall ;
begin
Result:
= x + y;
end ;

end .

三:调用DLL,声明

View Code
unit UntDll;

interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
MTestSum
= function (x,y:Integer):Integer; stdcall ;

var
dllhande:Integer;
TestSum:MTestSum;

implementation


initialization
dllhande:
= LoadLibrary( ' DemoDll.dll ' );
if dllhande <> 0 then
begin
@TestSum:
= GetProcAddress(dllhande, ' TestSum ' );
if @TestSum = nil then
begin
ShowMessage(
' dll函数不存在 ' );
end ;
// else ShowMessage( ' 调用dll成功 ' );
end
else ShowMessage( ' Dll不存在 ' );

finalization

if dllhande <> 0 then
begin
if @TestSum <> nil then @TestSum: = nil ;
end ;
FreeLibrary(dllhande);

end .
posted on 2011-07-13 10:29 龙七 阅读( ...) 评论( ...) 编辑 收藏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值