例如,一个实现了IDispatch的组件,在客户端程序用MFC来导入类型库,VS自动为我声明一个包装类,这个包装类可以用来Invoke组件的函数。我的问题是: 这个组件是用ATL实现的,某些接口和函数里面有[helpstring]这样的属性。请问: 客户端程序能否得到并显示这些类型库当中的helpstring? 如何编码呢?谢谢。

解决方案 »

  1.   

    msdn 例子:
    OLEAUT\TIBROWSE
    通过类型库接口获取到.
      

  2.   

    我在网上没有找到啊。能给个msdn链接吗?
    多谢。
      

  3.   

    vc6 的 msdn 中有, 后来微软给去掉了
    就是通过 dispatch 接口得到类型信息.
    看看 msdn 中:
    Win32 and COM Development
      Component Development
        Automation
          SDK Documentation
            Type Description Interface
    下面的接口, 自己也能写出来.
    void
    typetree::load(IDispatch* pDisp)
    {
      //remove all nodes
      DeleteAllItems();  // release previous if loaded
      release();  UINT cCount=0;
      HRESULT hr = pDisp->GetTypeInfoCount(&cCount);
      for (UINT i = 0; i < cCount; i++) {
        ITypeInfo* pTypeInfo = 0;
        pDisp->GetTypeInfo(i, LOCALE_USER_DEFAULT, &pTypeInfo);
        walkTypeInfo(pTypeInfo, TVI_ROOT);
      }
    }
    void
    typetree::walkTypeInfo(ITypeInfo* pTypeInfo, HTREEITEM hParentItem)
    {
      WORD cFuncs, cVars, cImplTypes;
      TYPEKIND typekind;
      TYPEATTR* pTypeAttr = 0;
      TCHAR szText[128];
      CStdString str;
      HTREEITEM hItem;
      HRESULT hr = pTypeInfo->GetTypeAttr(&pTypeAttr);
      if (SUCCEEDED(hr)) {
        GUID guid = pTypeAttr->guid;
        LCID lcid = pTypeAttr->lcid;
        DWORD dwReserved = pTypeAttr->dwReserved;
        MEMBERID memidConstructor = pTypeAttr->memidConstructor;
        MEMBERID memidDestructor = pTypeAttr->memidDestructor;
        LPOLESTR lpstrSchema = pTypeAttr->lpstrSchema;
        ULONG cbSizeInstance = pTypeAttr->cbSizeInstance;
        typekind = pTypeAttr->typekind;
        cFuncs = pTypeAttr->cFuncs;
        cVars = pTypeAttr->cVars;
        cImplTypes = pTypeAttr->cImplTypes;
        WORD cbSizeVft = pTypeAttr->cbSizeVft;
        WORD cbAlignment = pTypeAttr->cbAlignment;
        WORD wTypeFlags = pTypeAttr->wTypeFlags;
        WORD wMajorVerNum = pTypeAttr->wMajorVerNum;
        WORD wMinorVerNum = pTypeAttr->wMinorVerNum;
        TYPEDESC tdescAlias = pTypeAttr->tdescAlias;
        IDLDESC idldescType = pTypeAttr->idldescType;    pTypeInfo->ReleaseTypeAttr(pTypeAttr);
      } else {
        return;
      }
      {
        BSTR name, docString, helpFile;
        DWORD dwHelpContext;    //If the passed-in memid is MEMBERID_NIL, then the documentation for the type description is returned.
        pTypeInfo->GetDocumentation(MEMBERID_NIL, &name, &docString, &dwHelpContext, &helpFile);
        _stprintf(szText, _T("%s [%s]"), (LPCTSTR)name, typeKindToStr(typekind, str));
        SysFreeString(name);SysFreeString(docString);SysFreeString(helpFile);
      }  switch (typekind)
      {
      case TKIND_ENUM:
        hItem = InsertItem(hParentItem, NULL, szText, NULL, 4, 4);
        break;
      case TKIND_RECORD:
        hItem = InsertItem(hParentItem, NULL, szText, NULL, 0, 0);
        break;
      case TKIND_MODULE:
        hItem = InsertItem(hParentItem, NULL, szText, NULL, 14, 14);
        break;
      case TKIND_INTERFACE:
      case TKIND_DISPATCH:
        {
          hItem = InsertItem(hParentItem, NULL, szText, NULL, 8, 8);
          walkVariables(pTypeInfo, cVars, hItem);
          walkFunctions(pTypeInfo, cFuncs, hItem);
        }
        break;
      case TKIND_COCLASS:
        hItem = InsertItem(hParentItem, NULL, szText, NULL, 1, 1);
        break;
      case TKIND_ALIAS:
        hItem = InsertItem(hParentItem, NULL, szText, NULL, 10, 10);
        break;
      case TKIND_UNION:
        hItem = InsertItem(hParentItem, NULL, szText, NULL, 23, 23);
        break;
      default:
        hItem = InsertItem(hParentItem, NULL, szText, NULL, 21, 21);
        break;
      }
    }