COM对象和不同版本的DLL

时间:2011-10-25 17:07:28

标签: delphi dll

我是DLL对象的新手,我到处搜索,无法找到正确的答案。 我对Microsoft RMS做了一些小插件,它会自动从我的dll调用函数Process,IDispach参数传递当前会话详细信息。

我正在使用QSRules.dll的接口(组件>导入>组件>类型库...添加到项目)。 它创建包含所有引用等的TLB文件。

procedure TRefreshScreenRefreshScreen.Process(const Session: IDispatch);
begin

  CodeSite.Send( csmLevel1, '(Session as SessionClass).Cashier.Name', (Session as SessionClass).Cashier.Name );
  CodeSite.Send( csmLevel1, '(Session as SessionClass).Cashier.Number', (Session as SessionClass).Cashier.Number );

end;

与软件版本2.01完美配合,但在尝试在版本2.02上使用相同的功能时,它会因“不支持接口”而崩溃。 QSRules.dll已更新版本,所有类的GUID都不同。

我尝试了以下代码:

procedure TRefreshScreenRefreshScreen.Process(const Session: IDispatch);
begin

  if Supports(Session, QSRules_TLB_2_0_0_151.SessionClass) then
    Begin
      CodeSite.Send( csmLevel1, '(Session as SessionClass).Cashier.Name', (Session as QSRules_TLB_2_0_0_151.SessionClass).Cashier.Name );
      CodeSite.Send( csmLevel1, '(Session as SessionClass).Cashier.Number', (Session as QSRules_TLB_2_0_0_151.SessionClass).Cashier.Number );
    end else

  if Supports(Session, QSRules_TLB_2_0_0_105.SessionClass) then
    Begin
      CodeSite.Send( csmLevel1, '(Session as SessionClass).Cashier.Name', (Session as QSRules_TLB_2_0_0_105.SessionClass).Cashier.Name );
      CodeSite.Send( csmLevel1, '(Session as SessionClass).Cashier.Number', (Session as QSRules_TLB_2_0_0_151.SessionClass).Cashier.Number );
    end

end;

有4个或5个不同版本的dll都有不同的GUID,98%的代码在所有版本之间是相同的。 以这种方式做到这一点就是使代码倍增。

有什么方法可以缩短它吗?

我也试过

procedure TRefreshScreenRefreshScreen.Process(const Session: IDispatch);
var
 _Session: SessionClass;
begin

  if Supports(Session, QSRules_TLB_2_0_0_151.SessionClass) then
    _Session = (Session as QSRules_TLB_2_0_0_151.SessionClass)

  else if Supports(Session, QSRules_TLB_2_0_0_105.SessionClass) then
      _Session = (Session as QSRules_TLB_2_0_0_105.SessionClass);

  with _Session do
  Begin
      CodeSite.Send( csmLevel1, '_Session.Cashier.Name', Cashier.Name );
      CodeSite.Send( csmLevel1, '_Session..Cashier.Number', Cashier.Number );
  End;

end;

但这不起作用,因为变量类型只能从单位分配。

任何帮助表示赞赏!

3 个答案:

答案 0 :(得分:0)

你说接口在不同版本中有不同的guid。只要较新的接口来自较旧的接口,那就完全没问题了。实际情况是这样的吗?如果他们这样做,那么您可以通过将Session对象转换为实际定义Cashier成员的任何接口来简化代码。您不需要将其强制转换为每个单独的接口类型,除非接口不是相互派生的。你能展示实际的界面声明吗?

答案 1 :(得分:0)

v2.0.0.105的收银员声明

_Cashier = interface(IDispatch)
    ['{AA84B4FB-AA41-4423-A763-59D0723ED52B}']
    function Get_Session: _SessionClass; safecall;
    function Get_CashDrawer: _CashDrawer; safecall;
    function Get_OverShortLimitType: overshortlimitEnum; safecall;
    function Get_MaxOverShortAmount: Currency; safecall;
    function Get_MaxOverShortPercent: Double; safecall;
    function Get_SecurityLevel: Smallint; safecall;
    function Get_HasPrivilege(var CashierPrivilege: cashierprivilegesEnum): WordBool; safecall;
    function Get_FailedLogOnAttempts: Integer; safecall;
    function Get_EmailAddress: WideString; safecall;
    function Get_Messages: _CashierMessages; safecall;
    function Get_UnreadMessageCount: Integer; safecall;
    function Get_Name: WideString; safecall;
    function Get_FirstName: WideString; safecall;
    function Get_LastName: WideString; safecall;
    function Get_ReturnLimit: Currency; safecall;
    function Get_FloorLimit: Currency; safecall;
    function Get_ID: Integer; safecall;
    function Get_CashDrawerNumber: Smallint; safecall;
    function Get_Loaded: WordBool; safecall;
    function Get_Number: WideString; safecall;
    property Session: _SessionClass read Get_Session;
    property CashDrawer: _CashDrawer read Get_CashDrawer;
    property OverShortLimitType: overshortlimitEnum read Get_OverShortLimitType;
    property MaxOverShortAmount: Currency read Get_MaxOverShortAmount;
    property MaxOverShortPercent: Double read Get_MaxOverShortPercent;
    property SecurityLevel: Smallint read Get_SecurityLevel;
    property HasPrivilege[var CashierPrivilege: cashierprivilegesEnum]: WordBool read Get_HasPrivilege;
    property FailedLogOnAttempts: Integer read Get_FailedLogOnAttempts;
    property EmailAddress: WideString read Get_EmailAddress;
    property Messages: _CashierMessages read Get_Messages;
    property UnreadMessageCount: Integer read Get_UnreadMessageCount;
    property Name: WideString read Get_Name;
    property FirstName: WideString read Get_FirstName;
    property LastName: WideString read Get_LastName;
    property ReturnLimit: Currency read Get_ReturnLimit;
    property FloorLimit: Currency read Get_FloorLimit;
    property ID: Integer read Get_ID;
    property CashDrawerNumber: Smallint read Get_CashDrawerNumber;
    property Loaded: WordBool read Get_Loaded;
    property Number: WideString read Get_Number;
  end;

