代码如下:
library Project2;{ 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,
Dialogs,
Windows,
Unit1 in 'Unit1.pas' {Form1};{$R *.res}procedure DLLHint(dwreason:dword);
begin
case dwreason of
dll_process_attach:showmessage('Loading a DLL...');
dll_process_detach:showmessage('Unloading a DLL...');
dll_thread_attach:showmessage('Attaching a Thread...');
dll_thread_detach:showmessage('Detaching a Thread...');
end;
end;exports
JudgePws;begin
DLLProc:=@DLLHint;
DLLHint(dll_process_attach);
end.
接口单元代码:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, StdCtrls;type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
Edit1: TEdit;
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
private
input:string;
{ Private declarations }
public
{ Public declarations }
end;function JudgePws(AHandle:thandle;AInput:string):boolean;stdcall;var
returnval:string;
implementation{$R *.dfm}function JudgePws(AHandle:thandle;AInput:string):boolean;
var form1:tform1;
begin
result:=false;
application.Handle:=ahandle;
try
form1:=tform1.Create(application);
try
form1.input:=ainput;
form1.ShowModal;
result:=(returnval=trim(form1.Edit1.Text));
finally
form1.Free;
form1:=nil;
end;
except
on e:exception do
messagebox(application.Handle,'DLL Error!!','Warning',mb_iconerror);
end;
end;procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if key=#13 then begin
with adoquery1 do begin
close;
parameters[0].Value:=trim(input);
open;
returnval:=trim(fieldbyname('mm_c').AsString);
end;
end;
end;
end.
我在另一个工程里要显式调用这个DLL,代码如下:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
tJudgePws=procedure(AHandle:thandle;AInput:string);stdcall;
DLLException=class(exception);
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var DLLHandle:thandle;
JudgePws:tJudgePws;
begin
dllhandle:=loadlibrary('project2.dll');
try
if dllhandle=0 then begin
raise dllexception.Create('Loading DLL Error!!!');
exit;
end;
@judgepws:=getprocaddress(dllhandle,'JudgePws');
if assigned(@JudgePws) then
judgepws(application.Handle,'更新库存')
else raiselastwin32error;
finally
freelibrary(dllhandle);
end;
end;end.
但是程序说无法定位到judgepws于project2.dll,我已经把project2.dll放到了system文件夹中.各位帮忙看一看.
library Project2;{ 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,
Dialogs,
Windows,
Unit1 in 'Unit1.pas' {Form1};{$R *.res}procedure DLLHint(dwreason:dword);
begin
case dwreason of
dll_process_attach:showmessage('Loading a DLL...');
dll_process_detach:showmessage('Unloading a DLL...');
dll_thread_attach:showmessage('Attaching a Thread...');
dll_thread_detach:showmessage('Detaching a Thread...');
end;
end;exports
JudgePws;begin
DLLProc:=@DLLHint;
DLLHint(dll_process_attach);
end.
接口单元代码:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, StdCtrls;type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
Edit1: TEdit;
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
private
input:string;
{ Private declarations }
public
{ Public declarations }
end;function JudgePws(AHandle:thandle;AInput:string):boolean;stdcall;var
returnval:string;
implementation{$R *.dfm}function JudgePws(AHandle:thandle;AInput:string):boolean;
var form1:tform1;
begin
result:=false;
application.Handle:=ahandle;
try
form1:=tform1.Create(application);
try
form1.input:=ainput;
form1.ShowModal;
result:=(returnval=trim(form1.Edit1.Text));
finally
form1.Free;
form1:=nil;
end;
except
on e:exception do
messagebox(application.Handle,'DLL Error!!','Warning',mb_iconerror);
end;
end;procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if key=#13 then begin
with adoquery1 do begin
close;
parameters[0].Value:=trim(input);
open;
returnval:=trim(fieldbyname('mm_c').AsString);
end;
end;
end;
end.
我在另一个工程里要显式调用这个DLL,代码如下:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
tJudgePws=procedure(AHandle:thandle;AInput:string);stdcall;
DLLException=class(exception);
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var DLLHandle:thandle;
JudgePws:tJudgePws;
begin
dllhandle:=loadlibrary('project2.dll');
try
if dllhandle=0 then begin
raise dllexception.Create('Loading DLL Error!!!');
exit;
end;
@judgepws:=getprocaddress(dllhandle,'JudgePws');
if assigned(@JudgePws) then
judgepws(application.Handle,'更新库存')
else raiselastwin32error;
finally
freelibrary(dllhandle);
end;
end;end.
但是程序说无法定位到judgepws于project2.dll,我已经把project2.dll放到了system文件夹中.各位帮忙看一看.
tJudgePws=procedure(AHandle:thandle;AInput:string) of TObject;stdcall;
另外注册DLL文件的路径是否与你的程序在同一个目录下。还有一个问题,就是你在DLL工程中没有进行初始化,在调用时会出错。提示你没有进行初始化,并显示一个函数,这时把在DLL工程文件中添加ActiveX单元,在dll_process_attach:showmessage('Loading a DLL...');添加显示的函数进行初始化就行了。