Beispiel #1
0
        /// <summary>
        /// Returns all running com proxies there is match with given arguments
        /// </summary>
        /// <param name="componentName">component name, for example Excel</param>
        /// <param name="className">class name, for example Application</param>
        /// <returns>COM proxy enumerator</returns>
        public static IDisposableSequence GetActiveInstances(string componentName, string className)
        {
            string compName = ValidateArgumentString(componentName);
            string clsName  = ValidateArgumentString(className);

            if (compName == "EXCEL" && className == "APPLICATION")
            {
                return(GetActiveExcelApplicationProxiesFromDesktop());
            }
            else
            {
                return(RunningObjectTable.GetActiveProxies(componentName, className));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Returns a running com proxy there is match with given arguments
        /// </summary>
        /// <param name="componentName">component name, for example Excel or null as wildcard</param>
        /// <param name="className">class name, for example Application or null as wildcard</param>
        /// <param name="throwExceptionIfNothingFound">throw an exception if no proxy was found</param>
        /// <returns>proxy instance or null(Nothing in Visual Basic)</returns>
        /// <exception cref="NetOfficeCOMException">no instance found and throwExceptionIfNothingFound is set</exception>
        public static object GetActiveInstance(string componentName, string className, bool throwExceptionIfNothingFound)
        {
            string compName = ValidateArgumentString(componentName);
            string clsName  = ValidateArgumentString(className);

            if (compName == "EXCEL" && className == "APPLICATION")
            {
                object result = GetActiveExcelApplicationProxyFromDesktop();
                if (null == result && throwExceptionIfNothingFound)
                {
                    throw new NetOfficeCOMException(String.Format("Unable to find active instance {0}.", componentName + " " + className));
                }
                return(result);
            }
            else
            {
                return(RunningObjectTable.GetActiveProxy(componentName, className, throwExceptionIfNothingFound));
            }
        }
Beispiel #3
0
        /// <summary>
        /// Returns a running com proxy there is match with given arguments
        /// </summary>
        /// <param name="componentName">component name, for example Excel or null as wildcard</param>
        /// <param name="className">class name, for example Application or null as wildcard</param>
        /// <param name="throwExceptionIfNothingFound">throw an exception if no proxy was found</param>
        /// <returns>proxy instance or null</returns>
        public static object GetActiveInstance(string componentName, string className, bool throwExceptionIfNothingFound)
        {
            string compName = ValidateArgumentString(componentName);
            string clsName  = ValidateArgumentString(className);

            if (compName == "EXCEL" && className == "APPLICATION")
            {
                object result = GetActiveExcelApplicationProxyFromDesktop();
                if (null == result && throwExceptionIfNothingFound)
                {
                    throw new System.Runtime.InteropServices.COMException("Target instance is not running.");
                }
                return(result);
            }
            else
            {
                return(RunningObjectTable.GetActiveProxy(componentName, className, throwExceptionIfNothingFound));
            }
        }
Beispiel #4
0
        private static IDisposableEnumeration <ProxyInformation> GetKnownAccessibleProxiesFromPath(IEnumerable <AccessibleWindowTarget> targets, int maximumResultCount)
        {
            if (null == targets)
            {
                throw new ArgumentNullException("targets");
            }

            RunningWindowTableItemCollection result = new RunningWindowTableItemCollection();

            if (maximumResultCount <= 0)
            {
                return(result);
            }

            foreach (AccessibleWindowTarget target in targets)
            {
                Tools.WndUtils.WindowEnumerator enumerator =
                    new Tools.WndUtils.WindowEnumerator(
                        target.MainClassName, target.MainClassNameEnd, (Tools.WndUtils.WindowEnumerator.FilterMode)Convert.ToInt32(target.NameCompare));
                IntPtr[] mainHandles = enumerator.EnumerateWindows(_mainWindowTimeoutMilliseconds);
                if (null == mainHandles)
                {
                    continue;
                }
                foreach (IntPtr item in mainHandles)
                {
                    Tools.WndUtils.ChildWindowBatchEnumerator childEnumerator =
                        new Tools.WndUtils.ChildWindowBatchEnumerator(item);

                    foreach (string subItem in target.ChildPath)
                    {
                        childEnumerator.SearchOrder.Add(
                            new Tools.WndUtils.ChildWindowBatchEnumerator.SearchCriteria(subItem));
                    }
                    IntPtr[] childHandles = childEnumerator.EnumerateWindows(_childWindowTimeoutMilliseconds);
                    if (null == childHandles)
                    {
                        continue;
                    }

                    foreach (IntPtr childHandle in childHandles)
                    {
                        object accObject = Tools.WndUtils.Win32.AccessibleObjectFromWindow(childHandle);
                        if (null != accObject && accObject is MarshalByRefObject)
                        {
                            object targetProxy = null;
                            if (!String.IsNullOrEmpty(target.AccPropertyName))
                            {
                                targetProxy = TryInvokeProperty(accObject, target.AccPropertyName);
                                Marshal.ReleaseComObject(accObject);
                            }
                            else
                            {
                                targetProxy = accObject;
                            }

                            if (null != targetProxy)
                            {
                                string             itemComponentName = TypeDescriptor.GetComponentName(targetProxy);
                                COMTypes.ITypeInfo typeInfo          = RunningObjectTable.TryCreateTypeInfo(targetProxy);
                                string             library           = RunningObjectTable.GetParentLibraryGuid(typeInfo).ToString();
                                string             id            = GetTypeGuid(typeInfo).ToString();
                                string             itemClassName = TypeDescriptor.GetClassName(targetProxy);
                                string             itemCaption   = itemClassName;
                                if (!String.IsNullOrWhiteSpace(itemClassName) && !String.IsNullOrWhiteSpace(itemComponentName))
                                {
                                    itemCaption = String.Format("{0} {1}", itemComponentName, itemClassName);
                                }

                                IntPtr procID = Tools.WndUtils.Win32.GetWindowThreadProcessId(childHandle);
                                ProxyInformation.ProcessElevation procElevation =
                                    Tools.WndUtils.ProcessElevation.ConvertToProcessElevation(Tools.WndUtils.ProcessElevation.IsProcessElevated(procID));

                                ProxyInformation info = new ProxyInformation(targetProxy,
                                                                             itemCaption, id, itemClassName, itemComponentName, library, procID, procElevation);

                                result.Add(info);
                                if (null != typeInfo)
                                {
                                    RunningObjectTable.ReleaseTypeInfo(typeInfo);
                                }

                                if (result.Count >= maximumResultCount)
                                {
                                    return(result);
                                }
                            }
                        }
                    }
                }
            }

            return(result);
        }