将类放在 DLL 中? [英] Putting classes in a DLL?
问题描述
是否可以将某些类放入 DLL 中?
Is it possible to put some classes into a DLL?
我在我正在处理的项目中有几个自定义类,并希望将它们放入一个 DLL 中,然后在需要时在主应用程序中访问,此外,如果它们在 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.
我找到了这个链接:http://www.delphipages.com/forum/showthread.php?t=84394 讨论了访问 DLL 中的类,并提到委托给类类型的属性,但我在 Delphi 帮助或在线中找不到任何关于此的更多信息.
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.
是否有任何理由我不应该将类放入 DLL 中,如果可以,那么在上面链接的示例中是否有更好的方法?
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?
谢谢
推荐答案
无法从 DLL 中获取类/实例.您可以将接口交给类,而不是类.下面是一个简单的例子
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.
简单的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.
现在是简单的主程序
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屋!