private static bool FilterMatch(string className, WindowEnumerator instance) { switch (instance.Mode) { case FilterMode.StartEnd: { string start = null != instance.StartsWithFilter ? instance.StartsWithFilter.ToLower() : ""; string end = null != instance.EndsWithFilter ? instance.EndsWithFilter.ToLower() : ""; string target = className.ToLower(); return(target.StartsWith(start) && target.EndsWith(end)); } default: // Full { if (null != _currentInstance.Filter) { if (_currentInstance.Filter.Equals(className, StringComparison.InvariantCultureIgnoreCase)) { return(true); } else { return(false); } } else { return(false); } } } }
private static bool FilterMatch(string className, WindowEnumerator instance) { switch (instance.Mode) { case FilterMode.Full: { if (false == String.IsNullOrEmpty(_currentInstance.Filter)) { if (_currentInstance.Filter.Equals(className, StringComparison.InvariantCultureIgnoreCase)) { return(true); } else { return(false); } } else { return(true); } } case FilterMode.Start: { string start = null != instance.StartsWithFilter ? instance.StartsWithFilter.ToLower() : ""; string target = className.ToLower(); return(target.StartsWith(start)); } case FilterMode.End: { string end = null != instance.EndsWithFilter ? instance.EndsWithFilter.ToLower() : ""; string target = className.ToLower(); return(target.EndsWith(end)); } case FilterMode.StartEnd: { string start = null != instance.StartsWithFilter ? instance.StartsWithFilter.ToLower() : ""; string end = null != instance.EndsWithFilter ? instance.EndsWithFilter.ToLower() : ""; string target = className.ToLower(); return(target.StartsWith(start) && target.EndsWith(end)); } default: { throw new IndexOutOfRangeException(); } } }
/// <summary> /// Enumerates all top level windows on desktop. WARNING: The method returns null if operation timeout is reached. /// </summary> /// <param name="milliSecondsTimeout">a timeout for the operation. when a desktop windows is busy or non responding these method freeze. you can handle this with the operation timeout</param> /// <returns>result array or null</returns> public IntPtr[] EnumerateWindows(int milliSecondsTimeout) { if (milliSecondsTimeout < 0) { throw new ArgumentOutOfRangeException("milliSecondsTimeout"); } try { lock (_lockInstance) { Result.Clear(); _currentInstance = this; Thread thread1 = new Thread(new ParameterizedThreadStart(EnumerateWindowsAsync)); WaitHandle[] waitHandles = new WaitHandle[1]; ManualResetEvent mre1 = new ManualResetEvent(false); waitHandles[0] = mre1; thread1.Start(mre1); bool result = WaitHandle.WaitAll(waitHandles, milliSecondsTimeout); if (!result) { thread1.Abort(); Result.Clear(); _currentInstance = null; return(null); } else { _currentInstance = null; } } return(Result.ToArray()); } catch (Exception exception) { DebugConsole.Default.WriteException(exception); throw; } }
private static IDisposableEnumeration <ProxyInformation> GetUnknownAccessibleProxiesFromPath(int maximumResultCount) { RunningWindowTableItemCollection result = new RunningWindowTableItemCollection(); Tools.WndUtils.WindowEnumerator enumerator = new Tools.WndUtils.WindowEnumerator(String.Empty); IntPtr[] mainWindows = enumerator.EnumerateWindows(10000); if (null != mainWindows) { foreach (IntPtr item in mainWindows) { DoAccTest(result, item, item, maximumResultCount); EnumChildWindows(result, item, item, maximumResultCount); if (result.Count >= maximumResultCount) { break; } } } return(result); }
private static List<object> GetActiveExcelApplicationProxiesFromROT() { try { WindowEnumerator enumerator = new WindowEnumerator("XLMAIN"); IntPtr[] handles = enumerator.EnumerateWindows(2000); if (null == handles || handles.Length == 0) return new List<object>(); return ExcelApplicationWindow.GetApplicationProxiesFromHandle(handles); } catch (Exception exception) { DebugConsole.Default.WriteException(exception); throw; } }
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); }
private static bool FilterMatch(string className, WindowEnumerator instance) { switch (instance.Mode) { case FilterMode.Full: { if(false == String.IsNullOrEmpty(_currentInstance.Filter)) { if (_currentInstance.Filter.Equals(className, StringComparison.InvariantCultureIgnoreCase)) return true; else return false; } else return true; } case FilterMode.Start: { string start = null != instance.StartsWithFilter ? instance.StartsWithFilter.ToLower() : ""; string target = className.ToLower(); return target.StartsWith(start); } case FilterMode.End: { string end = null != instance.EndsWithFilter ? instance.EndsWithFilter.ToLower() : ""; string target = className.ToLower(); return target.EndsWith(end); } case FilterMode.StartEnd: { string start = null != instance.StartsWithFilter ? instance.StartsWithFilter.ToLower() : ""; string end = null != instance.EndsWithFilter ? instance.EndsWithFilter.ToLower() : ""; string target = className.ToLower(); return target.StartsWith(start) && target.EndsWith(end); } default: { throw new IndexOutOfRangeException(); } } }
/// <summary> /// Enumerates all top level windows on desktop. WARNING: The method returns null if operation timeout is reached. /// </summary> /// <param name="milliSecondsTimeout">a timeout for the operation. when a desktop windows is busy or non responding these method freeze. you can handle this with the operation timeout</param> /// <returns>result array or null</returns> public IntPtr[] EnumerateWindows(int milliSecondsTimeout) { if (milliSecondsTimeout < 0) throw new ArgumentOutOfRangeException("milliSecondsTimeout"); try { lock (_lockInstance) { Result.Clear(); _currentInstance = this; Thread thread1 = new Thread(new ParameterizedThreadStart(EnumerateWindowsAsync)); WaitHandle[] waitHandles = new WaitHandle[1]; ManualResetEvent mre1 = new ManualResetEvent(false); waitHandles[0] = mre1; thread1.Start(mre1); bool result = WaitHandle.WaitAll(waitHandles, milliSecondsTimeout); if (!result) { thread1.Abort(); Result.Clear(); _currentInstance = null; return null; } else { _currentInstance = null; } } return Result.ToArray(); } catch (Exception exception) { DebugConsole.Default.WriteException(exception); throw; } }
private static bool FilterMatch(string className, WindowEnumerator instance) { switch (instance.Mode) { case FilterMode.StartEnd: { string start = null != instance.StartsWithFilter ? instance.StartsWithFilter.ToLower() : ""; string end = null != instance.EndsWithFilter ? instance.EndsWithFilter.ToLower() : ""; string target = className.ToLower(); return target.StartsWith(start) && target.EndsWith(end); } default: // Full { if (null != _currentInstance.Filter) { if (_currentInstance.Filter.Equals(className, StringComparison.InvariantCultureIgnoreCase)) return true; else return false; } else return false; } } }
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; }
private static IDisposableEnumeration<ProxyInformation> GetUnknownAccessibleProxiesFromPath(int maximumResultCount) { RunningWindowTableItemCollection result = new RunningWindowTableItemCollection(); Tools.WndUtils.WindowEnumerator enumerator = new Tools.WndUtils.WindowEnumerator(String.Empty); IntPtr[] mainWindows = enumerator.EnumerateWindows(10000); if (null != mainWindows) { foreach (IntPtr item in mainWindows) { DoAccTest(result, item, item, maximumResultCount); EnumChildWindows(result, item, item, maximumResultCount); if (result.Count >= maximumResultCount) break; } } return result; }