// Gets the objects to iterate over to make the child nodes protected override ICollection GetChildren() { ProgressDialog progress = new ProgressDialog(); progress.Setup(String.Format(StringParser.Parse("${res:ComponentInspector.ProgressDialog.GettingInformationDialogTitle}"), "Type Library"), String.Format(StringParser.Parse("${res:ComponentInspector.ProgressDialog.GettingInformationMessage}"), "Type Library"), TypeLibrary.GetRegisteredTypeLibsCount(), !ProgressDialog.HAS_PROGRESS_TEXT, ProgressDialog.FINAL); progress.ShowIfNotDone(); return TypeLibrary.GetRegisteredTypeLibs(progress); }
// Gets the objects to iterate over to make the child nodes protected override ICollection GetChildren() { lock (_allChildren) { if (_allChildren.Count > 0) { _progress = null; return _allChildren.Values; } _progress = new ProgressDialog(); String[] keys = _baseKey.GetSubKeyNames(); // Show the progress of both reading the interfaces and // building the tree _progress.Setup(String.Format(StringParser.Parse("${res:ComponentInspector.ProgressDialog.GettingInformationDialogTitle}"), _progressName), String.Format(StringParser.Parse("${res:ComponentInspector.ProgressDialog.GettingInformationMessage}"), _progressName), keys.Length * 2, !ProgressDialog.HAS_PROGRESS_TEXT, ProgressDialog.FINAL); _progress.ShowIfNotDone(); foreach (String str in keys) { try { // Update early in case there is an exception // with this one _progress.UpdateProgress(1); RegistryKey key = _baseKey.OpenSubKey(str); Object info = ProcessChild(key, str); if (info != null) { _allChildren.Add(GetSortKey(info), info); } else { // Account for the fact that this will not be // present in the AllocateChildNode _progress.UpdateProgress(1); } } catch (Exception ex) { // Account for the fact that this will not be // present in the AllocateChildNode _progress.UpdateProgress(1); TraceUtil.WriteLineIf (null, TraceLevel.Info, "Failure to read: " + str + " " + ex); } } } return _allChildren.Values; }
// Restore the typelibs/controls that were previously loaded public static void RestoreComEnvironment() { if (_typelibs.Count == 0) return; ProgressDialog progressDialog = new ProgressDialog(); progressDialog.Setup("Loading Remembered ActiveX Files", "Please wait while I load the previously " + "opened ActiveX files.", _typelibs.Count, ProgressDialog.HAS_PROGRESS_TEXT, ProgressDialog.FINAL); progressDialog.ShowIfNotDone(); TraceUtil.WriteLineInfo(null, DateTime.Now + " ActiveX Restore start "); try { for (int i = _typelibs.Count - 1; i >= 0; --i) { PreviouslyOpenedTypeLibrary typeLib = _typelibs[i]; try { TraceUtil.WriteLineInfo(null, DateTime.Now + " restore assy: " + typeLib.FileName); progressDialog.UpdateProgressText(typeLib.FileName); Guid guid = new Guid(typeLib.Guid); TypeLibrary.RestoreTypeLib(typeLib.FileName, guid, typeLib.Version); TraceUtil.WriteLineInfo(null, DateTime.Now + " loaded assy: " + typeLib.FileName); progressDialog.UpdateProgress(1); } catch (Exception ex) { TraceUtil.WriteLineWarning(null, "Assemblies - deleting bad assemblies entry: " + typeLib.FileName + " " + ex); _typelibs.Remove(typeLib); progressDialog.UpdateProgress(1); } } // This depends on having all of the assemblies restored TraceUtil.WriteLineInfo(null, DateTime.Now + " ActiveX Restore end "); } catch (Exception ex) { TraceUtil.WriteLineError(null, "Unexpected exception " + "restoring assemblies: " + ex); } progressDialog.Finished(); if (ComponentInspectorProperties.AddRunningComObjects) { AddRunningObjs(); } FavoriteTypeLibNode.Expand(); }
protected static void AddRunningObjs() { TraceUtil.WriteLineInfo(null, "AddRunningObjs"); ObjectTreeNode tlNode = null; // Running objects - added to object tree if (_runningObjInfo == null) { _runningObjInfo = ObjectInfoFactory.GetObjectInfo(false, new ArrayList()); _runningObjInfo.ObjectName = StringParser.Parse("${res:ComponentInspector.ComRunningObjectsTreeNode.Text}"); tlNode = new ObjectTreeNode(true, _runningObjInfo); // The children are explicitly added here tlNode.ChildrenAlreadyAdded = true; tlNode.NodeOrder = 20; tlNode.AllowDelete = false; tlNode.IsDropTarget = false; ObjectBrowser.ObjTree.CreateControl(); ObjectBrowser.ObjTree.Invoke(new BrowserTree.AddNodeInvoker(ObjectBrowser.ObjTree.AddNode), new Object[] { tlNode }); } else { tlNode = ObjectTreeNode.FindObject(_runningObjInfo.Obj, !ObjectTreeNode.CREATE_OBJ); ((ArrayList)_runningObjInfo.Obj).Clear(); tlNode.InvalidateNode(); } ProgressDialog progress = new ProgressDialog(); progress.Setup(StringParser.Parse("${res:ComponentInspector.ProgressDialog.AddingRunningComObjectsDialogTitle}"), StringParser.Parse("${res:ComponentInspector.ProgressDialog.AddingRunningComObjectsMessage}"), ComObjectInfo.GetRunningObjectCount(), ProgressDialog.HAS_PROGRESS_TEXT, ProgressDialog.FINAL); progress.ShowIfNotDone(); foreach (ComObjectInfo comObjInfo in ComObjectInfo.GetRunningObjects(progress)) { ObjectBrowser.ObjTree.Invoke(new ObjectTreeNode.AddObjectInvoker(tlNode.AddObject), new Object[] { comObjInfo }); } tlNode.Expand(); progress.Finished(); }
// Finds the COM running objects public static IList GetRunningObjects(ProgressDialog progress) { ArrayList runningObjects = new ArrayList(); Hashtable runningHash = new Hashtable(); UCOMIBindCtx bc; UCOMIRunningObjectTable rot; UCOMIEnumMoniker em; UCOMIMoniker[] monikers = new UCOMIMoniker[1]; ActiveX.CreateBindCtx(0, out bc); bc.GetRunningObjectTable(out rot); rot.EnumRunning(out em); // Look at each Moniker in the ROT int unused; while (0 == em.Next(1, monikers, out unused)) { try { UCOMIMoniker moniker = monikers[0]; Object obj; rot.GetObject(moniker, out obj); String monikerName; moniker.GetDisplayName(bc, null, out monikerName); ComObjectInfo comObjInfo; // Check for duplicates against the other running objects Object runObj = runningHash[obj]; if (runObj != null) { // Get the existing object's moniker comObjInfo = (ComObjectInfo) ObjectInfo.GetObjectInfo(obj); if (monikerName.Equals(comObjInfo._monikerName)) { TraceUtil.WriteLineInfo (typeof(ComObjectInfo), "ROT - Skipping duplicate: " + monikerName); progress.UpdateProgress(1); continue; } } else { runningHash.Add(obj, obj); } comObjInfo = (ComObjectInfo) ObjectInfoFactory.GetObjectInfo(true, obj); // Need moniker name before update progress comObjInfo.CalcRunningObjName(rot, bc, moniker, monikerName); progress.UpdateProgressText(comObjInfo.GetMonikerName()); progress.UpdateProgress(1); runningObjects.Add(comObjInfo); TraceUtil.WriteLineIf(typeof(ComObjectInfo), TraceLevel.Info, "ROT - added: " + comObjInfo.GetName() + " " + comObjInfo.ObjType + " " + Win32Utils.RegKeyToString (comObjInfo._classIdKey) + Win32Utils.RegKeyToString (comObjInfo._classNameKey)); } catch (Exception ex) { TraceUtil.WriteLineIf(typeof(ComObjectInfo), TraceLevel.Info, "ROT - Exception processing ROT entry: " + ex); progress.UpdateProgress(1); continue; } } Marshal.ReleaseComObject(em); Marshal.ReleaseComObject(bc); Marshal.ReleaseComObject(rot); return runningObjects; }
// Reads the type library information // This is not supposed to throw internal void ReadTypeLibInfo() { if (_typeLibInfoRead) return; ReadTypeLibFile(); ProgressDialog progress = new ProgressDialog(); progress.Setup("Reading " + GetName(), "Please wait while I read the definitions for " + GetName(), ProgressDialog.NO_PROGRESS_BAR, !ProgressDialog.HAS_PROGRESS_TEXT, ProgressDialog.FINAL); progress.ShowIfNotDone(); try { ReadTypeLibInfoInternal(); } finally { progress.Finished(); } }
internal static ICollection GetRegisteredTypeLibs(ProgressDialog progress) { lock (typeof(TypeLibrary)) { if (_registeredTypeLibsValid) { if (progress != null) { progress.UpdateProgress(_registeredTypeLibs.Values.Count); } return _registeredTypeLibs.Values; } String[] keys = Windows.KeyTypeLib.GetSubKeyNames(); foreach (String str in keys) { if (progress != null) progress.UpdateProgress(1); GetTypeLibsFromRegistry(str); } _registeredTypeLibsValid = true; } return _registeredTypeLibs.Values; }
// This translates the type library into an assembly internal void TranslateTypeLib() { TraceUtil.WriteLineInfo(this, "TranslateTypeLib"); // Already associated with an assembly if (_assy != null) return; ReadTypeLibFile(); if (_iTypeLib == null) throw new Exception("Failed to open file: " + _fileName); // If this is present, we can't translate the assembly, // we just have to try and load it using this name. This // is the case for things like mscorlib.tlb if (_translatedAssyFullName != null) { Assembly assy = null; try { String fileName; // We will have the file name if we are coming through // here after the type library has been opened previously // (when it is being restored from the registry). if (_assyInfo._fileName != null) fileName = _assyInfo._fileName; else fileName = _translatedAssyFullName; TraceUtil.WriteLineInfo (null, "TypeLib - translated assy looking for: " + fileName); assy = Assembly.Load(fileName); } catch (FileNotFoundException) { // Don't ask the user if we have already asked // the user if (!_dontFindAssy) assy = FindAssemblyFile(); } TraceUtil.WriteLineInfo(null, "TypeLib - source assy is: " + assy + " for translated lib " + _translatedAssyFullName); RecordTranslatedAssy(assy); return; } CheckForPrimaryInteropAssy(); if (_primaryInteropAssyName != null) return; // Only create the versioned names if the primary interop assy // is not used, since that's not versioned. CreateAssyNames(); TraceUtil.WriteLineIf(null, TraceLevel.Info, "TypeLib - converting - url: " + _assyInfo._url + " file: " + _assyInfo._fileName); String dir = ComponentInspectorProperties.ConvertedAssemblyDirectory; Directory.CreateDirectory(dir); Directory.SetCurrentDirectory(dir); if (IsAssyCurrent(_assyInfo._fileName, _fileName)) { // See if there is a translated version already present. // We don't know if there should be an Ax version or not, // try the normal one and if it succeeds, try the Ax version. // If the normal version succeeds however, we can consider // that we are done. RecordTranslatedAssy(Assembly.LoadFrom(_assyInfo._url)); TraceUtil.WriteLineInfo(null, "TypeLib - found previous xlated " + _assyInfo._name); try { RecordTranslatedAssy(Assembly.LoadFrom(_axAssyInfo._url)); } catch { // Ignore } return; } // Convert the type library to an assembly // The assembly load event happens in here, it is going to // be unable to add the assembly to the registry since there // is no CodeBase associated with a dynamically created // assembly, but that's not a problem since we add it below // when adding the GUID for the typelib ProgressDialog progress = new ProgressDialog(); progress.Setup("Converting " + GetName() + " to .NET Assembly", "Please wait while I convert " + GetName() + " to a .NET Assembly", ProgressDialog.NO_PROGRESS_BAR, !ProgressDialog.HAS_PROGRESS_TEXT, ProgressDialog.FINAL); progress.ShowIfNotDone(); try { TypeLibConverter converter = new TypeLibConverter(); AssemblyBuilder asm = converter.ConvertTypeLibToAssembly (_iTypeLib, _assyInfo._fileName, // Can't do this because we can't strong name these //TypeLibImporterFlags.PrimaryInteropAssembly, 0, this, null, null, null, null); // For the problem associated with Q316653 //FixEventSinkHelpers(asm); asm.Save(_assyInfo._fileName); RecordTranslatedAssy(Assembly.LoadFrom(_assyInfo._url)); // Generate the ActiveX wrapper for the typelib, // if that's necessary AxImporter.Options impOptions = new AxImporter.Options(); impOptions.outputDirectory = dir; impOptions.outputName = _axAssyInfo._fileName; impOptions.references = this; impOptions.genSources = true; AxImporter axImport = new AxImporter(impOptions); try { // This converts the type library and generates the // wrapper for any ActiveX controls. It produces // a list of the assemblies that it created. If there // are no ActiveX controls this will throw. String wrapper = axImport. GenerateFromTypeLibrary(_iTypeLib); } catch (Exception ex) { if (ex.Message.StartsWith("Did not find a")) { TraceUtil.WriteLineInfo(this, "TypeLib - no controls found"); return; } TraceUtil.WriteLineWarning(this, "TypeLib - AxImporter error " + _assyInfo._url + " " + ex); throw new Exception("Error in AxImporter for: " + _assyInfo._url, ex); } foreach (String aname in axImport.GeneratedAssemblies) { TraceUtil.WriteLineInfo(this, "TypeLib - AX gen assy: " + aname); } foreach (String sname in axImport.GeneratedSources) { TraceUtil.WriteLineInfo(this, "TypeLib - AX gen source: " + sname); } RecordTranslatedAssy(Assembly.LoadFrom(_axAssyInfo._url)); TraceUtil.WriteLineIf(null, TraceLevel.Info, "TypeLib - conversion done: " + _assyInfo._url); } finally { progress.Finished(); } }
// Restore the assemblies that were previously loaded public static void RestoreAssemblies() { AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler(AssemblyLoadHandler); if (_assemblies.Count == 0) return; ProgressDialog progressDialog = new ProgressDialog(); progressDialog.Setup(StringParser.Parse("${res:ComponentInspector.ProgressDialog.LoadingPreviouslyOpenedAssembliesDialogTitle}"), StringParser.Parse("${res:ComponentInspector.ProgressDialog.LoadingPreviouslyOpenedAssembliesMessage}"), _assemblies.Count, ProgressDialog.HAS_PROGRESS_TEXT, ProgressDialog.FINAL); progressDialog.ShowIfNotDone(); TraceUtil.WriteLineInfo(null, DateTime.Now + " Assembly Restore start "); try { for (int i = _assemblies.Count - 1; i >= 0; --i) { PreviouslyOpenedAssembly assembly = _assemblies[i]; try { AssemblyName aName = new AssemblyName(); aName.CodeBase = assembly.CodeBase; TraceUtil.WriteLineInfo(null, DateTime.Now + " restore assy: " + assembly.CodeBase); progressDialog.UpdateProgressText(assembly.CodeBase); // Tell the TypeLibrary code the assembly // belongs to some type library Guid guid = Guid.Empty; // Don't mess with assemblies that came from // typelibs if the typelib is not there if (assembly.TypeLibGuid.Length > 0) { guid = new Guid(assembly.TypeLibGuid); TypeLibrary lib = TypeLibrary.GetTypeLibOpened(guid, assembly.TypeLibVersion); String assyFileName = new Uri(assembly.CodeBase).LocalPath; if (lib == null || !TypeLibrary.IsAssyCurrent(assyFileName, lib.FileName)) { TraceUtil.WriteLineInfo (null, DateTime.Now + " skipped assy (typelib not opened " + "or current): " + assembly.CodeBase); // Forget it _assemblies.Remove(assembly); progressDialog.UpdateProgress(1); continue; } } // The load event that happens under here causes the // assembly to appear in the tree Assembly assy = Assembly.Load(aName); // Tell the TypeLibrary code the assembly // belongs to some type library if (assembly.TypeLibGuid.Length > 0) TypeLibrary.RestoreAssembly(assy, guid, assembly.TypeLibVersion); TraceUtil.WriteLineInfo(null, DateTime.Now + " loaded assy: " + assembly.CodeBase); progressDialog.UpdateProgress(1); } catch (Exception ex) { TraceUtil.WriteLineWarning(null, "Assemblies - deleting bad assemblies entry: " + assembly.CodeBase + " " + ex); _assemblies.Remove(assembly); progressDialog.UpdateProgress(1); } } // This depends on having all of the assemblies restored TraceUtil.WriteLineInfo(null, DateTime.Now + " Assembly Restore end "); _assyRootNode.Expand(); } catch (Exception ex) { TraceUtil.WriteLineError(null, "Unexpected exception restoring assemblies: " + ex); } progressDialog.Finished(); }