Is it safe to use interfaces from dll

2019-06-24 06:14发布

When I want to export a class within a DLL, is it the right approach to derive it from an interface and return that interface by an exported function?

//exported dll function, which is used in the exe.
function MyClass_Create: IMyClass;
begin
  result := TMyClass.Create;
end;

What about the memory management? Can I than pass in/out different interfaces and strings without worries and crashes?

IMyClass = interface
  procedure SetString(aMsg: string);
  function GetString: string;

  procedure SetClass(aClass: ITestClass);
  function GetClass: ITestClass;
end;

2条回答
smile是对你的礼貌
2楼-- · 2019-06-24 06:41

Using interfaces like this will ensure that the object implementing the interface will be created and freed on the same heap.

However, this will not solve the problem of dynamic string types being allocated and deallocated on different heaps. There are many possible solutions to this, but in my view the best approach is to use WideString across the module boundary.

The WideString type is a wrapper around the COM BSTR and is allocated on the shared COM heap. You only need to use WideString for the interface. The internals of the implementing classes can use native Delphi strings.

Just as strings present problems so do dynamic arrays. Attempting to pas dynamic arrays across module boundaries is not safe. There is no solution analagous to WideString that is as convenient. You can use variant arrays but that's pretty clunky in comparison to WideString.

查看更多
\"骚年 ilove
3楼-- · 2019-06-24 06:48

Interface references are orthogonal to memory management. Usually you export a function that returns interface reference from dll, and don't care about memory management. With reference counting interfaces you can be sure that object instance that implements interface will be freed in dll too.

Strings are different. It does not matter whether you export interface or export flat functions - the same restrictions applies.

BTW your question title is incorrect, there are no 'interface instances' in Delphi.

查看更多
登录 后发表回答