首页 > 解决方案 > 如何将 popupparent 属性传递给 DLL

问题描述

我将 popupparent 属性从程序传递给 DLL。

当调用 DLL 两次时,程序会引发访问冲突。

这是我的dll代码:

library mydll;

uses
  System.SysUtils,
  System.Classes,
  Vcl.Forms,
  umydllform in 'umydllform.pas' {FormDLL};

{$R *.res}

procedure myfunction(Parent: TCustomForm); stdcall;
var
  A: TFormDLL;
begin
  A := TFormDLL.Create(nil);
  A.PopupMode := TPopupMode.pmAuto;
  A.PopupParent := Parent;
  A.ShowModal;
  A.PopupParent := nil;
  A.Free;
end;

exports
  myfunction;

begin

end.

这是主要形式:

unit umainform;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm37 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  end;

var
  Form37: TForm37;

implementation

{$R *.dfm}

procedure TForm37.Button1Click(Sender: TObject);
type
  Tmyfunction = procedure(AOwner: TCustomForm); stdcall;
var
  H: HMODULE;
  F: Tmyfunction;
begin
  H := LoadLibrary(PChar('mydll.dll'));
  @F := GetProcAddress(H, 'myfunction');
  F(Application.MainForm);
  FreeLibrary(H);
end;

end.

程序第一次运行正常。但是第二次执行 dll 时会发生异常。我需要分配弹出父属性,这样表单就不会被遗忘。

当不使用 FreeLibrary 时,程序可以正常运行两次或更多次。

此致,

标签: delphidelphi-xe

解决方案


推荐阅读