龙七

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一:创建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<>0then
begin
@TestSum:
=GetProcAddress(dllhande,'TestSum');
if @TestSum=nilthen
begin
ShowMessage(
'dll函数不存在');
end;
//else ShowMessage('调用dll成功');
end
else ShowMessage('Dll不存在');

finalization

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

end.
posted on 2011-07-13 10:29  龙七  阅读(417)  评论(0编辑  收藏  举报