Exemple #1
0
        /// <summary>
        /// clears factory informations List
        /// </summary>
        public static void ClearFactoryInformations()
        {
            bool isLocked = false;

            try
            {
                if (Settings.EnableThreadSafe)
                {
                    Monitor.Enter(_factoryListLock);
                    isLocked = true;
                }

                _factoryList.Clear();
            }
            catch (Exception throwedException)
            {
                DebugConsole.WriteException(throwedException);
                throw (throwedException);
            }
            finally
            {
                if (isLocked)
                {
                    Monitor.Exit(_factoryListLock);
                    isLocked = false;
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// perform property set as latebind call
        /// </summary>
        /// <param name="comObject">target object</param>
        /// <param name="name">name of property</param>
        /// <param name="paramsArray">array with parameters</param>
        /// <param name="value">value to be set</param>
        /// <param name="paramModifiers">array with modifiers correspond paramsArray</param>
        public static void PropertySet(COMObject comObject, string name, object[] paramsArray, object value, ParameterModifier[] paramModifiers)
        {
            try
            {
                if (comObject.IsDisposed)
                {
                    throw new InvalidComObjectException();
                }

                if ((Settings.EnableSafeMode) && (!comObject.EntityIsAvailable(name, SupportEntityType.Property)))
                {
                    throw new EntityNotSupportedException(string.Format("Property {0} is not available.", name));
                }

                object[] newParamsArray = new object[paramsArray.Length + 1];
                for (int i = 0; i < paramsArray.Length; i++)
                {
                    newParamsArray[i] = paramsArray[i];
                }
                newParamsArray[newParamsArray.Length - 1] = value;

                comObject.InstanceType.InvokeMember(name, BindingFlags.SetProperty, null, comObject.UnderlyingObject, newParamsArray, paramModifiers, Settings.ThreadCulture, null);
            }
            catch (Exception throwedException)
            {
                DebugConsole.WriteException(throwedException);
                throw new System.Runtime.InteropServices.COMException(GetExceptionMessage(throwedException), throwedException);
            }
        }
Exemple #3
0
        public void AddChildObject(COMObject childObject)
        {
            bool isLocked = false;

            try
            {
                if (Settings.EnableThreadSafe)
                {
                    Monitor.Enter(_childListLock);
                    isLocked = true;
                }

                _listChildObjects.Add(childObject);
            }
            catch (Exception throwedException)
            {
                DebugConsole.WriteException(throwedException);
                throw (throwedException);
            }
            finally
            {
                if (isLocked)
                {
                    Monitor.Exit(_childListLock);
                    isLocked = false;
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// remove object from global list
        /// </summary>
        /// <param name="proxy"></param>
        internal static void RemoveObjectFromList(COMObject proxy)
        {
            bool isLocked = false;

            try
            {
                if (Settings.EnableThreadSafe)
                {
                    Monitor.Enter(_globalObjectList);
                    isLocked = true;
                }

                _globalObjectList.Remove(proxy);

                if (null != ProxyCountChanged)
                {
                    ProxyCountChanged(_globalObjectList.Count);
                }
            }
            catch (Exception throwedException)
            {
                DebugConsole.WriteException(throwedException);
            }
            finally
            {
                if (isLocked)
                {
                    Monitor.Exit(_globalObjectList);
                    isLocked = false;
                }
            }
        }
        /// <summary>
        /// release event binding
        /// </summary>
        private void RemoveEventBinding(bool removeFromList)
        {
            if (_connectionCookie != 0)
            {
                try
                {
                    _connectionPoint.Unadvise(_connectionCookie);
                    Marshal.ReleaseComObject(_connectionPoint);
                }
                catch (System.Runtime.InteropServices.COMException throwedException)
                {
                    DebugConsole.WriteException(throwedException);
                    ; // RPC server is disconnected or dead
                }
                catch (Exception throwedException)
                {
                    DebugConsole.WriteException(throwedException);
                    throw new COMException("An error occured.", throwedException);
                }

                _connectionPoint  = null;
                _connectionCookie = 0;

                if (removeFromList)
                {
                    _pointList.Remove(this);
                }
            }
        }
Exemple #6
0
 /// <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);
     }
 }
Exemple #7
0
        /// <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));
                    }
                }
            }
        }
