/// <summary> /// AssemblyResolver Event /// </summary> /// <param name="sender"></param> /// <param name="args"></param> /// <returns></returns> private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { try { string directoryName = _thisAssembly.CodeBase.Substring(0, _thisAssembly.CodeBase.LastIndexOf("/")); directoryName = directoryName.Replace("/", "\\").Substring(8); string fileName = args.Name.Substring(0, args.Name.IndexOf(",")); string fullFileName = System.IO.Path.Combine(directoryName, fileName + ".dll"); if (System.IO.File.Exists(fullFileName)) { DebugConsole.WriteLine(string.Format("Try to resolve Assembly", args.Name)); Assembly assembly = System.Reflection.Assembly.Load(args.Name); return(assembly); } else { DebugConsole.WriteLine(string.Format("Failed to resolve Assembly", args.Name)); return(null); } } catch (Exception exception) { DebugConsole.WriteException(exception); return(null); } }
/// <summary> /// analyze loaded NetOffice assemblies and add dependent assemblies to the runtime if necessary /// </summary> private static void AddDependentNetOfficeAssemblies() { if (!Settings.EnableAdHocLoading) { return; } foreach (DependentAssembly dependAssembly in _dependentAssemblies) { if (!AssemblyExistsInFactoryList(dependAssembly.Name)) { string fileName = dependAssembly.ParentAssembly.CodeBase.Substring(0, dependAssembly.ParentAssembly.CodeBase.LastIndexOf("/")) + "/" + dependAssembly.Name; fileName = fileName.Replace("/", "\\").Substring(8); DebugConsole.WriteLine(string.Format("Try to load dependent assembly {0}.", fileName)); if (System.IO.File.Exists(fileName)) { try { Assembly asssembly = Assembly.LoadFile(fileName); AddAssembly(asssembly.GetName().Name, asssembly); } catch (Exception exception) { DebugConsole.WriteException(exception); } } else { DebugConsole.WriteLine(string.Format("Assembly {0} not found.", fileName)); } } } }
/// <summary> /// Assembly loader for multitargeting(host) scenarios /// </summary> /// <param name="fileName"></param> /// <returns></returns> private static Assembly TryLoadAssembly(string fileName) { try { string directoryName = _thisAssembly.CodeBase.Substring(0, _thisAssembly.CodeBase.LastIndexOf("/")); directoryName = directoryName.Replace("/", "\\").Substring(8); string fullFileName = System.IO.Path.Combine(directoryName, fileName); if (System.IO.File.Exists(fullFileName)) { DebugConsole.WriteLine(string.Format("Try to resolve Assembly", fileName)); Assembly assembly = System.Reflection.Assembly.LoadFile(fullFileName); return(assembly); } else { DebugConsole.WriteLine(string.Format("Failed to resolve Assembly", fileName)); return(null); } } catch (Exception exception) { DebugConsole.WriteException(exception); return(null); } }
int IMessageFilter.MessagePending(IntPtr htaskCallee, uint dwTickCount, uint dwPendingType) { if (LogMode == RetryMessageFilterLogMode.MessagePending || LogMode == RetryMessageFilterLogMode.Both) { DebugConsole.WriteLine("IMessageFilter.MessagePending.dwTickCount={0} , dwPendingType={1}", dwTickCount, dwPendingType); } return(1); // PENDINGMSG_WAITNOPROCESS see: http://msdn.microsoft.com/en-us/library/aa908923.aspx for further info }
/// <summary> /// add assembly to list /// </summary> /// <param name="name"></param> /// <param name="itemAssembly"></param> /// <returns>list of dependend assemblies</returns> private static string[] AddAssembly(string name, Assembly itemAssembly) { List <string> dependAssemblies = new List <string>(); object[] attributes = itemAssembly.GetCustomAttributes(true); foreach (object itemAttribute in attributes) { string fullnameAttribute = itemAttribute.GetType().FullName; DebugConsole.WriteLine("Attribute:{0}", fullnameAttribute); if (fullnameAttribute == "NetOffice.NetOfficeAssemblyAttribute") { Type factoryInfoType = itemAssembly.GetType(name + ".Utils.ProjectInfo"); NetOffice.IFactoryInfo factoryInfo = Activator.CreateInstance(factoryInfoType) as NetOffice.IFactoryInfo; bool exists = false; foreach (IFactoryInfo itemFactory in _factoryList) { DebugConsole.WriteLine("IFactoryInfo:{0}:{1}", itemFactory.Assembly.FullName, factoryInfo.Assembly.FullName); if (itemFactory.Assembly.FullName == factoryInfo.Assembly.FullName) { exists = true; break; } } if (!exists) { _factoryList.Add(factoryInfo); } foreach (string itemDependency in factoryInfo.Dependencies) { bool found = false; foreach (string itemExistingDependency in dependAssemblies) { if (itemDependency == itemExistingDependency) { found = true; break; } } if (!found) { dependAssemblies.Add(itemDependency); } } } } return(dependAssemblies.ToArray()); }
/// <summary> /// Must be called from client assembly for COMObject Support /// Recieve factory infos from all loaded NetOfficeApi Assemblies in current application domain /// </summary> public static void Initialize() { _initalized = true; bool isLocked = false; try { if (Settings.EnableThreadSafe) { Monitor.Enter(_factoryListLock); isLocked = true; } DebugConsole.WriteLine("NetOffice.Factory.Initialize() DeepLevel:{0}", Settings.EnableDeepLoading); TryLoadAssembly("ExcelApi.dll"); TryLoadAssembly("WordApi.dll"); TryLoadAssembly("OutlookApi.dll"); TryLoadAssembly("PowerPointApi.dll"); TryLoadAssembly("AccessApi.dll"); TryLoadAssembly("VisioApi.dll"); TryLoadAssembly("MSProjectApi.dll"); if (!_assemblyResolveEventConnected) { AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve); _assemblyResolveEventConnected = true; } ClearCache(); AddNetOfficeAssemblies(Settings.EnableDeepLoading); AddDependentNetOfficeAssemblies(); DebugConsole.WriteLine("Factory contains {0} assemblies", _factoryList.Count); DebugConsole.WriteLine("NetOffice.Factory.Initialize() passed"); } catch (Exception throwedException) { DebugConsole.WriteException(throwedException); throw (throwedException); } finally { if (isLocked) { Monitor.Exit(_factoryListLock); isLocked = false; } } }
public static string GetConnectionPoint(COMObject comProxy, ref IConnectionPoint point, params string[] sinkIds) { if (null == sinkIds) { return(null); } IConnectionPointContainer connectionPointContainer = (IConnectionPointContainer)comProxy.UnderlyingObject; if (Settings.EnableEventDebugOutput) { DebugConsole.WriteLine(comProxy.UnderlyingTypeName + " -> Call FindConnectionPoint"); } string id = FindConnectionPoint(connectionPointContainer, ref point, sinkIds); if (Settings.EnableEventDebugOutput) { DebugConsole.WriteLine(comProxy.UnderlyingTypeName + " -> Call FindConnectionPoint passed"); } if (null == id) { if (Settings.EnableEventDebugOutput) { DebugConsole.WriteLine(comProxy.UnderlyingTypeName + " -> Call EnumConnectionPoint"); } id = EnumConnectionPoint(connectionPointContainer, ref point, sinkIds); if (Settings.EnableEventDebugOutput) { DebugConsole.WriteLine(comProxy.UnderlyingTypeName + " -> Call EnumConnectionPoint passed"); } } if (null != id) { return(id); } else { throw new COMException("Specified instance doesnt implement the target event interface."); } }
/// <summary> /// Assembly loader for multitargeting(host) scenarios /// </summary> /// <param name="fileName"></param> /// <returns></returns> private static Assembly TryLoadAssembly(string fileName) { try { string directoryName = _thisAssembly.CodeBase.Substring(0, _thisAssembly.CodeBase.LastIndexOf("/")); directoryName = directoryName.Replace("/", "\\").Substring(8); string fullFileName = System.IO.Path.Combine(directoryName, fileName); if (System.IO.File.Exists(fullFileName)) { Assembly assembly = System.Reflection.Assembly.LoadFrom(fullFileName); Type factoryInfoType = assembly.GetType(fileName.Substring(0, fileName.Length - 4) + ".Utils.ProjectInfo", false, false); NetOffice.IFactoryInfo factoryInfo = Activator.CreateInstance(factoryInfoType) as NetOffice.IFactoryInfo; bool exists = false; foreach (IFactoryInfo itemFactory in _factoryList) { if (itemFactory.Assembly.FullName == factoryInfo.Assembly.FullName) { exists = true; break; } } if (!exists) { _factoryList.Add(factoryInfo); DebugConsole.WriteLine("Recieve IFactoryInfo:{0}:{1}", factoryInfo.Assembly.FullName, factoryInfo.Assembly.FullName); } return(assembly); } else { DebugConsole.WriteLine(string.Format("Unable to resolve assembly {0}. The assembly doesnt exists in current codebase.", fileName)); return(null); } } catch (Exception exception) { DebugConsole.WriteException(exception); return(null); } }
int IMessageFilter.RetryRejectedCall(IntPtr htaskCallee, uint dwTickCount, uint dwRejectType) { if (LogMode == RetryMessageFilterLogMode.RetryRejectedCall || LogMode == RetryMessageFilterLogMode.Both) { DebugConsole.WriteLine("IMessageFilter.RetryRejectedCall.dwTickCount={0} , dwRejectType={1}", dwTickCount, dwRejectType); } switch (RetryMode) { case RetryMessageFilterMode.Immediately: return(1); case RetryMessageFilterMode.Delayed: return(101); case RetryMessageFilterMode.None: return(-1); default: throw new IndexOutOfRangeException("RetryMessageFilterMode"); } }
/// <summary> /// analyze assemblies in current appdomain and connect all NetOffice assemblies to the core runtime /// </summary> private static void AddNetOfficeAssemblies(bool deepLevel) { _dependentAssemblies.Clear(); if (deepLevel) { Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly domainAssembly in assemblies) { foreach (AssemblyName itemName in domainAssembly.GetReferencedAssemblies()) { if (ContainsNetOfficePublicKeyToken(itemName)) { string assemblyName = itemName.Name; DebugConsole.WriteLine(string.Format("Detect NetOffice assembly {0}.", assemblyName)); Assembly itemAssembly = Assembly.Load(itemName); string[] depends = AddAssembly(assemblyName, itemAssembly); foreach (string depend in depends) { bool found = false; foreach (DependentAssembly itemExistingDependency in _dependentAssemblies) { if (depend == itemExistingDependency.Name) { found = true; break; } } if (!found) { _dependentAssemblies.Add(new DependentAssembly(depend, itemAssembly)); } } } } } } else { Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly itemAssembly in assemblies) { string assemblyName = itemAssembly.GetName().Name; if (ContainsNetOfficeAttribute(itemAssembly)) { DebugConsole.WriteLine(string.Format("Detect NetOffice assembly {0}.", assemblyName)); string[] depends = AddAssembly(assemblyName, itemAssembly); foreach (string depend in depends) { bool found = false; foreach (DependentAssembly itemExistingDependency in _dependentAssemblies) { if (depend == itemExistingDependency.Name) { found = true; break; } } if (!found) { _dependentAssemblies.Add(new DependentAssembly(depend, itemAssembly)); } } } } } }