protected Type GetTypeFromTypeLib(TypeLibrary typeLib) { if (typeLib == null) { _typeFailedException = new Exception("Unable to determine TypeLib " + "from CLSID: " + _guidStr); throw _typeFailedException; } // Set this information because we might have read the // class from a source other than the type library _container = typeLib; _typeLib = typeLib; // Get the type associated with the CLSId from the typelib if (Name == null) { ComClassInfo clsInfo = typeLib.GetClassInfoFromCLSID(_guid); Name = clsInfo.Name; } Type type = typeLib.FindTypeByName(Name, TypeLibrary.FIND_CLASS); if (type == null) { _typeFailedException = new Exception("CLR type not found in " + _container + " for ActiveX type " + this + ".\n\nThis is likely caused by " + "the assembly corresponding to " + "this type library not being " + "available."); throw _typeFailedException; } if (TraceUtil.If(this, TraceLevel.Info)) { Trace.WriteLine("ComClassInfo - type: " + type); } return(type); }
internal void GetBasicDetailText() { // The type library Guid comes from its key if (!(this is TypeLibrary) && _guidStr != null) { DetailPanel.Add(_infoType + " Guid", !ObjectBrowser.INTERNAL, 21, _guidStr); } // Get the CLR type if its available _displayedIdentifier = false; if (_container is TypeLibrary) { TypeLibrary typeLib = (TypeLibrary)_container; bool findClass; if (this is ComClassInfo) { findClass = TypeLibrary.FIND_CLASS; } else { findClass = !TypeLibrary.FIND_CLASS; } Type type = typeLib.FindTypeByName(_name, findClass); if (type != null) { DetailPanel.AddLink("CLR Type", !ObjectBrowser.INTERNAL, 15, TypeLinkHelper.TLHelper, type, HelpLinkHelper. MakeLinkModifier(type)); ComObjectInfo.GetDetailAxType(type, null, _infoType, 10); _displayedIdentifier = true; } else { DetailPanel.AddLink("Type Library", !ObjectBrowser.INTERNAL, 25, ComLinkHelper.CLHelper, _container, HelpLinkHelper.MakeLinkModifier (_container._helpFile, 0)); } } if (_helpFile != null) { DetailPanel.AddLink("Help File", !ObjectBrowser.INTERNAL, 225, HelpLinkHelper.HLHelper, HelpLinkHelper.MakeLinkModifier (_helpFile, 0)); if (_helpContext != 0) { DetailPanel.AddLink("Help Context", !ObjectBrowser.INTERNAL, 227, HelpLinkHelper.HLHelper, HelpLinkHelper.MakeLinkModifier (_helpFile, _helpContext)); } } // Type library description is displayed in the type library stuff if (DocString != null && !(this is TypeLibrary)) { DetailPanel.Add("Description", !ObjectBrowser.INTERNAL, 23, DocString); } // Show everything in the registry if (!(this is TypeLibrary) && _regKey != null) { try { string[] keys = _regKey.GetSubKeyNames(); foreach (String str in keys) { RegistryKey key = _regKey.OpenSubKey(str); string value = (string)key.GetValue(null); // If there is no value, see if there are any subkeys if (value == null || value.Equals("")) { String[] subKeys = key.GetSubKeyNames(); if (subKeys.Length > 0) { foreach (string subStr in subKeys) { AddRegistryValue(str, subStr); } } else { AddRegistryValue(str, null); } } else { AddRegistryValue(str, (string)key.GetValue(null)); } } } catch { // ignore errors } } }
// We have an object and we want to figure out the best // type for it protected override void SetType() { if (TraceUtil.If(this, TraceLevel.Info)) { Trace.WriteLine("ComObjInfo - SetType: " + _obj); } if (_obj != null) { if (!_obj.GetType().IsCOMObject&& !_obj.GetType().Equals(Windows.COM_ROOT_TYPE)) { base.SetType(); return; } if (TypeIsGoodEnough(_obj.GetType())) { _objType = _obj.GetType(); return; } } if (TypeIsGoodEnough(_objType)) { return; } // Get the type library so we can convert it only once. If // we don't do this, the GetTypeForITypeInfo code will // try to convert it multiple times. This also sets the _typeInfo // pointer. if (_typeLib == null) { IntPtr dispPtr; // Get the IDispatch ptr try { dispPtr = Marshal.GetIDispatchForObject(_obj); } catch { // This could just be a COM object, see if it // implements any of the interfaces we know about _interfaces = ComInterfaceInfo.GetImplementedInterfacesKnown(_obj); if (_interfaces.Count == 0) { throw new Exception ("Unable to determine type of object, " + "IDispatch not implemented, and it implements " + "no interfaces associated with known type " + "libraries."); } ComInterfaceInfo ciInfo = PickBestInterface(); AssignType(ciInfo.GetCLRType()); return; } // Get the type library from the dispPtr try { Marshal.AddRef(dispPtr); GetTypeLib(dispPtr); } finally { Marshal.Release(dispPtr); } } if (_typeLib != null) { // Get the COM pointer to the ITypeInfo so we can call // GetTypeForITypeInfo below. //IntPtr iTypeInfo = Marshal.GetIUnknownForObject(_typeInfo); // This figures out the actual class of the object, by hand // based on what interfaces it implements. //Type newType = FigureOutClass(); // This returns the type of the interface this object implements, // but is done completely automatically. This is probably the // better solution (since is less of my code). // The problem this this one is that it converts any // necessary type libraries automatically without // having my hooks so I can properly find out about the // converted type libraries //Type newType = Marshal.GetTypeForITypeInfo(iTypeInfo); // Just look up the type by name from the type info's name, // this will get an interface type, which is fine. A class type // might be better, but that takes more work to figure out. // FIXME - we may want to go to find the class type for those // objects that implement multiple interfaces, see about that. String typeName = ComClassInfo.GetTypeName(_typeInfo); _comTypeName = _typeLib.Name + "." + typeName; if (TraceUtil.If(this, TraceLevel.Info)) { Trace.WriteLine("TypeName: " + typeName); } Type newType = _typeLib.FindTypeByName(typeName, !TypeLibrary.FIND_CLASS); AssignType(newType); } // Can't figure it out, let the superclass deal with it if (_objType == null) { base.SetType(); } }