Exemple #8
0
 /// <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);
     }
 }
Exemple #9
0
        public bool EqualsOnServer(COMObject obj)
        {
            if (_isCurrentlyDisposing || _isDisposed)
            {
                return(base.Equals(obj));
            }

            if (Object.ReferenceEquals(obj, null))
            {
                return(false);
            }

            IntPtr outValueA = IntPtr.Zero;
            IntPtr outValueB = IntPtr.Zero;
            IntPtr ptrA      = IntPtr.Zero;
            IntPtr ptrB      = IntPtr.Zero;

            try
            {
                ptrA = Marshal.GetIUnknownForObject(this.UnderlyingObject);
                int hResultA = Marshal.QueryInterface(ptrA, ref IID_IUnknown, out outValueA);

                ptrB = Marshal.GetIUnknownForObject(obj.UnderlyingObject);
                int hResultB = Marshal.QueryInterface(ptrB, ref IID_IUnknown, out outValueB);

                return(hResultA == 0 && hResultB == 0 && ptrA == ptrB);
            }
            catch (Exception exception)
            {
                DebugConsole.WriteException(exception);
                throw exception;
            }
            finally
            {
                if (IntPtr.Zero != ptrA)
                {
                    Marshal.Release(ptrA);
                }

                if (IntPtr.Zero != outValueA)
                {
                    Marshal.Release(outValueA);
                }

                if (IntPtr.Zero != ptrB)
                {
                    Marshal.Release(ptrB);
                }

                if (IntPtr.Zero != outValueB)
                {
                    Marshal.Release(outValueB);
                }
            }
        }
Exemple #10
0
 /// <summary>
 /// calls Quit for a proxy
 /// </summary>
 /// <param name="proxy"></param>
 private static void CallQuit(object proxy)
 {
     try
     {
         if (Settings.EnableAutomaticQuit)
         {
             Invoker.Method(proxy, "Quit");
         }
     }
     catch (Exception exception)
     {
         DebugConsole.WriteException(exception);
     }
 }
Exemple #11
0
        /// <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;
                }
            }
        }
Exemple #12
0
        /// <summary>
        /// creates a new COMObject based on classType of comProxy
        /// </summary>
        /// <param name="caller">parent there have created comProxy</param>
        /// <param name="comProxy">new created proxy</param>
        /// <returns>corresponding Wrapper class Instance or plain COMObject</returns>
        public static COMObject CreateObjectFromComProxy(COMObject caller, object comProxy)
        {
            CheckInitialize();
            bool isLocked = false;

            try
            {
                if (null == comProxy)
                {
                    return(null);
                }

                if (Settings.EnableThreadSafe)
                {
                    Monitor.Enter(_comObjectLock);
                    isLocked = true;
                }

                IFactoryInfo factoryInfo = GetFactoryInfo(comProxy);

                string className     = TypeDescriptor.GetClassName(comProxy);
                string fullClassName = factoryInfo.AssemblyNamespace + "." + className;

                // create new proxyType
                Type comProxyType = null;
                if (false == _proxyTypeCache.TryGetValue(fullClassName, out comProxyType))
                {
                    comProxyType = comProxy.GetType();
                    _proxyTypeCache.Add(fullClassName, comProxyType);
                }

                COMObject newObject = CreateObjectFromComProxy(factoryInfo, caller, comProxy, comProxyType, className, fullClassName);
                return(newObject);
            }
            catch (Exception throwedException)
            {
                DebugConsole.WriteException(throwedException);
                throw throwedException;
            }
            finally
            {
                if (isLocked)
                {
                    Monitor.Exit(_comObjectLock);
                    isLocked = false;
                }
            }
        }
