使用晚绑定调用DLL(DLL)

一:创建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 .

你可能感兴趣的:(dll)