Get the type name of a COM object

Source: Internet
Author: User

    /// <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)]InterfaceIDispatch {[PreserveSig]intGetTypeInfoCount ( out intcount); [PreserveSig]intGetTypeInfo ([MarshalAs (UNMANAGEDTYPE.U4)]intItinfo, [MarshalAs (UNMANAGEDTYPE.U4)]intLCID, outcomtypes.itypeinfo TypeInfo); [PreserveSig]intGetIDsOfNames (refGuid riid, [MarshalAs (UnmanagedType.LPArray, ArraySubType=unmanagedtype.lpwstr)]string[] rgsnames,intCNAMEs,intLCID, [MarshalAs (UnmanagedType.LPArray)]int[] rgdispid); [PreserveSig]intInvoke (intDispidmember,refGuid riid,UINTLCID,ushortwflags,refcomtypes.dispparams pDispParams, out ObjectPvarresult,refcomtypes.excepinfo Pexcepinfo, intptr[] pargerr); }     Public classOccomhelper {/// <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 stringGettypename (Objectcomobj) {            if(Comobj = =NULL)                returnString.Empty; if(!Marshal.iscomobject (comobj))//The specified object is not a COM object                returnString.Empty; IDispatch Dispatch= Comobj asIDispatch; if(Dispatch = =NULL)                //The specified COM object doesn ' t support getting type information                returnString.Empty; Comtypes.itypeinfo TypeInfo=NULL; Try            {                Try                {                    //Obtain the ITypeInfo interface from the objectDispatch. GetTypeInfo (0,0, outtypeinfo); }                Catch(Exception) {//cannot get the ITypeInfo interface for the specified COM object                    returnString.Empty; }                stringTypeName =""; stringdocumentation, HelpFile; intHelpContext =-1; Try                {                    //retrieves the documentation string for the specified type descriptionTypeinfo.getdocumentation (-1, outTypeName, outdocumentation, outHelpContext, outhelpFile); }                Catch(Exception) {//cannot extract ITypeInfo information                    returnString.Empty; }                returnTypeName; }            Catch(Exception) {//Unexpected error                returnString.Empty; }            finally            {                if(TypeInfo! =NULL) Marshal.ReleaseComObject (TypeInfo); }        }    }

Get the type name of a COM object

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.