/// <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