调用Dll在窗体关闭时提示无效的指针操作异常,大家帮忙看看. ( 积分: 50 )

R

root_zh

Unregistered / Unconfirmed
GUEST, unregistred user!
Dll声明:
library BDEAM_TCMS_FORMS;

uses
SysUtils,
Classes,
Forms,
Windows,
Controls,
ActnList,
Dialogs,
ubdPluginManager,
ubdGlobalService,
U_TCMSMainForm in 'U_TCMSMainForm.pas' {TCMSMainForm};

type
TCMSFormPlugin = Class( TbdPlugin )
public
function CreateAPluginForm( aGlobalService : TbdGlobalService ) : TForm;
override;
function CreateAPluginChildForm(AParentForm: TForm) : TForm ;
override;
function CallPluginForm(aGlobalService : TbdGlobalService;
var AFormName,AFormCaption : string): Boolean;
override;
function GetPluginForm : TForm;
override;
//function GetCommAction(aActionType : Integer): TAction;
override;
end;

var
SavedDLLApp : TApplication;
SavedScreen : TScreen;

{$R *.res}

//--------------------------------------------
// 获取数据维护模块窗体的实例
//--------------------------------------------
function TCMSFormPlugin.GetPluginForm : TForm;
begin

Result := frmTCMSForm;
end;


function TCMSFormPlugin.CreateAPluginForm( aGlobalService : TbdGlobalService ) : TForm;
begin

frmTCMSForm := TTCMSMainForm.Create( Application );
frmTCMSForm.Init( aGlobalService );
Result := frmTCMSForm;
end;


//-----------------------------------------------------------
//创建指定窗体的子窗体
//-----------------------------------------------------------
function TCMSFormPlugin.CreateAPluginChildForm(AParentForm: TForm) : TForm ;
begin

frmTCMSForm := TTCMSMainForm.Create( AParentForm );
Result := frmTCMSForm;
end;


//------------------------------------------------------------------------------------------------------
//创建窗体并设获取窗体的名称
//------------------------------------------------------------------------------------------------------
function TCMSFormPlugin.CallPluginForm(aGlobalService : TbdGlobalService;
var AFormName,AFormCaption : string): Boolean;
begin

if not Assigned( frmTCMSForm ) then

begin

frmTCMSForm := TTCMSMainForm.Create( Application );
frmTCMSForm.Init( aGlobalService );
end;

AFormName := frmTCMSForm.Name;
AFormCaption := frmTCMSForm.Caption;
Result := true;
end;


function PluginObjCreateProc(aApplication : TApplication;
aScreen : TScreen;
aGlobalService : TbdGlobalService) : IbdPlugin;
var
lPlugin : TbdPlugin;
begin

// ShowMessage( '建立.' );
Application := aApplication;
Screen := aScreen;

lPlugin := TCMSFormPlugin.Create( aGlobalService );
lPlugin.Title := '测试接口名称';
Result := lPlugin;
end;


procedure LibExit(Reason: Integer);
begin

if (Reason = DLL_PROCESS_DETACH) or (Reason = DLL_THREAD_DETACH) then

begin


if Assigned(SavedDLLApp) then

Application := SavedDLLApp;

if Assigned(SavedScreen) then

Screen := SavedScreen;

SavedDLLApp := nil;
SavedScreen := nil;
end;

ShowMessage( 'Free.' );
end;


exports
PluginObjCreateProc;

begin

SavedDLLApp := Application;
SavedScreen := Screen;
DllProc := @LibExit;

end.


exe声明:
unit EntryForm;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,
ubdPluginManager,
ubdGlobalService;

type
InvokeFunction = function (aApplication : TApplication;
aScreen : TScreen;
aGlobalService : TbdGlobalService) : IbdPlugin;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
//procedure FormClose(Sender: TObject;
var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;


var
Form1: TForm1;
m_plugin : IbdPlugin;
m_GlobalService : TbdGlobalService;
dllHandle : THandle;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
func : InvokeFunction;
frm : TForm;
begin

m_GlobalService := TbdGlobalService.Create( Application,Screen );

dllHandle := LoadLibrary( PChar( GetCurrentDir() + '/BDEAM_TCMS_FORMS.dll' ) );
if dllHandle = 0 then

begin

m_GlobalService.ShowErrMsg( '装载DLL失败!' );
exit;
end;

try
@func := GetProcAddress( dllHandle,PChar( 'PluginObjCreateProc' ) );

if @func = nil then

begin

m_GlobalService.ShowInfoMsg( '未找到指定的方法!' );
FreeLibrary( dllHandle );
exit;
end;

m_plugin := func( Application,Screen,m_GlobalService );
if m_plugin = nil then

begin

m_GlobalService.ShowErrMsg( '调用方法失败.' );
FreeLibrary( dllHandle );
exit;
end;

frm := m_plugin.CreateAPluginForm( m_GlobalService );
frm.Show;
finally

end;

end;


procedure TForm1.FormDestroy(Sender: TObject);
begin

FreeLibrary( dllHandle );//此处已显示释放的对话框.

end;


end.
 
顶部