zoukankan      html  css  js  c++  java
  • Get the type name of a com object

        /// <summary>
        /// Exposes objects, methods and properties to programming tools and other
        /// applications that support Automation.
        /// </summary>
        [ComImport()]
        [Guid("00020400-0000-0000-C000-000000000046")]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        interface IDispatch
        {
            [PreserveSig]
            int GetTypeInfoCount(out int count);
    
            [PreserveSig]
            int GetTypeInfo(
                [MarshalAs(UnmanagedType.U4)] int iTInfo,
                [MarshalAs(UnmanagedType.U4)] int lcid,
                out ComTypes.ITypeInfo typeInfo);
    
            [PreserveSig]
            int GetIDsOfNames(
                ref Guid riid,
                [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr)]
                string[] rgsNames,
                int cNames,
                int lcid,
                [MarshalAs(UnmanagedType.LPArray)] int[] rgDispId);
    
            [PreserveSig]
            int Invoke(
                int dispIdMember,
                ref Guid riid,
                uint lcid,
                ushort wFlags,
                ref ComTypes.DISPPARAMS pDispParams,
                out object pVarResult,
                ref ComTypes.EXCEPINFO pExcepInfo,
                IntPtr[] pArgErr);
        }
    
        public class ocComHelper
        {
            /// <summary>
            /// Returns a string value representing the type name of the specified COM object.
            /// </summary>
            /// <param name="comObj">A COM object the type name of which to return.</param>
            /// <returns>A string containing the type name.</returns>
            public static string GetTypeName(object comObj)
            {
    
                if (comObj == null)
                    return String.Empty;
    
                if (!Marshal.IsComObject(comObj))
                    //The specified object is not a COM object
                    return String.Empty;
    
                IDispatch dispatch = comObj as IDispatch;
                if (dispatch == null)
                    //The specified COM object doesn't support getting type information
                    return String.Empty;
    
                ComTypes.ITypeInfo typeInfo = null;
                try
                {
                    try
                    {
                        // obtain the ITypeInfo interface from the object
                        dispatch.GetTypeInfo(0, 0, out typeInfo);
                    }
                    catch (Exception)
                    {
                        //Cannot get the ITypeInfo interface for the specified COM object
                        return String.Empty;
                    }
    
                    string typeName = "";
                    string documentation, helpFile;
                    int helpContext = -1;
    
                    try
                    {
                        //retrieves the documentation string for the specified type description 
                        typeInfo.GetDocumentation(-1, out typeName, out documentation,
                            out helpContext, out helpFile);
                    }
                    catch (Exception)
                    {
                        // Cannot extract ITypeInfo information
                        return String.Empty;
                    }
                    return typeName;
                }
                catch (Exception)
                {
                    // Unexpected error
                    return String.Empty;
                }
                finally
                {
                    if (typeInfo != null) Marshal.ReleaseComObject(typeInfo);
                }
            }
        }
  • 相关阅读:
    接口XMPPConnection
    Smack 4.3.4 API
    Tigase XMPP Server
    Openfire Meetings插件是一个包含各种Jitsi项目(如VideoBridge和Meet)的实现
    华为方舟编译器 下载 和 LiteOS Studio Setup 2019-04-16.exe SDK下载
    华为 鸿蒙系统(HarmonyOS)
    C#.NET常见问题(FAQ)-如何使用2D绘图控件ZedGraph绘制坐标轴和坐标曲线
    C#.NET常见问题(FAQ)-如何声明list的多维数组
    C#.NET常见问题(FAQ)-如何设置控件水平对齐,垂直对齐
    C#.NET常见问题(FAQ)-如何修改Form不能修改窗体大小
  • 原文地址:https://www.cnblogs.com/yoyohappy/p/4597383.html
Copyright © 2011-2022 走看看