delphi7调用DLL与DELPHI6不同吗,程序是书本上的例子,如下通不过;马上给分
library DLLShowForm;
uses
SysUtils,
Classes,
DLLFrm in 'DLLFrm.pas' {frmDLL};
{$R *.res}
//输出ShowDLLModalForm,ShowDLLForm接口方法,以便外部程序调用
exports
ShowDLLModalForm, ShowDLLForm;
begin
end.
//-------------------------------------
unit DLLFrm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TfrmDLL = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;
{声明要引出的方法}
procedure ShowDLLModalForm(aHandle: THandle); stdcall; //模式显示窗口
procedure ShowDLLForm(aHandle: THandle); stdcall; //非模式显示窗口
implementation
{$R *.dfm}
//模式显示窗口
procedure ShowDLLModalForm(aHandle: THandle);
begin
Application.Handle := aHandle; //传递应用程序句柄
with TfrmDLL.Create(Application) do //创建窗体
begin
try
ShowModal; //模式显示窗体
finally
free;
end;
end;
end;
//非模式显示窗口
procedure ShowDLLForm(aHandle: THandle);
begin
Application.Handle := aHandle; //传递应用程序句柄
with TfrmDLL.Create(application) do //创建窗体
Show; //非模式显示窗体
end;
end.
//--------调用-----------
unit MainFrm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
//定义DLL中引出的过程类型
TShowDLLForm = procedure(aHandle: THandle); stdcall;
EDLLError = class(Exception);
TFrmMain = class(TForm)
btnShowModal: TButton;
btnShow: TButton;
procedure btnShowModalClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btnShowClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
//指向加载后DLL句柄
DLLHandle: THandle;
{ Private declarations }
public
{ Public declarations }
end;
var
FrmMain: TFrmMain;
implementation
{$R *.dfm}
//窗体创建时,加载DLL
procedure TFrmMain.FormCreate(Sender: TObject);
begin
DLLHandle := LoadLibrary('DLLShowForm.dll');
{如果DLLHandle为0,代表加载DLL失败}
if DLLHandle = 0 then
raise EDLLError.Create('不能加载DLLShowForm.dll');
end;
//窗体释放时,卸载DLL
procedure TFrmMain.FormDestroy(Sender: TObject);
begin
FreeLibrary(DLLHandle);
end;
//模式显示窗口
procedure TFrmMain.btnShowModalClick(Sender: TObject);
var
ShowDLLModalForm: TShowDLLForm;
begin
{链接DLL中的输出函数,以被调用}
if (@ShowDLLModalForm = nil) then
RaiseLastWin32Error;
ShowDLLModalForm(Application.Handle);
end;
//非模式显示窗口
procedure TFrmMain.btnShowClick(Sender: TObject);
var
ShowDLLForm: TShowDLLForm;
begin
@ShowDLLForm := GetProcAddress(DLLHandle, 'ShowDLLForm');
if (@ShowDLLForm = nil) then
RaiseLastWin32Error;
ShowDLLForm(Application.Handle);
end;
end.