Why do I get the error Missing implementation of interface method in Delphi XE2?

落花浮王杯 提交于 2021-01-28 19:24:24

问题


Consider the following code:

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;

This compiles fine in Delphi 2007 and XE but Delphi XE2 gives me the error: "[DCC Error] u_ContextMenuHandler.pas(16): E2291 Missing implementation of interface method IContextMenu.GetCommandString"

This has me baffled. I checked the interface declaration and my GetCommandString function has the exactly the same declaration as the interface's. Any hints?


回答1:


'_idCmd' is declared UINT_PTR (which is 8 bytes when targeting 64-bit) in XE2.




回答2:


The correct declaration of GetCommandString is:

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

Make sure that you check for the presence of GCS_UNICODE in uFlags before writing to pszName. That test determines whether or not you should return a Unicode or ANSI string. This nuance is described in the documentation.



来源:https://stackoverflow.com/questions/10373636/why-do-i-get-the-error-missing-implementation-of-interface-method-in-delphi-xe2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!