将类放在DLL中? [英] Putting classes in a DLL?

查看:126
本文介绍了将类放在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