W
wind_2005
Unregistered / Unconfirmed
GUEST, unregistred user!
大家好!
我在看了《Delphi面向对象编程思想》(刘艺)第八章后,根据“8.2.3 界面和业务的物理分离”中所讲,自已创建了一个类似程序,内容非常简单,但老是出错。现将代码贴出来,烦劳各位能帮忙看看,先在这里谢过了!
=====================
DLL部分(包括两个单元,单元VirtualClass,是一个抽像类,很简单只有一个方法;
单元ImplClass,是上一抽象类的子类):
library objdllserver;
{ 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,
Classes,
VirtualClass in 'VirtualClass.pas',
ImplClass in 'ImplClass.pas';
{$R *.res}
function TImpl:TVirtualClassClass;
begin
Result := TImplClass;
end;
exports TImpl;
begin
end.
----------------------------
unit VirtualClass;
interface
uses classes;
Type
TVirtualClass= class(TObject)
public
function getName:string;virtual;abstract;
end;
TVirtualClassClass = class of TVirtualClass;
implementation
{ TVirtualClass }
end.
-----------------
unit ImplClass;
interface
Uses classes, VirtualClass;
type
TImplClass = class(TVirtualClass)
public
function getName:string;Override;
end;
implementation
function TImplClass.getName: string;
begin
Result := 'ABC';
end;
end.
=========================
界面部分(包括两个部分:界面单元及抽象类单元,与DLL中VirtualClass单元为同一单元):
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, VirtualClass;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
aImpl: TVirtualClass;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TImpl:TVirtualClassClass;stdcall;external 'objdllserver.dll';
procedure TForm1.Button1Click(Sender: TObject);
var
s: string;
begin
aImpl := TImpl.Create;
s := aImpl.getName;
ShowMessage(s);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
aImpl.Free;
end;
end.
------------------
以下单元与DLL中VirtualClass单元为同一单元
----------------
unit VirtualClass;
interface
uses classes;
Type
TVirtualClass= class(TObject)
public
function getName:string;virtual;abstract;
end;
TVirtualClassClass = class of TVirtualClass;
implementation
{ TVirtualClass }
end.
我在看了《Delphi面向对象编程思想》(刘艺)第八章后,根据“8.2.3 界面和业务的物理分离”中所讲,自已创建了一个类似程序,内容非常简单,但老是出错。现将代码贴出来,烦劳各位能帮忙看看,先在这里谢过了!
=====================
DLL部分(包括两个单元,单元VirtualClass,是一个抽像类,很简单只有一个方法;
单元ImplClass,是上一抽象类的子类):
library objdllserver;
{ 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,
Classes,
VirtualClass in 'VirtualClass.pas',
ImplClass in 'ImplClass.pas';
{$R *.res}
function TImpl:TVirtualClassClass;
begin
Result := TImplClass;
end;
exports TImpl;
begin
end.
----------------------------
unit VirtualClass;
interface
uses classes;
Type
TVirtualClass= class(TObject)
public
function getName:string;virtual;abstract;
end;
TVirtualClassClass = class of TVirtualClass;
implementation
{ TVirtualClass }
end.
-----------------
unit ImplClass;
interface
Uses classes, VirtualClass;
type
TImplClass = class(TVirtualClass)
public
function getName:string;Override;
end;
implementation
function TImplClass.getName: string;
begin
Result := 'ABC';
end;
end.
=========================
界面部分(包括两个部分:界面单元及抽象类单元,与DLL中VirtualClass单元为同一单元):
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, VirtualClass;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
aImpl: TVirtualClass;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TImpl:TVirtualClassClass;stdcall;external 'objdllserver.dll';
procedure TForm1.Button1Click(Sender: TObject);
var
s: string;
begin
aImpl := TImpl.Create;
s := aImpl.getName;
ShowMessage(s);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
aImpl.Free;
end;
end.
------------------
以下单元与DLL中VirtualClass单元为同一单元
----------------
unit VirtualClass;
interface
uses classes;
Type
TVirtualClass= class(TObject)
public
function getName:string;virtual;abstract;
end;
TVirtualClassClass = class of TVirtualClass;
implementation
{ TVirtualClass }
end.