首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >COM对象和不同版本的DLL

COM对象和不同版本的DLL
EN

Stack Overflow用户
提问于 2011-10-26 01:07:28
回答 3查看 611关注 0票数 1

我对DLL对象非常陌生,我到处寻找都找不到正确的答案。我对Microsoft RMS做了一些附加操作,它会自动从我的动态链接库调用函数进程,并使用IDispach参数传递当前会话的详细信息。

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

代码语言:javascript
复制
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都不同。

我用code代码试过了:

代码语言:javascript
复制
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%的代码是相同的。这样做是不必要的代码倍增。

有什么办法可以缩短吗?

我也试过了

代码语言:javascript
复制
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;

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

感谢任何人的帮助!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-10-27 18:02:51

终于把它整理好了!只要分享答案,以防其他人在寻找它。

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

代码语言:javascript
复制
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不会检查它。

像做梦一样工作!

无论如何,谢谢你们所有人!

票数 0
EN

Stack Overflow用户

发布于 2011-10-26 08:42:45

您说接口在不同的版本中有不同的guids。只要新的接口是从旧的接口派生出来的,这就完全没问题了。真的是这样吗?如果是这样,那么您可以通过将Session对象转换为实际定义收银员成员的任何接口来简化代码。您不需要将其强制转换为每个单独的接口类型,除非这些接口不是彼此派生的。你能展示实际的接口声明吗?

票数 0
EN

Stack Overflow用户

发布于 2011-10-26 15:33:49

v2.0.0.105中的收银员声明

代码语言:javascript
复制
_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中的收银员声明

代码语言:javascript
复制
_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种类型中的一种,所以如果我必须为所有版本编写相同的基本实现...这是一项艰巨的任务,也是后期修改的可怕代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7893050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档