M
mentoro
Unregistered / Unconfirmed
GUEST, unregistred user!
以下是我的程序,可以正常调用,但是释放时就报内存错误:
源码如下:
//DLL
library SYS_date;
Uses
ShareMem,
ActiveX,
SysUtils,
Classes,
Forms,
windows,
U_DATAFROM in 'U_DATAFROM.pas' {F_DATAFROM};
{$R *.res}
PROCEDURE Open_date(H: THandle; app: TForm); Stdcall;
VAR
frm: TF_DATAFROM;
ptr: PLongInt;
BEGIN
Application.Handle := h;
ptr := @(application.MainForm);
ptr^ := longint(app);
frm := TF_DATAFROM.Create(app);
///////初始化窗体
frm.Caption := frm.TITL.Caption;
frm.font.charset := gb2312_charset;
frm.font.Name := '宋体';
frm.font.size := 10;
frm.ShowModal;
END;
EXPORTS
Open_date;
VAR
DLLApp: TApplication;
PROCEDURE DLLUnloadProc(Reason: Integer);
BEGIN
IF Reason = DLL_PROCESS_DETACH THEN Application := DLLApp; //恢复
END;
BEGIN
DLLApp := Application; //保存 DLL 中初始的 Application 对象
DLLProc := @DLLUnloadProc; //保证 DLL 卸载时恢复原来的 Application
END.
/////////////dll中只是一个简单的窗体而已
主程序调用代码如下:
PROCEDURE Tfrm_hoho.Button12Click(Sender: TObject);
type TOpen_date=PROCEDURE(H: THandle; app: TForm); Stdcall;
VAR
DLLHandle: THandle; //使用DLL句柄
FPointer: pointer; //定义函数入口指针
Open_date: TOpen_date; //定义函数类型变量
BEGIN
DLLHandle := LoadLibrary('SYS_date.dll'); //DLL文件的绝对路径
IF DLLHandle = 0 THEN //判断句柄是否有值
begin
Application.MessageBox('加载错误:SYS_date.dll', '提示', MB_OK +
MB_ICONSTOP);
Exit;
end;
IF DLLHandle > 0 THEN
TRY
FPointer := GetProcAddress(DLLHandle, 'Open_date'); //根据函数名称获得函数指针
IF FPointer <> NIL THEN //判断FPointer是否为空,不为空则得到函数地址
BEGIN
Open_date:=TOpen_date(FPointer); //指针强制转化为函数指针
Open_date(Self.Handle, frm_hoho); //调用导出的函数
END
ELSE
application.MessageBox(' Open_date调用出错', '错误', MB_OK + MB_IconError);
FINALLY
FreeLibrary(DLLHandle);
END;
END;
源码如下:
//DLL
library SYS_date;
Uses
ShareMem,
ActiveX,
SysUtils,
Classes,
Forms,
windows,
U_DATAFROM in 'U_DATAFROM.pas' {F_DATAFROM};
{$R *.res}
PROCEDURE Open_date(H: THandle; app: TForm); Stdcall;
VAR
frm: TF_DATAFROM;
ptr: PLongInt;
BEGIN
Application.Handle := h;
ptr := @(application.MainForm);
ptr^ := longint(app);
frm := TF_DATAFROM.Create(app);
///////初始化窗体
frm.Caption := frm.TITL.Caption;
frm.font.charset := gb2312_charset;
frm.font.Name := '宋体';
frm.font.size := 10;
frm.ShowModal;
END;
EXPORTS
Open_date;
VAR
DLLApp: TApplication;
PROCEDURE DLLUnloadProc(Reason: Integer);
BEGIN
IF Reason = DLL_PROCESS_DETACH THEN Application := DLLApp; //恢复
END;
BEGIN
DLLApp := Application; //保存 DLL 中初始的 Application 对象
DLLProc := @DLLUnloadProc; //保证 DLL 卸载时恢复原来的 Application
END.
/////////////dll中只是一个简单的窗体而已
主程序调用代码如下:
PROCEDURE Tfrm_hoho.Button12Click(Sender: TObject);
type TOpen_date=PROCEDURE(H: THandle; app: TForm); Stdcall;
VAR
DLLHandle: THandle; //使用DLL句柄
FPointer: pointer; //定义函数入口指针
Open_date: TOpen_date; //定义函数类型变量
BEGIN
DLLHandle := LoadLibrary('SYS_date.dll'); //DLL文件的绝对路径
IF DLLHandle = 0 THEN //判断句柄是否有值
begin
Application.MessageBox('加载错误:SYS_date.dll', '提示', MB_OK +
MB_ICONSTOP);
Exit;
end;
IF DLLHandle > 0 THEN
TRY
FPointer := GetProcAddress(DLLHandle, 'Open_date'); //根据函数名称获得函数指针
IF FPointer <> NIL THEN //判断FPointer是否为空,不为空则得到函数地址
BEGIN
Open_date:=TOpen_date(FPointer); //指针强制转化为函数指针
Open_date(Self.Handle, frm_hoho); //调用导出的函数
END
ELSE
application.MessageBox(' Open_date调用出错', '错误', MB_OK + MB_IconError);
FINALLY
FreeLibrary(DLLHandle);
END;
END;