将类放在DLL中? [英] Putting classes in a DLL?
问题描述
我在一个项目中有几个自定义类,我想要把它们放在一个DLL,然后在需要时在主应用程序中访问,加上如果他们在一个DLL中,我可以在其他项目中重用这些类,如果我需要。
我发现这个链接: http://www.delphipages.com/forum/showthread.php?t= 84394 讨论了访问DLL中的类,并提到委派给类类型属性,但是我在Delphi帮助或在线时找不到任何进一步的信息。
有没有什么理由我不应该把类放在一个DLL中,如果可以的话,那么在上面的链接的例子中有没有更好的方法?
谢谢
不可能从DLL获取类/实例。
而不是类可以将接口移交给类。
下面你会发现一个简单的例子
// Main和DLL的接口解除
单元StringFunctions_IntfU ;
接口
类型
IStringFunctions =接口
['{240B567B-E619-48E4-8CDA-F6A722F44A71}']
函数CopyStr (const AStr:WideString; Index,Count:Integer):WideString;
结束
执行
结束。
简单的DLL
library StringFunctions;
使用
StringFunctions_IntfU; //使用Interface-Deklaration
{$ R * .res}
类型
TStringFunctions =类(TInterfacedObject,IStringFunctions)
protected
函数CopyStr(const AStr:WideString; Index:Integer; Count:Integer):WideString;
结束
{TStringFunctions}
函数TStringFunctions.CopyStr(const AStr:WideString; Index,Count:Integer):WideString;
begin
结果:= Copy(AStr,Index,Count);
结束
函数GetStringFunctions:IStringFunctions;标准出口;
begin
结果:= TStringFunctions.Create;
结束
export
GetStringFunctions;
begin
end。
现在简单的主程序
使用
StringFunctions_IntfU; //使用Interface-Deklaration
//静态链接到外部函数
函数GetStringFunctions:IStringFunctions;标准external'StringFunctions.dll'name'GetStringFunctions';
程序TMainView.Button1Click(Sender:TObject);
begin
Label1.Caption:= GetStringFunctions.CopyStr(Edit1.Text,1,5);
结束
Is it possible to put some classes into a DLL?
I have several custom classes in a project I am working on and would like to have them put in a DLL and then accessed in the main application when needed, plus if they are in a DLL I can reuse these classes in other projects if I need to.
I found this link: http://www.delphipages.com/forum/showthread.php?t=84394 which discusses accessing classes in a DLL and it mentions delegating to a class-type property but I could not find any further information on this in the Delphi help or online.
Is there any reason I should not put classes in a DLL, and if it is ok is there a better way of doing it then in the example from the link above?
Thanks
It is not possible to get a Class/Instance from a DLL. Instead of the class you can hand over an interface to the class. Below you find a simple example
// The Interface-Deklaration for Main and DLL
unit StringFunctions_IntfU;
interface
type
IStringFunctions = interface
['{240B567B-E619-48E4-8CDA-F6A722F44A71}']
function CopyStr( const AStr : WideString; Index, Count : Integer ) : WideString;
end;
implementation
end.
The simple DLL
library StringFunctions;
uses
StringFunctions_IntfU; // use Interface-Deklaration
{$R *.res}
type
TStringFunctions = class( TInterfacedObject, IStringFunctions )
protected
function CopyStr( const AStr : WideString; Index : Integer; Count : Integer ) : WideString;
end;
{ TStringFunctions }
function TStringFunctions.CopyStr( const AStr : WideString; Index, Count : Integer ) : WideString;
begin
Result := Copy( AStr, Index, Count );
end;
function GetStringFunctions : IStringFunctions; stdcall; export;
begin
Result := TStringFunctions.Create;
end;
exports
GetStringFunctions;
begin
end.
And now the simple Main Program
uses
StringFunctions_IntfU; // use Interface-Deklaration
// Static link to external function
function GetStringFunctions : IStringFunctions; stdcall; external 'StringFunctions.dll' name 'GetStringFunctions';
procedure TMainView.Button1Click( Sender : TObject );
begin
Label1.Caption := GetStringFunctions.CopyStr( Edit1.Text, 1, 5 );
end;
这篇关于将类放在DLL中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!