Exemple #13
0
        /// <summary>
        /// creates a new COMObject from factoryInfo
        /// </summary>
        /// <param name="factoryInfo">Factory Info from Wrapper Assemblies</param>
        /// <param name="caller">parent there have created comProxy</param>
        /// <param name="comProxy">new created proxy</param>
        /// <param name="comProxyType">Type of comProxy</param>
        /// <param name="className">name of COMServer proxy class</param>
        /// <param name="fullClassName">full namespace and name of COMServer proxy class</param>
        /// <returns>corresponding Wrapper class Instance or plain COMObject</returns>
        public static COMObject CreateObjectFromComProxy(IFactoryInfo factoryInfo, COMObject caller, object comProxy, Type comProxyType, string className, string fullClassName)
        {
            CheckInitialize();
            bool isLocked = false;

            try
            {
                if (Settings.EnableThreadSafe)
                {
                    Monitor.Enter(_comObjectLock);
                    isLocked = true;
                }

                Type classType = null;
                if (true == _wrapperTypeCache.TryGetValue(fullClassName, out classType))
                {
                    // cached classType
                    object newClass = Activator.CreateInstance(classType, new object[] { caller, comProxy });
                    return(newClass as COMObject);
                }
                else
                {
                    // create new classType
                    classType = factoryInfo.Assembly.GetType(fullClassName, false, true);
                    if (null == classType)
                    {
                        throw new ArgumentException("Class not exists: " + fullClassName);
                    }

                    _wrapperTypeCache.Add(fullClassName, classType);
                    COMObject newClass = Activator.CreateInstance(classType, new object[] { caller, comProxy, comProxyType }) as COMObject;
                    return(newClass);
                }
            }
            catch (Exception throwedException)
            {
                DebugConsole.WriteException(throwedException);
                throw throwedException;
            }
            finally
            {
                if (isLocked)
                {
                    Monitor.Exit(_comObjectLock);
                    isLocked = false;
                }
            }
        }
Exemple #14
0
        public static void SingleMethodWithoutSafeMode(COMObject comObject, string name, object[] paramsArray)
        {
            try
            {
                if (comObject.IsDisposed)
                {
                    throw new InvalidComObjectException();
                }

                comObject.InstanceType.InvokeMember(name, BindingFlags.InvokeMethod | BindingFlags.GetProperty, null, comObject.UnderlyingObject, paramsArray, Settings.ThreadCulture);
            }
            catch (Exception throwedException)
            {
                DebugConsole.WriteException(throwedException);
                throw new System.Runtime.InteropServices.COMException(GetExceptionMessage(throwedException), throwedException);
            }
        }
Exemple #15
0
        /// <summary>
        /// perform method as latebind call with parameters
        /// </summary>
        /// <param name="comObject">target proxy</param>
        /// <param name="name">name of method</param>
        /// <param name="paramsArray">array with parameters</param>
        public static void Method(object comObject, string name, object[] paramsArray)
        {
            try
            {
                if ((comObject as COMObject).IsDisposed)
                {
                    throw new InvalidComObjectException();
                }

                comObject.GetType().InvokeMember(name, BindingFlags.InvokeMethod, null, comObject, paramsArray, Settings.ThreadCulture);
            }
            catch (Exception throwedException)
            {
                DebugConsole.WriteException(throwedException);
                throw new System.Runtime.InteropServices.COMException(GetExceptionMessage(throwedException), throwedException);
            }
        }
        /// <summary>
        /// try to find connection point by EnumConnectionPoints
        /// </summary>
        /// <param name="connectionPointContainer"></param>
        /// <param name="point"></param>
        /// <param name="sinkIds"></param>
        /// <returns></returns>
        private static string EnumConnectionPoint(IConnectionPointContainer connectionPointContainer, ref IConnectionPoint point, params string[] sinkIds)
        {
            IConnectionPoint[]    points     = new IConnectionPoint[1];
            IEnumConnectionPoints enumPoints = null;

            try
            {
                connectionPointContainer.EnumConnectionPoints(out enumPoints);
                while (enumPoints.Next(1, points, IntPtr.Zero) == 0) // S_OK = 0 , S_FALSE = 1
                {
                    if (null == points[0])
                    {
                        break;
                    }

                    Guid interfaceGuid;
                    points[0].GetConnectionInterface(out interfaceGuid);

                    for (int i = sinkIds.Length; i > 0; i--)
                    {
                        string id = interfaceGuid.ToString().Replace("{", "").Replace("}", "");
                        if (true == sinkIds[i - 1].Equals(id, StringComparison.InvariantCultureIgnoreCase))
                        {
                            Marshal.ReleaseComObject(enumPoints);
                            enumPoints = null;
                            point      = points[0];
                            return(id);
                        }
                    }
                }
                return(null);
            }
            catch (Exception throwedException)
            {
                DebugConsole.WriteException(throwedException);
                return(null);
            }
            finally
            {
                if (null != enumPoints)
                {
                    Marshal.ReleaseComObject(enumPoints);
                }
            }
        }
 /// <summary>
 /// create event binding
 /// </summary>
 /// <param name="connectPoint"></param>
 public void SetupEventBinding(IConnectionPoint connectPoint)
 {
     try
     {
         if (true == Settings.EnableEvents)
         {
             //connectPoint.GetConnectionInterface(out _interfaceId);
             _connectionPoint = connectPoint;
             _connectionPoint.Advise(this, out _connectionCookie);
             _pointList.Add(this);
         }
     }
     catch (Exception throwedException)
     {
         DebugConsole.WriteException(throwedException);
         throw (throwedException);
     }
 }