v2.0.0.151的收银员声明

_Cashier = interface(IDispatch)
    ['{39B2C128-00F1-4834-B1A4-05197C708BD9}']
    function Get_Session: _SessionClass; safecall;
    function Get_CashDrawer: _CashDrawer; safecall;
    function Get_OverShortLimitType: overshortlimitEnum; safecall;
    function Get_MaxOverShortAmount: Currency; safecall;
    function Get_MaxOverShortPercent: Double; safecall;
    function Get_SecurityLevel: Smallint; safecall;
    function Get_HasPrivilege(var CashierPrivilege: cashierprivilegesEnum): WordBool; safecall;
    function Get_FailedLogOnAttempts: Integer; safecall;
    function Get_EmailAddress: WideString; safecall;
    function Get_Messages: _CashierMessages; safecall;
    function Get_UnreadMessageCount: Integer; safecall;
    function Get_Name: WideString; safecall;
    function Get_FirstName: WideString; safecall;
    function Get_LastName: WideString; safecall;
    function Get_ReturnLimit: Currency; safecall;
    function Get_FloorLimit: Currency; safecall;
    function Get_ID: Integer; safecall;
    function Get_CashDrawerNumber: Smallint; safecall;
    function Get_Loaded: WordBool; safecall;
    function Get_Number: WideString; safecall;
    function Get_PasswordAge: Integer; safecall;
    function Get_ReminderPeriod: Integer; safecall;
    function Get_PasswordResetFlag: WordBool; safecall;
    function Get_IsPasswordChanged: WordBool; safecall;
    procedure Set_IsPasswordChanged(var Param1: WordBool); safecall;
    function Get_TimecardID: Integer; safecall;
    procedure Set_TimecardID(var Param1: Integer); safecall;
    function ValidatePassword(var Password: WideString): WordBool; safecall;
    function IsPwdDuplicated(var CashierNumber: Integer; var Password: WideString): WordBool; safecall;
    property Session: _SessionClass read Get_Session;
    property CashDrawer: _CashDrawer read Get_CashDrawer;
    property OverShortLimitType: overshortlimitEnum read Get_OverShortLimitType;
    property MaxOverShortAmount: Currency read Get_MaxOverShortAmount;
    property MaxOverShortPercent: Double read Get_MaxOverShortPercent;
    property SecurityLevel: Smallint read Get_SecurityLevel;
    property HasPrivilege[var CashierPrivilege: cashierprivilegesEnum]: WordBool read Get_HasPrivilege;
    property FailedLogOnAttempts: Integer read Get_FailedLogOnAttempts;
    property EmailAddress: WideString read Get_EmailAddress;
    property Messages: _CashierMessages read Get_Messages;
    property UnreadMessageCount: Integer read Get_UnreadMessageCount;
    property Name: WideString read Get_Name;
    property FirstName: WideString read Get_FirstName;
    property LastName: WideString read Get_LastName;
    property ReturnLimit: Currency read Get_ReturnLimit;
    property FloorLimit: Currency read Get_FloorLimit;
    property ID: Integer read Get_ID;
    property CashDrawerNumber: Smallint read Get_CashDrawerNumber;
    property Loaded: WordBool read Get_Loaded;
    property Number: WideString read Get_Number;
    property PasswordAge: Integer read Get_PasswordAge;
    property ReminderPeriod: Integer read Get_ReminderPeriod;
    property PasswordResetFlag: WordBool read Get_PasswordResetFlag;
    property IsPasswordChanged: WordBool read Get_IsPasswordChanged write Set_IsPasswordChanged;
    property TimecardID: Integer read Get_TimecardID write Set_TimecardID;
  end;

正如您所看到的,在更高版本中添加的内容很少,但毫无疑问,在调用它们时,我需要检查软件版本。收银员只有25-30种类型中的一种,所以如果我必须为所有版本编写相同的基本实现......大任务,以及后期修改的可怕代码。

答案 2 :(得分:0)

终于把它分类了!只需分享答案,以防其他人寻找答案。

成功的关键是“后期绑定”,这意味着你不使用接口。

procedure TRefreshScreenRefreshScreen.Process(const Session: IDispatch);
var
 _Session: Variant;
begin

  _Session := Session;

  CodeSite.Send( csmLevel1, '_Session.Cashier.Name', _Session.Cashier.Name );
  CodeSite.Send( csmLevel1, '_Session.Cashier.Number', _Session.Cashier.Number );

end;

对于变量变量,函数不是由编译器检查,而是在运行时检查,因此您必须确保拼写正确,因为intellisense没有检查它。

像梦一样工作!

无论如何,谢谢大家!