delphi – 将类放在DLL中?

前端之家收集整理的这篇文章主要介绍了delphi – 将类放在DLL中?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
是否可以将一些类放入DLL中?

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

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

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

谢谢

解决方法

无法从DLL获取类/实例.
您可以将接口移交给类,而不是类.
下面是一个简单的例子
  1. // The Interface-Deklaration for Main and DLL
  2. unit StringFunctions_IntfU;
  3.  
  4. interface
  5.  
  6. type
  7. IStringFunctions = interface
  8. ['{240B567B-E619-48E4-8CDA-F6A722F44A71}']
  9. function CopyStr( const AStr : WideString; Index,Count : Integer ) : WideString;
  10. end;
  11.  
  12. implementation
  13.  
  14. end.

简单的DLL

  1. library StringFunctions;
  2.  
  3. uses
  4. StringFunctions_IntfU; // use Interface-Deklaration
  5.  
  6. {$R *.res}
  7.  
  8. type
  9. TStringFunctions = class( TInterfacedObject,IStringFunctions )
  10. protected
  11. function CopyStr( const AStr : WideString; Index : Integer; Count : Integer ) : WideString;
  12. end;
  13.  
  14. { TStringFunctions }
  15.  
  16. function TStringFunctions.CopyStr( const AStr : WideString; Index,Count : Integer ) : WideString;
  17. begin
  18. Result := Copy( AStr,Index,Count );
  19. end;
  20.  
  21. function GetStringFunctions : IStringFunctions; stdcall; export;
  22. begin
  23. Result := TStringFunctions.Create;
  24. end;
  25.  
  26. exports
  27. GetStringFunctions;
  28.  
  29. begin
  30. end.

现在简单的主程序

  1. uses
  2. StringFunctions_IntfU; // use Interface-Deklaration
  3.  
  4. // Static link to external function
  5. function GetStringFunctions : IStringFunctions; stdcall; external 'StringFunctions.dll' name 'GetStringFunctions';
  6.  
  7. procedure TMainView.Button1Click( Sender : TObject );
  8. begin
  9. Label1.Caption := GetStringFunctions.CopyStr( Edit1.Text,1,5 );
  10. end;

猜你在找的Delphi相关文章