D2009 UP3和D2010 V3449 都没解决DLL的问题!(值得大家研究)
2009-06-15 10:36
399 查看
DLL的DPR代码如下:
library DllPro;
{ 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
SimpleShareMem,
SysUtils,
Forms,
Windows,
Classes,
uIntf in 'uIntf.pas',
uImp in 'uImp.pas',
Unit2 in 'Unit2.pas' {Form2};
{$R *.res}
var
DLLApp: TApplication;
function InitDLL(App: TApplication): Intf_DLLTest;
begin
//问题出在这句上,
//如果没有这句程序退出时就不会出错。
//但是这句在DLL里一般都是要这样做的。否则DLL里的窗口显示的图标都是不正确的。
//这样的代码在Delphi 2007以前是不会有问题的。
Application := App;
Obj_DLLTest := TImpl_DLLTest.Create(nil);
Intf_DLLTest1 := Obj_DLLTest as Intf_DLLTest;
Result := Intf_DLLTest1;
end;
procedure UnInitDLL;
begin
FreeAndNil(Obj_DLLTest);
end;
procedure ExitDLL(Reason: Integer);
begin
if Reason = DLL_PROCESS_DETACH then
begin
Application := DLLApp;
end;
end;
exports
InitDLL,
UnInitDLL;
begin
DLLApp := Application;
DLLProc := @ExitDLL;
end.
调用DLL的主程序的主窗口的代码如下:
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
uIntf;
var
InitDLL: function (App: TApplication): Intf_DLLTest;
UnInitDLL: procedure;
DllHandle: THandle;
procedure TForm1.Button1Click(Sender: TObject);
begin
DllHandle := LoadLibrary('DllPro.dll');
if DllHandle > 0 then
begin
InitDLL := GetProcAddress(DllHandle, 'InitDLL');
if Assigned(InitDLL) then
begin
Intf_DLLTest1 := InitDLL(Application);
if Intf_DLLTest1 <> nil then
begin
Intf_DLLTest1.ShowTestForm;
end;
end;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
if DllHandle > 0 then
begin
if Assigned(Intf_DLLTest1) then Intf_DLLTest1 := nil;
UnInitDLL := GetProcAddress(DllHandle, 'UnInitDLL');
if Assigned(UnInitDLL) then
begin
UnInitDLL;
end;
FreeLibrary(DllHandle);
end;
end;
end.
详细原代码请看这里:
http://files.cnblogs.com/AnyDelphi/Dll_Intf.rar
library DllPro;
{ 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
SimpleShareMem,
SysUtils,
Forms,
Windows,
Classes,
uIntf in 'uIntf.pas',
uImp in 'uImp.pas',
Unit2 in 'Unit2.pas' {Form2};
{$R *.res}
var
DLLApp: TApplication;
function InitDLL(App: TApplication): Intf_DLLTest;
begin
//问题出在这句上,
//如果没有这句程序退出时就不会出错。
//但是这句在DLL里一般都是要这样做的。否则DLL里的窗口显示的图标都是不正确的。
//这样的代码在Delphi 2007以前是不会有问题的。
Application := App;
Obj_DLLTest := TImpl_DLLTest.Create(nil);
Intf_DLLTest1 := Obj_DLLTest as Intf_DLLTest;
Result := Intf_DLLTest1;
end;
procedure UnInitDLL;
begin
FreeAndNil(Obj_DLLTest);
end;
procedure ExitDLL(Reason: Integer);
begin
if Reason = DLL_PROCESS_DETACH then
begin
Application := DLLApp;
end;
end;
exports
InitDLL,
UnInitDLL;
begin
DLLApp := Application;
DLLProc := @ExitDLL;
end.
调用DLL的主程序的主窗口的代码如下:
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
uIntf;
var
InitDLL: function (App: TApplication): Intf_DLLTest;
UnInitDLL: procedure;
DllHandle: THandle;
procedure TForm1.Button1Click(Sender: TObject);
begin
DllHandle := LoadLibrary('DllPro.dll');
if DllHandle > 0 then
begin
InitDLL := GetProcAddress(DllHandle, 'InitDLL');
if Assigned(InitDLL) then
begin
Intf_DLLTest1 := InitDLL(Application);
if Intf_DLLTest1 <> nil then
begin
Intf_DLLTest1.ShowTestForm;
end;
end;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
if DllHandle > 0 then
begin
if Assigned(Intf_DLLTest1) then Intf_DLLTest1 := nil;
UnInitDLL := GetProcAddress(DllHandle, 'UnInitDLL');
if Assigned(UnInitDLL) then
begin
UnInitDLL;
end;
FreeLibrary(DllHandle);
end;
end;
end.
详细原代码请看这里:
http://files.cnblogs.com/AnyDelphi/Dll_Intf.rar
相关文章推荐
- vs2010 office组件dll右键属性找不到”嵌入互操作类型“问题的解决
- VS2010(64位) DLL 无法正常加载问题解决方案
- 解决 Delphi XE (2010,2009) 里的UTF8乱码问题!
- 解决 Delphi XE (2010,2009) 里的UTF8乱码问题!
- 解决Delphi XE(2010,2009)里的UTF8乱码问题
- 一个值得大家来考虑的DLL问题
- 一个值得大家来考虑的DLL问题
- Visual Studio 2010 .NET 4.0 WinForm 下无法引用 System.Web.dll 问题的解决方法
- delphi中一个值得大家来考虑的DLL问题
- sharepoint 2010 中添加webpart时引用dll一些问题及解决方法。
- 解决Tomcat: Can't load IA 32-bit .dll on a AMD 64-bit platform问题
- OpenCv中没有找到tbb_debug.dll的解决办法(问题总结)
- java中调用本地动态链接库(*.DLL)的两种方式详解和not found library、打包成jar,war包dll无法加载等等问题解决办法
- 解决vs2010添加WeifenLuo.WinFormsUI.Docking.DockPanel.dll文件报错问题
- office word 2010解决参考文献问题
- 无法启动此程序,因为计算机中丢失suite objects.dll。尝试重新安装该程序以解决此问题
- 还是关于无法加载DLL(OCI.DLL)问题解决办法!
- 如何解决Office 2010中Excel文档双击打不开问题?
- 使用WinDbg调试Silverlight中遇到“Failed to find runtime DLL (clr.dll), 0x80004005”问题的解决方法之一
- 解决 SharePoint 2010 外部列表时间字段出现时差的问题