D2009 UP3和D2010 V3449 都没解决DLL的问题!(值得大家研究)
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.
详细原代码请看这里:
https://files.cnblogs.com/AnyDelphi/Dll_Intf.rar