调用dll报错的问题
1、dll封装部分
library FrontYanYin;
uses
ShareMem,
SysUtils,
Classes,
Forms,
uAppDcr in 'uAppDcr.pas',
uMVC_API in 'uMVC_API.pas',
Unit1 in 'Unit1.pas' {Form1},
Unit_Drv in 'Unit_Drv.pas',
UnitPublicFun in 'UnitPublicFun.pas',
UnitYYHX in 'UnitYYHX.pas',
XmlHelper in 'XmlHelper.pas';
{$R *.res}
procedure OpenForm;stdcall;export;
begin
Form1 := TForm1.Create(Application);
Form1.ShowModal;
end;
exports
OpenForm;
begin
end.
2、调用部分
unit Unit1;
interface
uses
ShareMem,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
hint : Cardinal;
procedure OpenForm();stdcall;
implementation
procedure OpenForm();stdcall;External'FrontYanYin.dll';
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
OpenForm();
end;
end.
3、报错
运行的时候一切正常。
加上ShareMem,关闭调用函数的时候报错invalid pointer operation;
不加ShareMem,关闭时候报内存地址错误。
[解决办法]
- Delphi(Pascal) code
//procedure OpenForm;stdcall;export;procedure OpenForm;stdcall;begin Form1 := TForm1.Create(Application); Form1.ShowModal;end;
[解决办法]
你需要传调用程序的application或是handle给DLL里面的form
[解决办法]
楼上正解,DLL中的OpenForm需改写
- Delphi(Pascal) code
//MainHandle为主调程序的Application.Handleprocedure OpenForm(MainHandle: THandle); stdcall;begin Application.Handle := MainHandle; Form1 := TForm1.Create(Application); Form1.ShowModal;end;
[解决办法]
改不改写application.handle不是关键,关键是showmodal以后不需要free吗
[解决办法]
改不改写application.handle不是关键,关键是showmodal以后不需要free吗