是否可以将一些类放入DLL中?
我正在处理的项目中有几个自定义类,并希望将它们放入DLL中,然后在需要时在主应用程序中访问,如果它们在DLL中,我可以在其他项目中重用这些类,如果我需要的话至.
我发现这个链接:http://www.delphipages.com/forum/showthread.php?t=84394讨论了访问DLL中的类,它提到委托给类类型的属性,但我在Delphi帮助或在线中找不到任何关于此的更多信息.
有什么理由我不应该把类放在DLL中,如果没有问题,那么在上面的链接示例中有更好的方法吗?
谢谢
解决方法
无法从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.
简单的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;