Delphi接口和DLL的

问题描述:

我试图找出将对象从主应用程序传递到DLL(用于插件系统)的安全方式。
这个想法是使用主应用程序的TZConnection(来自Zeos Lib的数据库访问)在dll的。

I'm trying to figure out a safe way of passing objects from a main app to a DLL (for a plugin system). The idea is to use the main app's TZConnection (database access from Zeos Lib) in the dll's.

我宁愿不使用运行时软件包和DLL必须要走(我不需要BPL需要每次重新编译,不知道如何使用COM)。

I'd rather not use Runtime Packages, and DLL must be the way to go (I don't need BPL's need to recompile each time, and have no idea of how to use COM).

我被告知可以使用Interfaces来实现。

I've been told it's possible to do it with Interfaces.

我以前从来没有使用过它,但是一直在搞乱,并设法做到这一点...但是,我不知道我是否正确(如在,安全)。

I've never used them before, but been messing around with it, and managed to do it... But, I don't know if I did it right (as in, safe).

这是我的界面单元。

unit PluginIntf;

interface

uses
    ZConnection, ZDataSet;

type
    IQueryResult = interface   ['{743AB77E-7897-403E-A0D9-4D02748E565D}']
      function GetRecordCount: Integer;
      function GetDataSet: TZQuery;
    end;

    IPluginHost = interface   ['{A5A416B4-437E-4A1E-B228-0F94D54840B0}']
      function RunSql(const SQLString:WideString): IQueryResult;
    end;

    IPlugin = interface   ['{8D9591C3-5949-4F0A-883E-6ABD02597846}']
      function GetCaption: WideString;
    end;

implementation

end.

在IQueryResult中,我传递一个TZQuery。它的工作,但...是安全吗?
有没有其他方法将它包装在界面?

In IQueryResult, I'm passing a TZQuery. It works, but... Is it safe? Is there any other way to wrap it in the Interface?

谢谢
Nuno Picado

Thank you Nuno Picado

TZQuery 是一个类。因此,除非使用运行时程序包,否则通过模块边界是不安全的。使用带有DLL的类,实际上有两个单独的类型,每个模块中有一个。

TZQuery is a class. Therefore it is not safe to pass one across the module boundary unless you use runtime packages. Use a class with DLLs and you actually have two separate types, one in each module.

你是正确的接口是安全的,但你需要限制自己的DLL互操作安全类型。这是简单的类型,记录,指向数组,接口或这些类型的组合的指针。

You are correct that interfaces are safe for this but you need to restrict yourself to DLL interop safe types. That is simple types, records, pointers to arrays, interfaces, or combinations of these types.

您需要使用公开其功能的界面来封装 TZQuery

You need to wrap TZQuery with an interface that exposes it's functionality.