动态库源码如下: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弹出的地方,一点就会提示地址错误,请问这是什么原因?有什么办法能解决吗
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弹出的地方,一点就会提示地址错误,请问这是什么原因?有什么办法能解决吗
----------------------------------------------------------------
花自飘零水自流,一种相思,两处闲愁。
此情无计可消除,才下眉头,又上心头。
----------------------------------------------------------------
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.----------------------------------------------------------------
花自飘零水自流,一种相思,两处闲愁。
此情无计可消除,才下眉头,又上心头。
----------------------------------------------------------------
begin
Showmessage('a');
end;
这个过程是popupmenu里的
----------------------------------------------------------------
花自飘零水自流,一种相思,两处闲愁。
此情无计可消除,才下眉头,又上心头。
----------------------------------------------------------------
还有一个问题:我的popupmenu的子项是动态创建的,会不会因为这个有问题? 望不吝赐教
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.
----------------------------------------------------------------
花自飘零水自流,一种相思,两处闲愁。
此情无计可消除,才下眉头,又上心头。
----------------------------------------------------------------