Exemple #18
0
        /// <summary>
        /// perform property get as latebind call with return value
        /// </summary>
        /// <param name="comObject">target proxy</param>
        /// <param name="name">name of property</param>
        /// <returns>any return value</returns>
        public static object PropertyGet(object comObject, string name)
        {
            try
            {
                if ((comObject as COMObject).IsDisposed)
                {
                    throw new InvalidComObjectException();
                }

                object returnValue = comObject.GetType().InvokeMember(name, BindingFlags.GetProperty, null, comObject, null, Settings.ThreadCulture);
                return(returnValue);
            }
            catch (Exception throwedException)
            {
                DebugConsole.WriteException(throwedException);
                throw new System.Runtime.InteropServices.COMException(GetExceptionMessage(throwedException), throwedException);
            }
        }
Exemple #19
0
        /// <summary>
        ///  creates a new COMObject array
        /// </summary>
        /// <param name="caller">parent there have created comProxy</param>
        /// <param name="comProxyArray">new created proxy array</param>
        /// <returns>corresponding Wrapper class Instance array or plain COMObject array</returns>
        public static COMObject[] CreateObjectArrayFromComProxy(COMObject caller, object[] comProxyArray)
        {
            CheckInitialize();
            bool isLocked = false;

            try
            {
                if (null == comProxyArray)
                {
                    return(null);
                }

                if (Settings.EnableThreadSafe)
                {
                    Monitor.Enter(_comObjectLock);
                    isLocked = true;
                }

                Type        comVariantType  = null;
                COMObject[] newVariantArray = new COMObject[comProxyArray.Length];
                for (int i = 0; i < comProxyArray.Length; i++)
                {
                    comVariantType = comProxyArray[i].GetType();
                    IFactoryInfo factoryInfo   = GetFactoryInfo(comProxyArray[i]);
                    string       className     = TypeDescriptor.GetClassName(comProxyArray[i]);
                    string       fullClassName = factoryInfo.AssemblyNamespace + "." + className;
                    newVariantArray[i] = CreateObjectFromComProxy(factoryInfo, caller, comProxyArray[i], comVariantType, className, fullClassName);
                }
                return(newVariantArray);
            }
            catch (Exception throwedException)
            {
                DebugConsole.WriteException(throwedException);
                throw throwedException;
            }
            finally
            {
                if (isLocked)
                {
                    Monitor.Exit(_comObjectLock);
                    isLocked = false;
                }
            }
        }
Exemple #20
0
        /// <summary>
        /// Calls the OnDispose event as service for client callers
        /// </summary>
        /// <returns></returns>
        private bool RaiseOnDispose()
        {
            bool cancelDispose = false;

            try
            {
                if (null != OnDispose)
                {
                    OnDisposeEventArgs eventArgs = new OnDisposeEventArgs(this);
                    OnDispose(eventArgs);
                    cancelDispose = eventArgs.Cancel;
                }
            }
            catch (Exception exception)
            {
                DebugConsole.WriteException(exception);
            }
            return(cancelDispose);
        }
