把类放在DLL中?

6 delphi dll class delphi-xe

是否可以将一些类放入DLL中?

我正在处理的项目中有几个自定义类,并希望将它们放入DLL中,然后在需要时在主应用程序中访问,如果它们在DLL中,我可以在其他项目中重用这些类,如果我需要的话至.

我找到了这个链接:http://www.delphipages.com/forum/showthread.php? t = 84394讨论了访问DLL中的类,它提到委托给类类型的属性,但我找不到任何进一步的信息这在Delphi的帮助或在线.

有什么理由我不应该把类放在DLL中,如果没有问题,那么在上面的链接示例中有更好的方法吗?

谢谢

Sir*_*ufo 14

无法从DLL获取类/实例.您可以将接口移交给类,而不是类.下面是一个简单的例子

// 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.
Run Code Online (Sandbox Code Playgroud)

简单的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.
Run Code Online (Sandbox Code Playgroud)

现在简单的主程序

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;
Run Code Online (Sandbox Code Playgroud)


Ken*_*ite 5

为此目的使用运行时包; 这正是他们首先设计的.它们会自动加载(或者可以手动加载),并自动设置相同内存管理器的共享,以便您可以在它们之间自由使用类和类型.

你最好不要使用软件包(由于这个原因,这正是IDE为其大部分功能所做的工作).

  • 你真的无法导入Delphi中DLL中定义的类.除了可以克服的所有障碍之外,没有语言支持.你不能在课堂上使用`external`.对于Delphi类,没有`__declspec(dllimport)`. (2认同)