首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将TLB转换为IDL

将TLB转换为IDL
EN

Stack Overflow用户
提问于 2012-12-05 17:10:20
回答 2查看 1.5K关注 0票数 4

有谁知道命令行工具可以从类型库(.tlb)生成idl文件吗?这样做的原因是为了消除从regasm生成的枚举值中的下划线。本博客讨论了枚举中的下划线问题。

http://blogs.artinsoft.net/mrojas/archive/2010/05/17/interop-remove-prefix-from-c-enums-for-com.aspx

我知道OLEView可以生成IDL文件,但我正在寻找一种适合自动构建的解决方案。

EN

回答 2

Stack Overflow用户

发布于 2012-12-10 16:19:47

我怀疑这是一个能做你想做的事情的工具。但这应该不是太难滚动您自己的转换器。使用类型库有点麻烦。我编写了一个工具,用于从.idl文件或.tlb文件为COM接口生成C++包装类。读取接口相关信息的代码大约有250行代码,所以如果您添加一些代码来生成.idl文件,那么最终应该会有大约1000行代码。

下面的代码是我的C++代码的精简版本,应该可以让您大致了解一下。但是,由于我目前无法访问编译器,所以我无法检查它是编译(不太可能)还是工作(肯定不会)。

代码语言:javascript
复制
    #include <afxwin.h>
    #include <comdef.h>
    #include <atlconv.h>
    #include <atlbase.h>

    char* paTypeNames[] =
    {
        "VT_EMPTY",          // = 0,
        "VT_NULL",           // = 1,
        "short", //"VT_I2", //             = 2,
        "long", //"VT_I4", //             = 3,
        "real", // "VT_R4", //             = 4,
        "double", // "VT_R8", //             = 5,
        "VT_CY", //              = 6,
        "VT_DATE", //            = 7,
        "BSTR", //"VT_BSTR", //            = 8,
        "IDispatch*", // "VT_DISPATCH", //        = 9,
        "VT_ERROR", //           = 10,
        "VARIANT_BOOL", //"VT_BOOL", //            = 11,
        "VARIANT", //"VT_VARIANT", //         = 12,
        "IUnknown*", // VT_UNKNOWN        = 13,
        "VT_DECIMAL", //         = 14,
        "VBA reserves 15 for future use",
        "VT_I1", //              = 16,
        "VT_UI1", //             = 17,
        "VT_UI2", //             = 18,
        "VT_UI4", //             = 19,
        "VT_I8", //              = 20,
        "VT_UI8", //             = 21,
        "int", //"VT_INT", //             = 22,
        "UINT", //            = 23,
        "VOID", //            = 24,
        "HRESULT", //         = 25,
        "VT_PTR", //             = 26,
        "VT_SAFEARRAY", //       = 27,
        "VT_CARRAY", //          = 28,
        "VT_USERDEFINED", //     = 29,
        "VT_LPSTR", //           = 30,
        "VT_LPWSTR", //          = 31,
        "VBA reserves 32 for future use",
        "VBA reserves 33 for future use",
        "VBA reserves 34 for future use",
        "VBA reserves 35 for future use",
        "VT_RECORD", //          = 36,
        "VT_INT_PTR", //         = 37,
        "VT_UINT_PTR", //        = 38,
        "Unknown Type number above 39"
    };

    CString GetType (const TYPEDESC& p_TYPEDESC, ITypeInfo* p_pTypeInfo)
    {
        // Look up user defined types in the type library.
        if (p_TYPEDESC.vt == VT_USERDEFINED)
        {
            ITypeInfoPtr spInnerType;
            VERIFY (SUCCEEDED (p_pTypeInfo->GetRefTypeInfo (p_TYPEDESC.hreftype, &spInnerType)));
            BSTR CurrTypeName;
            VERIFY (SUCCEEDED (spInnerType->GetDocumentation (MEMBERID_NIL, &CurrTypeName, NULL, NULL, NULL)));
            return CString (CurrTypeName);
        }
        else if (p_TYPEDESC.vt == VT_PTR)
            return GetType (*p_TYPEDESC.lptdesc, p_pTypeInfo) + CString (_T("*"));
        else
        {
            return CString (paTypeNames[min (p_TYPEDESC.vt & VT_TYPEMASK, 39)]);
        }
    }


    bool ParseTypeLib (char* p_strTypeLibName)
    {
        USES_CONVERSION;

        // Load the type library.
        ITypeLibPtr spTypeLib;
        HRESULT hr = LoadTypeLibEx (A2OLE (p_strTypeLibName), REGKIND_DEFAULT, &spTypeLib);
        if (!(bool)spTypeLib)
            return false;

        UINT uiNumberOfTypes = spTypeLib->GetTypeInfoCount ();
        for (int i = 0; i < uiNumberOfTypes; i++)
        {
            ITypeInfoPtr spCurrTypeInfo;
            spTypeLib->GetTypeInfo (i, &spCurrTypeInfo);
            if (!(bool) spCurrTypeInfo)
                return false;

            // We only want to process interface definitions, so if we encounter anything
            // else (for example enums), we skip the rest of the loop.
            TYPEATTR* pCurrentTypeAttr;
            VERIFY (SUCCEEDED (spCurrTypeInfo->GetTypeAttr (&pCurrentTypeAttr)));
            if (pCurrentTypeAttr->typekind != TKIND_DISPATCH &&
                pCurrentTypeAttr->typekind != TKIND_INTERFACE)
                continue;

            // Retrieve the current interface name.
            CComBSTR CurrInterfaceName;
            hr = spTypeLib->GetDocumentation (i, &CurrInterfaceName, NULL, NULL, NULL);

            std::cout << "interface " << CurrInterfaceName;

            // Retrieve the name of the base class. According to MSDN
            // (http://msdn.microsoft.com/en-us/library/aa909031.aspx), we must first retrieve
            // the TKIND_INTERFACE type description for our class, and then we can retrieve
            // the base class information. We also need the TKIND_INTERFACE type description
            // because TKIND_DISPATCH type descriptions contain both the methods of the current
            // interface as well as the methods of all base interfaces.
            ITypeInfoPtr spBaseType;
            if (pCurrentTypeAttr->typekind == TKIND_DISPATCH)
            {
                HREFTYPE TempHREF;
                VERIFY (SUCCEEDED (spCurrTypeInfo->GetRefTypeOfImplType (-1, &TempHREF)));
                ITypeInfoPtr spTempInfo;
                VERIFY (SUCCEEDED (spCurrTypeInfo->GetRefTypeInfo (TempHREF, &spTempInfo)));
                spCurrTypeInfo = spTempInfo;
            }
            HREFTYPE BaseClassHREF;
            VERIFY (SUCCEEDED (spCurrTypeInfo->GetRefTypeOfImplType (0, &BaseClassHREF)));
            VERIFY (SUCCEEDED (spCurrTypeInfo->GetRefTypeInfo (BaseClassHREF, &spBaseType)));
            CComBSTR CurrBaseTypeName;
            VERIFY (SUCCEEDED (spBaseType->GetDocumentation (MEMBERID_NIL, &CurrBaseTypeName, NULL, NULL, NULL)));

            std::cout << " : " << CurrBaseTypeName << "{\n";

            // Process the methods of the current interface.
            FUNCDESC* pCurrFUNCDESC;
            int j = 0;
            while (SUCCEEDED (spCurrTypeInfo->GetFuncDesc (j++, &pCurrFUNCDESC)))
            {
                // Retrieve the return type of the COM method (this does not necessarily have to be
                // an HRESULT).
                std::cout << paTypeNames[pCurrFUNCDESC->elemdescFunc.tdesc.vt] << " ";

                // Ask for the function descriptor for the current function.
                unsigned int cNames;
                BSTR* CurrNames = DEBUG_NEW BSTR[pCurrFUNCDESC->cParams + 1];
                hr = spCurrTypeInfo->GetNames (pCurrFUNCDESC->memid, CurrNames, pCurrFUNCDESC->cParams + 1, &cNames);

                // The first element contains the name of the function.
                std::cout << CurrNames[0] << " (";

                // Process the parameters of the current function.
                for (int k = 0; k < pCurrFUNCDESC->cParams; k++)
                {
                    std::cout << "[";

                    // Determine the type of the parameter (in, out, retval).
                    bool needComma = false;
                    if (pCurrFUNCDESC->lprgelemdescParam[k].paramdesc.wParamFlags & PARAMFLAG_FIN)
                    {
                        std::cout << "in";
                        needComma = true;
                    }
                    if (pCurrFUNCDESC->lprgelemdescParam[k].paramdesc.wParamFlags & PARAMFLAG_FOUT)
                    {
                        if (needComma)
                            std::cout << ", ";
                        std::cout << "out";
                        needComma = true;
                    }
                    if (pCurrFUNCDESC->lprgelemdescParam[k].paramdesc.wParamFlags & PARAMFLAG_FRETVAL)
                    {
                        if (needComma)
                            std::cout << ", ";
                        std::cout << "retval";
                    }

                    std::cout << "] ";
                    std::cout << GetType (pCurrFUNCDESC->lprgelemdescParam[k].tdesc, spCurrTypeInfo);

                    // If we didn't get a name for the parameter, it must be the parameter of a property put
                    // method. In this case we call the parameter simply "RHS"
                    if (k + 1 >= cNames)
                        std::cout << "RHS";
                    else
                        std::cout << CurrNames[k + 1];
                }
                delete[] CurrNames;
            }
        }

        // If we have reached this line, all of the above operations must have succeeded.
        return true;
    }

你好,斯图尔特

票数 1
EN

Stack Overflow用户

发布于 2014-04-12 08:50:02

如果这是存储的副作用(第三方?)TLB文件,而不是源代码控制中的IDL文件,一种可能是签入IDL,并在构建过程中自动生成TLB。

使用IDL而不是TLB有一个额外的好处,即使对COM接口的版本更改易于使用diff工具进行比较。

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

https://stackoverflow.com/questions/13719631

复制
相关文章

相似问题

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