【问题标题】:How to identify a "default member" in a COM library?如何识别 COM 库中的“默认成员”?
【发布时间】:2016-03-17 01:51:12
【问题描述】:

我正在使用ITypeInfo API 将 COM 类型库“翻译”成我的应用程序使用的“声明”对象。

“声明”的特征之一是“属性”——我能够判断属性“声明”何时应该具有 {name}.VB_UserMemId = -4 属性(即,当它是 _NewEnum 不可浏览属性时;我'我还能够判断一个属性何时被隐藏。

我没有说的是,当一个成员是该类型的 默认成员。根据FUNCFLAG_FDEFAULTBIND(MSDN)的描述:

最能代表对象的函数。一个类型信息中只有一个函数可以有这个属性。

...这听起来很像我正在寻找的东西。然而,在我遍历少数类型库(包括 VBA 标准库和整个 Excel 对象模型)的所有成员中,none 完全打开了该标志。

我希望这段代码调用AddDefaultMemberAttribute 来处理Collection.Item 之类的事情,这是类型的默认成员 - 但没有骰子。我没有检查正确的标志/值吗?

var attributes = new Attributes();
if (((FUNCFLAGS)memberDescriptor.wFuncFlags) != 0)
{
    if (memberName == "_NewEnum" && ((FUNCFLAGS)memberDescriptor.wFuncFlags).HasFlag(FUNCFLAGS.FUNCFLAG_FNONBROWSABLE))
    {
        attributes.AddEnumeratorMemberAttribute(memberName);
    }
    else if (((FUNCFLAGS)memberDescriptor.wFuncFlags).HasFlag(FUNCFLAGS.FUNCFLAG_FHIDDEN))
    {
        attributes.AddHiddenMemberAttribute(memberName);
    }
    else if (((FUNCFLAGS)memberDescriptor.wFuncFlags).HasFlag(FUNCFLAGS.FUNCFLAG_FDEFAULTBIND))
    {
        // bug: none of the 30K+ reflected members have this flag on??
        attributes.AddDefaultMemberAttribute(memberName);
    }
}

【问题讨论】:

  • 默认属性是dispid为0的那个。
  • @HansPassant 这可能是memberDescriptor.memid 值吗?我与memid=0 接洽的所有成员都是名为_Default 的隐藏成员,它们返回VT_HRESULT、VT_PTR 或VT_DISPATCH ...我想我还不太接近Collection.Item 或 TextBox.Text...
  • 可能,返回类型听起来相当古怪。使用 Oleview.exe 来比较你得到的,使用 File + View TypeLib。它像你一样反编译类型库。

标签: c# com


【解决方案1】:

删除wFuncFlags != 0 的检查,您将跳过该检查的大量成员,其中很多是您真正感兴趣的。

现在检查memid == 0 (thanks @HansPassant)

else if (memberDescriptor.memid == 0)
{
    attributes.AddDefaultMemberAttribute(memberName);
    Debug.WriteLine("Default member found: {0}.{1} ({2} / {3})", moduleDeclaration.IdentifierName, memberName, memberDeclarationType, (VarEnum)memberDescriptor.elemdescFunc.tdesc.vt);
}

这个小Debug.WriteLine 指令产生这个输出(截断,以适应帖子字符限制):

Default member found: _ErrObject.Number (PropertyGet / VT_I4)
Default member found: _ErrObject.Number (PropertyLet / VT_VOID)
Default member found: _Collection.Item (Function / VT_VARIANT)
Default member found: Adjustments.Item (PropertyGet / VT_R4)
Default member found: Adjustments.Item (PropertyLet / VT_VOID)
Default member found: ColorFormat.RGB (PropertyGet / VT_USERDEFINED)
Default member found: ColorFormat.RGB (PropertyLet / VT_VOID)
Default member found: ShapeNodes.Item (Function / VT_PTR)
Default member found: DiagramNodes.Item (Function / VT_PTR)
Default member found: DiagramNodeChildren.Item (Function / VT_PTR)
Default member found: IWindows._Default (PropertyGet / VT_HRESULT)
Default member found: _Application._Default (PropertyGet / VT_BSTR)
Default member found: IRange._Default (PropertyGet / VT_HRESULT)
Default member found: IRange._Default (PropertyLet / VT_HRESULT)
Default member found: Sheets._Default (PropertyGet / VT_DISPATCH)
Default member found: IHPageBreaks._Default (PropertyGet / VT_HRESULT)
Default member found: IVPageBreaks._Default (PropertyGet / VT_HRESULT)
Default member found: IRecentFiles._Default (PropertyGet / VT_HRESULT)
Default member found: IStyle._Default (PropertyGet / VT_HRESULT)
Default member found: IStyles._Default (PropertyGet / VT_HRESULT)
Default member found: IBorders._Default (PropertyGet / VT_HRESULT)
Default member found: IAddIns._Default (PropertyGet / VT_HRESULT)
Default member found: IToolbars._Default (PropertyGet / VT_HRESULT)
Default member found: IToolbarButtons._Default (PropertyGet / VT_HRESULT)
Default member found: IAreas._Default (PropertyGet / VT_HRESULT)
Default member found: Workbooks._Default (PropertyGet / VT_PTR)
Default member found: IMenuBars._Default (PropertyGet / VT_HRESULT)
Default member found: IMenus._Default (PropertyGet / VT_HRESULT)
Default member found: IMenuItems._Default (PropertyGet / VT_HRESULT)
Default member found: ICharts._Default (PropertyGet / VT_HRESULT)
Default member found: IDrawingObjects._Default (PropertyGet / VT_HRESULT)
Default member found: IDrawingObjects._Default (PropertyLet / VT_HRESULT)
Default member found: IPivotCaches._Default (PropertyGet / VT_HRESULT)
Default member found: IPivotFormula._Default (PropertyGet / VT_HRESULT)
Default member found: IPivotFormula._Default (PropertyLet / VT_HRESULT)
Default member found: IPivotFormulas._Default (PropertyGet / VT_HRESULT)
Default member found: IPivotTable._Default (PropertyGet / VT_HRESULT)
Default member found: IPivotTable._Default (PropertyLet / VT_HRESULT)
Default member found: IPivotField._Default (PropertyGet / VT_HRESULT)
Default member found: IPivotField._Default (PropertyLet / VT_HRESULT)
Default member found: ICalculatedFields._Default (PropertyGet / VT_HRESULT)
Default member found: IPivotItem._Default (PropertyGet / VT_HRESULT)
Default member found: IPivotItem._Default (PropertyLet / VT_HRESULT)
Default member found: ICalculatedItems._Default (PropertyGet / VT_HRESULT)
Default member found: IDialogs._Default (PropertyGet / VT_HRESULT)
Default member found: ICheckBox._Default (PropertyGet / VT_HRESULT)
Default member found: ICheckBox._Default (PropertyLet / VT_HRESULT)
Default member found: ICheckBoxes._Default (PropertyGet / VT_HRESULT)
Default member found: ICheckBoxes._Default (PropertyLet / VT_HRESULT)
Default member found: IOptionButton._Default (PropertyGet / VT_HRESULT)
Default member found: IOptionButton._Default (PropertyLet / VT_HRESULT)
Default member found: IOptionButtons._Default (PropertyGet / VT_HRESULT)
Default member found: IOptionButtons._Default (PropertyLet / VT_HRESULT)
Default member found: IScrollBar._Default (PropertyGet / VT_HRESULT)
Default member found: IScrollBar._Default (PropertyLet / VT_HRESULT)
Default member found: IScrollBars._Default (PropertyGet / VT_HRESULT)
Default member found: IScrollBars._Default (PropertyLet / VT_HRESULT)
Default member found: IListBox._Default (PropertyGet / VT_HRESULT)
Default member found: IListBox._Default (PropertyLet / VT_HRESULT)
Default member found: IListBoxes._Default (PropertyGet / VT_HRESULT)
Default member found: IListBoxes._Default (PropertyLet / VT_HRESULT)
Default member found: IDropDown._Default (PropertyGet / VT_HRESULT)
Default member found: IDropDown._Default (PropertyLet / VT_HRESULT)
Default member found: IDropDowns._Default (PropertyGet / VT_HRESULT)
Default member found: IDropDowns._Default (PropertyLet / VT_HRESULT)
Default member found: ISpinner._Default (PropertyGet / VT_HRESULT)
Default member found: ISpinner._Default (PropertyLet / VT_HRESULT)
Default member found: ISpinners._Default (PropertyGet / VT_HRESULT)
Default member found: ISpinners._Default (PropertyLet / VT_HRESULT)
Default member found: IPanes._Default (PropertyGet / VT_HRESULT)
Default member found: IGroupObject._Default (PropertyGet / VT_HRESULT)
Default member found: IGroupObject._Default (PropertyLet / VT_HRESULT)
Default member found: IGroupObjects._Default (PropertyGet / VT_HRESULT)
Default member found: IGroupObjects._Default (PropertyLet / VT_HRESULT)
Default member found: IModules._Default (PropertyGet / VT_HRESULT)
Default member found: IDialogSheets._Default (PropertyGet / VT_HRESULT)
Default member found: IWorksheets._Default (PropertyGet / VT_HRESULT)
Default member found: INames._Default (Function / VT_HRESULT)
Default member found: IName._Default (PropertyGet / VT_HRESULT)
Default member found: IChartObjects._Default (Function / VT_HRESULT)
Default member found: ICustomViews._Default (PropertyGet / VT_HRESULT)
Default member found: IFormatConditions._Default (PropertyGet / VT_HRESULT)
Default member found: IComments._Default (PropertyGet / VT_HRESULT)
Default member found: IQueryTables._Default (PropertyGet / VT_HRESULT)
Default member found: IParameters._Default (PropertyGet / VT_HRESULT)
Default member found: IODBCErrors._Default (PropertyGet / VT_HRESULT)
Default member found: IHyperlinks._Default (PropertyGet / VT_HRESULT)
Default member found: IFilters._Default (PropertyGet / VT_HRESULT)
Default member found: IChartColorFormat._Default (PropertyGet / VT_HRESULT)
Default member found: IAxes._Default (Function / VT_HRESULT)
Default member found: IPoints._Default (Function / VT_HRESULT)
Default member found: ISeriesCollection._Default (Function / VT_HRESULT)
Default member found: IDataLabels._Default (Function / VT_HRESULT)
Default member found: ILegendEntries._Default (Function / VT_HRESULT)
Default member found: ITrendlines._Default (Function / VT_HRESULT)
Default member found: IShapes._Default (Function / VT_HRESULT)
Default member found: IShapeRange._Default (Function / VT_HRESULT)
Default member found: IGroupShapes._Default (Function / VT_HRESULT)
Default member found: IControlFormat._Default (PropertyGet / VT_HRESULT)
Default member found: IControlFormat._Default (PropertyLet / VT_HRESULT)
Default member found: IPublishObjects._Default (PropertyGet / VT_HRESULT)
Default member found: IOLEDBErrors._Default (PropertyGet / VT_HRESULT)
Default member found: IPhonetics._Default (PropertyGet / VT_HRESULT)
Default member found: CubeFields._Default (PropertyGet / VT_PTR)
Default member found: IUsedObjects._Default (PropertyGet / VT_HRESULT)
Default member found: ICustomProperties._Default (PropertyGet / VT_HRESULT)
Default member found: ICustomProperty._Default (PropertyGet / VT_HRESULT)
Default member found: ICalculatedMembers._Default (PropertyGet / VT_HRESULT)
Default member found: ICalculatedMember._Default (PropertyGet / VT_HRESULT)
Default member found: IWatches._Default (PropertyGet / VT_HRESULT)
Default member found: IErrors._Default (PropertyGet / VT_HRESULT)
Default member found: ISmartTagAction._Default (PropertyGet / VT_HRESULT)
Default member found: ISmartTagActions._Default (PropertyGet / VT_HRESULT)
Default member found: ISmartTag._Default (PropertyGet / VT_HRESULT)
Default member found: ISmartTags._Default (PropertyGet / VT_HRESULT)
Default member found: ISmartTagRecognizer._Default (PropertyGet / VT_HRESULT)
Default member found: ISmartTagRecognizers._Default (PropertyGet / VT_HRESULT)
Default member found: IPivotItemList._Default (PropertyGet / VT_HRESULT)
Default member found: IAllowEditRanges._Default (PropertyGet / VT_HRESULT)
Default member found: IUserAccessList._Default (PropertyGet / VT_HRESULT)
Default member found: IListObjects._Default (PropertyGet / VT_HRESULT)
Default member found: IListObject._Default (PropertyGet / VT_HRESULT)
Default member found: IListColumns._Default (PropertyGet / VT_HRESULT)
Default member found: IListColumn._Default (PropertyGet / VT_HRESULT)
Default member found: IListRows._Default (PropertyGet / VT_HRESULT)
Default member found: IXmlNamespace._Default (PropertyGet / VT_HRESULT)
Default member found: IXmlNamespaces._Default (PropertyGet / VT_HRESULT)
Default member found: IXmlDataBinding._Default (PropertyGet / VT_HRESULT)
Default member found: IXmlSchemas._Default (PropertyGet / VT_HRESULT)
Default member found: IXmlMap._Default (PropertyGet / VT_HRESULT)
Default member found: IXmlMaps._Default (PropertyGet / VT_HRESULT)
Default member found: IListDataFormat._Default (PropertyGet / VT_HRESULT)
Default member found: IXPath._Default (PropertyGet / VT_HRESULT)
Default member found: IPivotLineCells._Default (PropertyGet / VT_HRESULT)
Default member found: IPivotLines._Default (PropertyGet / VT_HRESULT)
Default member found: IPivotFilters._Default (PropertyGet / VT_HRESULT)
Default member found: IWorkbookConnection._Default (PropertyGet / VT_HRESULT)
Default member found: IWorkbookConnection._Default (PropertyLet / VT_HRESULT)
Default member found: IConnections._Default (PropertyGet / VT_HRESULT)
Default member found: ISheetViews._Default (PropertyGet / VT_HRESULT)
Default member found: IActions._Default (PropertyGet / VT_HRESULT)
Default member found: IColorScaleCriteria._Default (PropertyGet / VT_HRESULT)
Default member found: IIconCriteria._Default (PropertyGet / VT_HRESULT)
Default member found: IIconSet._Default (PropertyGet / VT_HRESULT)
Default member found: IIconSets._Default (PropertyGet / VT_HRESULT)
Default member found: IRanges._Default (PropertyGet / VT_HRESULT)
Default member found: IPages._Default (PropertyGet / VT_HRESULT)
Default member found: IServerViewableItems._Default (PropertyGet / VT_HRESULT)
Default member found: ITableStyleElements._Default (PropertyGet / VT_HRESULT)
Default member found: ITableStyle._Default (PropertyGet / VT_HRESULT)
Default member found: ITableStyles._Default (PropertyGet / VT_HRESULT)
Default member found: ISortFields._Default (PropertyGet / VT_HRESULT)
Default member found: IColorStops._Default (PropertyGet / VT_HRESULT)
Default member found: IFileExportConverters._Default (PropertyGet / VT_HRESULT)
Default member found: IAddIns2._Default (PropertyGet / VT_HRESULT)
Default member found: ISparklineGroups._Default (PropertyGet / VT_HRESULT)
Default member found: IPivotTableChangeList._Default (PropertyGet / VT_HRESULT)
Default member found: ISlicerCaches._Default (PropertyGet / VT_HRESULT)
Default member found: ISlicerCacheLevels._Default (PropertyGet / VT_HRESULT)
Default member found: ISlicers._Default (PropertyGet / VT_HRESULT)
Default member found: ISlicerItems._Default (PropertyGet / VT_HRESULT)
Default member found: ISlicerPivotTables._Default (PropertyGet / VT_HRESULT)
Default member found: IProtectedViewWindows._Default (PropertyGet / VT_HRESULT)
Default member found: IProtectedViewWindow._Default (PropertyGet / VT_HRESULT)
Default member found: Windows._Default (PropertyGet / VT_PTR)
Default member found: Range._Default (PropertyGet / VT_VARIANT)
Default member found: Range._Default (PropertyLet / VT_VOID)
Default member found: HPageBreaks._Default (PropertyGet / VT_PTR)
Default member found: VPageBreaks._Default (PropertyGet / VT_PTR)
Default member found: RecentFiles._Default (PropertyGet / VT_PTR)
Default member found: Style._Default (PropertyGet / VT_BSTR)
Default member found: Styles._Default (PropertyGet / VT_PTR)
Default member found: Borders._Default (PropertyGet / VT_PTR)
Default member found: AddIns._Default (PropertyGet / VT_PTR)
Default member found: Toolbars._Default (PropertyGet / VT_PTR)
Default member found: ToolbarButtons._Default (PropertyGet / VT_PTR)
Default member found: Areas._Default (PropertyGet / VT_PTR)
Default member found: MenuBars._Default (PropertyGet / VT_PTR)
Default member found: Menus._Default (PropertyGet / VT_PTR)
Default member found: MenuItems._Default (PropertyGet / VT_DISPATCH)
Default member found: Charts._Default (PropertyGet / VT_DISPATCH)
Default member found: DrawingObjects._Default (PropertyGet / VT_I4)
Default member found: DrawingObjects._Default (PropertyLet / VT_VOID)
Default member found: PivotCaches._Default (PropertyGet / VT_PTR)
Default member found: PivotFormula._Default (PropertyGet / VT_BSTR)
Default member found: PivotFormula._Default (PropertyLet / VT_VOID)
Default member found: PivotFormulas._Default (PropertyGet / VT_PTR)
Default member found: PivotTable._Default (PropertyGet / VT_BSTR)
Default member found: PivotTable._Default (PropertyLet / VT_VOID)
Default member found: PivotField._Default (PropertyGet / VT_BSTR)
Default member found: PivotField._Default (PropertyLet / VT_VOID)
Default member found: CalculatedFields._Default (PropertyGet / VT_PTR)
Default member found: PivotItem._Default (PropertyGet / VT_BSTR)
Default member found: PivotItem._Default (PropertyLet / VT_VOID)
Default member found: CalculatedItems._Default (PropertyGet / VT_PTR)
Default member found: Dialogs._Default (PropertyGet / VT_PTR)
Default member found: CheckBox._Default (PropertyGet / VT_I4)
Default member found: CheckBox._Default (PropertyLet / VT_VOID)
Default member found: CheckBoxes._Default (PropertyGet / VT_I4)
Default member found: CheckBoxes._Default (PropertyLet / VT_VOID)
Default member found: OptionButton._Default (PropertyGet / VT_I4)
Default member found: OptionButton._Default (PropertyLet / VT_VOID)
Default member found: OptionButtons._Default (PropertyGet / VT_I4)
Default member found: OptionButtons._Default (PropertyLet / VT_VOID)
Default member found: ScrollBar._Default (PropertyGet / VT_I4)
Default member found: ScrollBar._Default (PropertyLet / VT_VOID)
Default member found: ScrollBars._Default (PropertyGet / VT_I4)
Default member found: ScrollBars._Default (PropertyLet / VT_VOID)
Default member found: ListBox._Default (PropertyGet / VT_I4)
Default member found: ListBox._Default (PropertyLet / VT_VOID)
Default member found: ListBoxes._Default (PropertyGet / VT_I4)
Default member found: ListBoxes._Default (PropertyLet / VT_VOID)
Default member found: DropDown._Default (PropertyGet / VT_I4)
Default member found: DropDown._Default (PropertyLet / VT_VOID)
Default member found: DropDowns._Default (PropertyGet / VT_I4)
Default member found: DropDowns._Default (PropertyLet / VT_VOID)
Default member found: Spinner._Default (PropertyGet / VT_I4)
Default member found: Spinner._Default (PropertyLet / VT_VOID)
Default member found: Spinners._Default (PropertyGet / VT_I4)
Default member found: Spinners._Default (PropertyLet / VT_VOID)
...

所以,你得到了你所追求的 Collection.Item 和 ErrObject.Number。 [other] 现在的问题是,对于几乎所有其他默认成员,如何获取 _Default 所指向的成员的名称。

但这只是一个开始。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-15
    • 2013-10-02
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 2013-12-31
    • 2017-10-09
    • 1970-01-01
    相关资源
    最近更新 更多