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

被折叠的 条评论
为什么被折叠?