Exemple #21
0
        /// <summary>
        /// creates a new COMObject based on wrapperClassType
        /// </summary>
        /// <param name="caller"></param>
        /// <param name="comProxy"></param>
        /// <param name="wrapperClassType"></param>
        /// <returns></returns>
        public static COMObject CreateKnownObjectFromComProxy(COMObject caller, object comProxy, Type wrapperClassType)
        {
            CheckInitialize();
            bool isLocked = false;

            try
            {
                if (null == comProxy)
                {
                    return(null);
                }

                if (Settings.EnableThreadSafe)
                {
                    Monitor.Enter(_comObjectLock);
                    isLocked = true;
                }

                // create new proxyType
                Type comProxyType = null;
                if (false == _proxyTypeCache.TryGetValue(wrapperClassType.FullName, out comProxyType))
                {
                    comProxyType = comProxy.GetType();
                    _proxyTypeCache.Add(wrapperClassType.FullName, comProxyType);
                }

                COMObject newClass = Activator.CreateInstance(wrapperClassType, new object[] { caller, comProxy, comProxyType }) as COMObject;
                return(newClass);
            }
            catch (Exception throwedException)
            {
                DebugConsole.WriteException(throwedException);
                throw throwedException;
            }
            finally
            {
                if (isLocked)
                {
                    Monitor.Exit(_comObjectLock);
                    isLocked = false;
                }
            }
        }
Exemple #22
0
 /// <summary>
 /// returns info the assembly is a NetOffice Api Assembly
 /// </summary>
 /// <param name="itemName"></param>
 /// <returns></returns>
 private static bool ContainsNetOfficePublicKeyToken(AssemblyName itemName)
 {
     try
     {
         string targetKeyToken = itemName.FullName.Substring(itemName.FullName.LastIndexOf(" ") + 1);
         foreach (string item in KnownNetOfficeKeyTokens)
         {
             if (item.EndsWith(targetKeyToken, StringComparison.InvariantCultureIgnoreCase))
             {
                 return(true);
             }
         }
         return(false);
     }
     catch (System.IO.FileNotFoundException exception)
     {
         DebugConsole.WriteException(exception);
         return(false);
     }
 }
Exemple #23
0
        /// <summary>
        /// creates a new COMObject array based on wrapperClassType
        /// </summary>
        /// <param name="caller"></param>
        /// <param name="comProxyArray"></param>
        /// <param name="wrapperClassType"></param>
        /// <returns></returns>
        public static COMObject[] CreateKnownObjectArrayFromComProxy(COMObject caller, object[] comProxyArray, Type wrapperClassType)
        {
            CheckInitialize();
            bool isLocked = false;

            try
            {
                if (null == comProxyArray)
                {
                    return(null);
                }

                if (Settings.EnableThreadSafe)
                {
                    Monitor.Enter(_comObjectLock);
                    isLocked = true;
                }

                Type        comVariantType  = null;
                COMObject[] newVariantArray = new COMObject[comProxyArray.Length];
                for (int i = 0; i < comProxyArray.Length; i++)
                {
                    newVariantArray[i] = Activator.CreateInstance(wrapperClassType, new object[] { caller, comProxyArray[i], comVariantType }) as COMObject;
                }

                return(newVariantArray);
            }
            catch (Exception throwedException)
            {
                DebugConsole.WriteException(throwedException);
                throw throwedException;
            }
            finally
            {
                if (isLocked)
                {
                    Monitor.Exit(_comObjectLock);
                    isLocked = false;
                }
            }
        }
Exemple #24
0
 /// <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);
     }
 }
Exemple #25
0
        /// <summary>
        /// perform property set as latebind call
        /// </summary>
        /// <param name="comObject"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public static void PropertySet(COMObject comObject, string name, object[] value)
        {
            try
            {
                if (comObject.IsDisposed)
                {
                    throw new InvalidComObjectException();
                }

                if ((Settings.EnableSafeMode) && (!comObject.EntityIsAvailable(name, SupportEntityType.Property)))
                {
                    throw new EntityNotSupportedException(string.Format("Property {0} is not available.", name));
                }

                comObject.InstanceType.InvokeMember(name, BindingFlags.SetProperty, null, comObject.UnderlyingObject, value, Settings.ThreadCulture);
            }
            catch (Exception throwedException)
            {
                DebugConsole.WriteException(throwedException);
                throw new System.Runtime.InteropServices.COMException(GetExceptionMessage(throwedException), throwedException);
            }
        }
