(* 直接看源码 *)
{创建 DLL}
library Mydll;
{$R 'MyRes.res' 'MyRes.rc'}
uses
SysUtils,
Classes,
dialogs;
{$R *.res}
procedure testDLL; stdcall;
begin
ShowMessage('DLL 测试');
end;
function test_i_j(i,j :integer):string; stdcall;
begin
result := IntToStr(i + j);
end;
EXPORTS
testDLL, test_i_j;
begin
end.
{动态 or 静态 载入DLL函数或过程 }
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
button1: TButton;
procedure FormPaint(Sender: TObject);
procedure button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
//procedure testDLL; stdcall; external 'MyDLL.dll'; {静态掉用}
implementation
{$R *.dfm}
type Tfn1 = function (i,j:integer):string; stdcall;
type Tproc1 = procedure; stdcall;
procedure TForm1.button1Click(Sender: TObject);
var
h:THandle;
pro1:procedure;
//fn1 :function (i,j:integer):string;
FPointer :TFarProc;
fn1 :Tfn1;
proc1:Tproc1;
begin
{动态调用}
h := LoadLibrary(PCHAR('D:\Program\LoadLibrary\Mydll.dll'));
if h <> 0 then
begin
{调用过程}
//@pro1 := GetProcAddress(h, 'testDLL');
FPointer := GetProcAddress(h, 'testDLL'); {注意 "testDLL" 大小写敏感}
proc1 := Tproc1(FPointer);
//if @pro1 <> nil then pro1;
proc1;
{调用函数}
(* 如果直接定义 函数 fn1 :function (i,j:integer):string; 可以这样直接
@fn1 := GetProcAddress(h, 'test_i_j'); 获取地址,然后调用 ,不透过 TFarProc *)
//@fn1 := GetProcAddress(h, 'test_i_j');
FPointer := GetProcAddress(h, 'test_i_j');
fn1 := Tfn1(FPointer);
//if @fn1 <> nil then Showmessage(fn1(1, 9));
Showmessage(fn1(1, 9));
end;
FreeLibrary(h); {释放}
end;
end.