试着编写了一个带窗体的DLL,可调用的时候总是说无法初始化?疑惑,郁闷
DLL代码:
.DPR
library CheckPasswordForm;
{ 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,
XcqCheck in 'XcqCheck.pas' {XcqForm};
{$R *.res}
begin
end.
DLL中的窗体
XcqCheck.pas
unit XcqCheck;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Mask;
type
TXcqForm = class(TForm)
XcqEdit: TMaskEdit;//密码输入框
OkBtn: TButton;
procedure OkBtnClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
XcqForm: TXcqForm;
Check: Boolean;//验证输入密码的正确性
function CheckPassword: Boolean;export;
//本dll文件的关键实现
implementation
{$R *.dfm}
procedure TXcqForm.OkBtnClick(Sender: TObject);
begin
if XcqEdit.Text = 'xcq' then
Check := True;
Close;
end;
function CheckPassword;export;
begin
Check := False;
XcqForm := TXcqForm.Create(Application);
//创建密码输入框
XcqForm.ShowModal;//显示密码输入框;
//注意:不能用XcqForm.Show!
Result := Check;//返回给调用本dll的应用程序判断
XcqForm.Free;//释放本dll文件所占资源
end;
end.
/////
测试调用的代码
unit Unit1;
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
function CheckPassword: Boolean; external 'CheckPasswordForm.dll' name 'CheckPassword';
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if CheckPassword then
begin
Showmessage('DLL is Ok');//如XcqQuery.Open;
end;
end;