Exemple #26
0
 /// <summary>
 /// returns info the assembly is a NetOffice Api Assembly
 /// </summary>
 /// <param name="itemAssembly"></param>
 /// <returns></returns>
 private static bool ContainsNetOfficeAttribute(Assembly itemAssembly)
 {
     try
     {
         List <string> dependAssemblies = new List <string>();
         object[]      attributes       = itemAssembly.GetCustomAttributes(true);
         foreach (object itemAttribute in attributes)
         {
             string fullnameAttribute = itemAttribute.GetType().FullName;
             if (fullnameAttribute == "NetOffice.NetOfficeAssemblyAttribute")
             {
                 return(true);
             }
         }
         return(false);
     }
     catch (System.IO.FileNotFoundException exception)
     {
         DebugConsole.WriteException(exception);
         return(false);
     }
 }
Exemple #27
0
        public void RemoveChildObject(COMObject childObject)
        {
            bool isLocked = false;

            try
            {
                _listChildObjects.Remove(childObject);
            }
            catch (Exception throwedException)
            {
                DebugConsole.WriteException(throwedException);
                throw (throwedException);
            }
            finally
            {
                if (isLocked)
                {
                    Monitor.Exit(_childListLock);
                    isLocked = false;
                }
            }
        }
Exemple #28
0
        /// <summary>
        /// perform method as latebind call with parameters and parameter modifiers to use ref parameter(s)
        /// </summary>
        /// <param name="comObject">target object</param>
        /// <param name="name">name of method</param>
        /// <param name="paramsArray">array with parameters</param>
        /// <param name="paramModifiers">ararry with modifiers correspond paramsArray</param>
        public static void SingleMethod(COMObject comObject, string name, object[] paramsArray, ParameterModifier[] paramModifiers)
        {
            try
            {
                if (comObject.IsDisposed)
                {
                    throw new InvalidComObjectException();
                }

                if ((Settings.EnableSafeMode) && (!comObject.EntityIsAvailable(name, SupportEntityType.Method)))
                {
                    throw new EntityNotSupportedException(string.Format("Method {0} is not available.", name));
                }

                comObject.InstanceType.InvokeMember(name, BindingFlags.InvokeMethod | BindingFlags.GetProperty, null, comObject.UnderlyingObject, paramsArray, paramModifiers, Settings.ThreadCulture, null);
            }
            catch (Exception throwedException)
            {
                DebugConsole.WriteException(throwedException);
                throw new System.Runtime.InteropServices.COMException(GetExceptionMessage(throwedException), throwedException);
            }
        }
Exemple #29
0
 /// <summary>
 /// calls dipose in case if param is COMObject, calls Marshal.ReleaseComObject in case of param is a COM proxy
 /// </summary>
 public static void ReleaseParam(object param)
 {
     try
     {
         if (null != param)
         {
             COMObject comObject = param as COMObject;
             if (null != comObject)
             {
                 comObject.Dispose();
             }
             else if (param is MarshalByRefObject)
             {
                 Marshal.ReleaseComObject(param);
             }
         }
     }
     catch (Exception throwedException)
     {
         DebugConsole.WriteException(throwedException);
         throw new System.Runtime.InteropServices.COMException(GetExceptionMessage(throwedException), throwedException);
     }
 }
        /// <summary>
        /// try to find connection point by FindConnectionPoint
        /// </summary>
        /// <param name="connectionPointContainer"></param>
        /// <param name="point"></param>
        /// <param name="sinkIds"></param>
        /// <returns></returns>
        private static string FindConnectionPoint(IConnectionPointContainer connectionPointContainer, ref IConnectionPoint point, params string[] sinkIds)
        {
            try
            {
                for (int i = sinkIds.Length; i > 0; i--)
                {
                    Guid             refGuid  = new Guid(sinkIds[i - 1]);
                    IConnectionPoint refPoint = null;
                    connectionPointContainer.FindConnectionPoint(ref refGuid, out refPoint);
                    if (null != refPoint)
                    {
                        point = refPoint;
                        return(sinkIds[i - 1]);
                    }
                }

                return(null);
            }
            catch (Exception throwedException)
            {
                DebugConsole.WriteException(throwedException);
                return(null);
            }
        }