【问题标题】:Why do I get the error Missing implementation of interface method in Delphi XE2?为什么我在 Delphi XE2 中收到错误缺少接口方法的实现?
【发布时间】:2012-04-29 16:06:38
【问题描述】:

考虑以下代码:

uses
  {... }
  ComObj,
  ShlObj;

type
  TContextMenu = class(TComObject, IShellExtInit, IContextMenu)
  private
  {(*}
  const
    GUID: TGUID = '{99D8B139-0855-4C5D-95E7-BC8EC6254B3D}';
  {*)}
  private
    FCmdCount: LongWord;
    FDm: Tdm_ContextMenu;
  protected
    function IShellExtInit.Initialize = IShellExtInit_Initialize;
    function IShellExtInit_Initialize(_pidlFolder: PItemIDList; _lpdobj: IDataObject;
      _HKeyProgID: HKEY): HResult; stdcall;
    function QueryContextMenu(_Menu: HMENU; _indexMenu, _idCmdFirst, _idCmdLast,
      _UFlags: UINT): HResult; stdcall;
    function InvokeCommand(var _ici: TCMInvokeCommandInfo): HResult; stdcall;
    function GetCommandString(_idCmd, _uType: UINT; _pwReserved: PUINT;
      _PszName: LPSTR; _cchMax: UINT): HResult; stdcall;
  public
    procedure Initialize; override;
    destructor Destroy; override;
  end;

这在 Delphi 2007 和 XE 中编译得很好,但 Delphi XE2 给了我错误: "[DCC 错误] u_ContextMenuHandler.pas(16): E2291 缺少接口方法 IContextMenu.GetCommandString 的实现"

这让我很困惑。我检查了接口声明,我的 GetCommandString 函数与接口的声明完全相同。有什么提示吗?

【问题讨论】:

    标签: delphi delphi-xe2


    【解决方案1】:

    '_idCmd' 在 XE2 中声明为 UINT_PTR(针对 64 位时为 8 个字节)。

    【讨论】:

    • 当大卫恢复他的答案时将删除这个答案。
    • @David - 好吧,但你的还是更全面:)
    • 啊,你是对的!但它说“缺少实现”而不是“类型不匹配”。 :-(
    • 编译错误是正确的。确实缺少接口函数的实现。为了提供有效的实现,您需要匹配名称、参数列表、返回类型和调用约定。
    【解决方案2】:

    GetCommandString的正确声明是:

    function GetCommandString(idCmd: UINT_PTR; uFlags: UINT; pwReserved: PUINT;
      pszName: LPSTR; cchMax: UINT): HResult; stdcall;
    

    在写信给pszName 之前,请确保检查uFlags 中是否存在GCS_UNICODE。该测试确定您是否应该返回 Unicode 或 ANSI 字符串。 documentation 中描述了这种细微差别。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-12
      • 1970-01-01
      • 2020-11-13
      • 2017-12-30
      • 2023-03-19
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多