library MyDll;
{ 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,forms,
UAbout in 'UAbout.pas' {frmAbout};
{$R *.res}
function MyAdd(x,y:integer):integer;stdcall;
begin
result:=x+y;
end;
procedure ShowAbout(PName,Author,Version,Contact:string);stdcall;
begin
frmAbout:=TfrmAbout.Create(Application);
with frmAbout do
begin
Label1.Caption:=PName;
Label2.Caption:='作者:'+Author;
Label3.Caption:='版本号:'+Version;
Label5.Caption:=Contact;
end;
frmAbout.ShowModal;
frmAbout.Free;
end;
exports
MyAdd, ShowAbout;
begin
// ShowMessage('Hi,dll!');
end.
unit UAbout;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, jpeg, ExtCtrls;
type
TfrmAbout = class(TForm)
Image1: TImage;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmAbout: TfrmAbout;
implementation
{$R *.dfm}
end.
unit UdllClient;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function MyAdd(x,y:integer):integer;stdcall;external 'MyDll.dll';
procedure ShowAbout(PName,Author,Version,Contact:string);stdcall;external 'MyDll.dll';
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('3+5='+IntToStr(MyAdd(3,5)));
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
ShowAbout('TestDll','----','1.0.0','QQ:-------')
end;
end.
unit UDyClient;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TMyProc=procedure;stdcall;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
HInst:THandle;//存储加载的dll文件句柄
FPointer:TFarProc;//dll文件中指定函数地址
MyFunc:TMyProc;//TMyProc过程的变量
begin
HInst:=LoadLibrary('MyDll.dll');//加载dll
if HInst>0 then//如果加载成功
try
FPointer:=GetProcAddress(HInst,'ShowAbout');//获取函数地址
if FPointer<>nil then
begin
MyFunc:=TMyProc(FPointer);
MyFunc('TestDll','----','1.0.0','QQ:-----'); //
end
else//获取函数地址失败
ShowMessage('ShowAbout未找到!');
finally
FreeLibrary(HInst);
end
else
ShowMessage('加载MyDll.dll失败');
end;
end.