private void RaiseDetector() { if (!string.IsNullOrEmpty(Processname)) { using (var automation = AutomationUtil.getAutomation()) { var current = automation.FocusedElement(); if (current.Properties.ProcessId.IsSupported) { var ProcessId = automation.FocusedElement().Properties.ProcessId.Value; var p = System.Diagnostics.Process.GetProcessById(automation.FocusedElement().Properties.ProcessId.Value); if (!PatternMatcher.FitsMask(p.ProcessName.ToLower(), Processname.ToLower())) { Log.Information("KeyboardDetector skipped, expected " + Processname + ", but got " + p.ProcessName); return; } } } } OnDetector?.Invoke(this, new DetectorEvent(), EventArgs.Empty); }
public static ProcessInfo GetProcessInfo(this AutomationElement element) { if (!element.Properties.ProcessId.IsSupported) { return(null); } ProcessInfo result = new ProcessInfo(); int processId = -1; IntPtr handle = IntPtr.Zero; try { processId = element.Properties.ProcessId.Value; var p = System.Diagnostics.Process.GetProcessById(processId); handle = p.Handle; result.ProcessName = p.ProcessName; result.Filename = p.MainModule.FileName.ReplaceEnvironmentVariable(); } catch (Exception) { } bool _isImmersiveProcess = false; try { if (handle != IntPtr.Zero) { _isImmersiveProcess = NativeMethods.IsImmersiveProcess(handle); } } catch (Exception) { } string ApplicationUserModelId = null; if (_isImmersiveProcess) { var automation = AutomationUtil.getAutomation(); var pc = new FlaUI.Core.Conditions.PropertyCondition(automation.PropertyLibrary.Element.ClassName, "Windows.UI.Core.CoreWindow"); var _el = element.FindFirstChild(pc); if (_el != null) { processId = _el.Properties.ProcessId.Value; IntPtr ptrProcess = OpenProcess(QueryLimitedInformation, false, processId); if (IntPtr.Zero != ptrProcess) { uint cchLen = 130; // Currently APPLICATION_USER_MODEL_ID_MAX_LENGTH = 130 StringBuilder sbName = new StringBuilder((int)cchLen); Int32 lResult = GetApplicationUserModelId(ptrProcess, ref cchLen, sbName); if (APPMODEL_ERROR_NO_APPLICATION == lResult) { _isImmersiveProcess = false; } else if (ERROR_SUCCESS == lResult) { ApplicationUserModelId = sbName.ToString(); } else if (ERROR_INSUFFICIENT_BUFFER == lResult) { sbName = new StringBuilder((int)cchLen); if (ERROR_SUCCESS == GetApplicationUserModelId(ptrProcess, ref cchLen, sbName)) { ApplicationUserModelId = sbName.ToString(); } } CloseHandle(ptrProcess); } } else { _isImmersiveProcess = false; } } try { var arguments = GetCommandLine(processId); var arr = ParseCommandLine(arguments); if (arr.Length == 0) { } else if (arguments.Contains("\"" + arr[0] + "\"")) { result.Arguments = arguments.Replace("\"" + arr[0] + "\"", ""); } else { result.Arguments = arguments.Replace(arr[0], ""); } if (result.Arguments != null) { result.Arguments = result.Arguments.ReplaceEnvironmentVariable(); } //if (arr.Length > 0) //{ // var resultarr = new string[arr.Length - 1]; // Array.Copy(arr, 1, resultarr, 0, arr.Length - 1); // result.arguments = string.Join(" ", resultarr).replaceEnvironmentVariable(); //} } catch (Exception) { } result.ApplicationUserModelId = ApplicationUserModelId; result.IsImmersiveProcess = _isImmersiveProcess; return(result); }