动态库源码如下:library ExportToZJJ;{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }uses
  SysUtils,
  Forms,
  Messages,
  Variants,
  Windows,
  Classes,
  USetting in 'USetting.pas',
  FormASProgress in 'FormASProgress.pas' {FrmASProgress},
  ArchiveWfFrm in 'ArchiveWfFrm.pas' {FrmArchiveWf},
  MainFrm in 'MainFrm.pas' {FrmMain},
  ArchiveYhFrm in 'ArchiveYhFrm.pas' {FrmArchiveYh};{$R *.res}
var
  DLLApp: TApplication;
  DLLScreen: TScreen;procedure InportAndExport(aApp: TApplication; Scr: TScreen); stdcall;
begin
  Application := aApp;
  Screen := Scr;
  try
    Application.CreateForm(TFrmMain, FrmMain);
    FrmMain.ShowModal;
    FreeAndNil(FrmMain);
  except
  end;
end;
function ReadDLLInfo(): Pchar; stdcall;
begin
  Result := MyCaption;
end;procedure DLLUnloadProc(dwReason: DWORD);
begin
  if dwReason = DLL_PROCESS_DETACH then begin
    Application := DLLApp; //恢复
    Screen := DLLScreen;
  end;
end;exports
  InportAndExport,
  ReadDLLInfo;begin
  DLLApp := Application; //保存 DLL 中初始的 Application 对象
  DLLScreen := Screen;
  DLLProc := @DLLUnloadProc; //保证 DLL 卸载时恢复原来的 Application
  DLLUnloadProc(DLL_PROCESS_DETACH);
end.现在的问题是我在动态库的窗体中加了一个popupmenu,但是只要涉及到popupmenu弹出的地方,一点就会提示地址错误,请问这是什么原因?有什么办法能解决吗

解决方案 »

  1.   

    CSDN还没解决这个问题,发的帖子不回一下看不到。。
      

  2.   

    uses  Menus
    ----------------------------------------------------------------
                 花自飘零水自流,一种相思,两处闲愁。
                   此情无计可消除,才下眉头,又上心头。
    ----------------------------------------------------------------
      

  3.   

    uses  Menus没用啊,我在窗体里就有uses Menus了,可是就是一用popupmenu就出错。
      

  4.   

    up一下,急等。。有哪个DX会啊,帮帮我。。感激不尽
      

  5.   

    我这里测试没有问题DLL代码:
    library Project2;uses
      SysUtils,
      Forms,
      Messages,
      Windows,
      Classes,
      Unit1 in 'Unit1.pas' {Form1};{$R *.res}
    var
      DLLApp: TApplication;
      DLLScreen: TScreen;procedure InportAndExport(aApp: TApplication; Scr: TScreen); stdcall;
    begin
      try
        Application.CreateForm(TForm1, Form1);
        Form1.ShowModal;
      Finally
        FreeAndNil(Form1);
      end;
    end;procedure DLLUnloadProc(dwReason: DWORD);
    begin
      if dwReason = DLL_PROCESS_DETACH then 
      begin
        Application := DLLApp; 
        Screen := DLLScreen;
      end;
    end;exports
      InportAndExport;begin
      DLLApp := Application; //保存 DLL 中初始的 Application 对象
      DLLScreen := Screen;
      DLLProc := @DLLUnloadProc; //保证 DLL 卸载时恢复原来的 Application
      DLLUnloadProc(DLL_PROCESS_DETACH);
    end.unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Menus;type
      TForm1 = class(TForm)
        PopupMenu1: TPopupMenu;
        a1: TMenuItem;
        b1: TMenuItem;
        procedure a1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.a1Click(Sender: TObject);
    begin
      Showmessage('a');
    end;end.调用程序代码:
    unit Unit2;interfaceuses
      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;implementation{$R *.dfm}
    procedure InportAndExport(aApp: TApplication; Scr: TScreen); stdcall;external 'project2.dll';procedure TForm1.Button1Click(Sender: TObject);
    begin
      InportAndExport(Application,Screen);
    end;end.----------------------------------------------------------------
                 花自飘零水自流,一种相思,两处闲愁。
                   此情无计可消除,才下眉头,又上心头。
    ----------------------------------------------------------------
      

  6.   

    procedure TForm1.a1Click(Sender: TObject);
    begin
      Showmessage('a');
    end;
    这个过程是popupmenu里的
    ----------------------------------------------------------------
                 花自飘零水自流,一种相思,两处闲愁。
                   此情无计可消除,才下眉头,又上心头。
    ----------------------------------------------------------------
      

  7.   

    谢谢 Shiyl(云淡风清(回到梦开始的地方)) ,我这里出现个很奇怪的事,我在公司的机器上运行会出错,在家里的机器上就没有问题,相同代码竟然会有这么大的差异。。
    还有一个问题:我的popupmenu的子项是动态创建的,会不会因为这个有问题? 望不吝赐教
      

  8.   

    动态创建也没有问题啊,dll里面的窗体代码如下:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Menus;type
      TForm1 = class(TForm)
        procedure a1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    procedure TForm1.a1Click(Sender: TObject);
    begin
      Showmessage(TMenuItem(Sender).Caption);
    end;procedure TForm1.FormCreate(Sender: TObject);
    var
      PopMenu: TPopupMenu;
      Item: TMenuItem;
    begin
      PopMenu := TPopUpMenu.Create(self);
      Item := TMenuItem.Create(self);
      Item.Caption := 'a';
      Item.OnClick := a1Click;
      PopMenu.Items.Add(Item);
      Item := TMenuItem.Create(self);
      Item.Caption := 'b';
      Item.OnClick := a1Click;
      PopMenu.Items.Add(Item);
      Form1.PopupMenu := PopMenu;
    end;end.
    ----------------------------------------------------------------
                 花自飘零水自流,一种相思,两处闲愁。
                   此情无计可消除,才下眉头,又上心头。
    ----------------------------------------------------------------
      

  9.   

    很奇怪的问题没解决。。算了。。谢谢Shiyl(云淡风清(回到梦开始的地方)) 的大力帮助,先结贴给分了。。