Пример #1
0
        private static IntPtr FindDescendantBy(IntPtr parent, string className = null, string text = null)
        {
            var matches = new List <IntPtr>();

            WindowsApi.EnumChildWindows(parent, (handle, pointer) =>
            {
                if (
                    (className == null || WindowsHelpers.GetWindowClassName(handle) == className)
                    &&
                    (text == null || WindowsHelpers.GetWindowTextRaw(handle) == text)
                    )
                {
                    matches.Add(handle);
                }

                return(true);
            }, IntPtr.Zero);

            if (matches.Count > 1)
            {
                throw new Exception($"Found {matches.Count} matching descendants.");
            }

            return(matches.FirstOrDefault());
        }
Пример #2
0
 public void Dispose()
 {
     if (!WindowsApi.SetThreadDesktop(originalDesktop))
     {
         WindowsHelpers.CheckLastError();
     }
 }
Пример #3
0
 public void Dispose()
 {
     if (!WindowsApi.CloseDesktop(Handle))
     {
         WindowsHelpers.CheckLastError();
     }
 }
Пример #4
0
        public int?GetExitCode()
        {
            if (!WindowsApi.GetExitCodeProcess(processInformation.hProcess, out var exitCode))
            {
                WindowsHelpers.CheckLastError();
            }

            return(exitCode == 259 /* STILL_ACTIVE */ ? null : (int?)exitCode);
        }
Пример #5
0
        public Desktop(string baseName)
        {
            Name = baseName + " " + DateTime.Now.Ticks.ToString(); //Ensuring this is unique since sometimes a name can't be reused later on

            Handle = WindowsApi.CreateDesktop(Name, null, null, 0, WindowsApi.ACCESS_MASK.DESKTOP_CREATEWINDOW, null);
            if (Handle == IntPtr.Zero)
            {
                WindowsHelpers.CheckLastError();
            }
        }
Пример #6
0
        public void Dispose()
        {
            if (!WindowsApi.CloseHandle(processInformation.hProcess))
            {
                WindowsHelpers.CheckLastError();
            }

            if (!WindowsApi.CloseHandle(processInformation.hThread))
            {
                WindowsHelpers.CheckLastError();
            }
        }
Пример #7
0
        /// <summary>
        /// Switches to the <paramref name="newDesktopHandle"/> desktop.
        /// </summary>
        public DesktopContext(IntPtr newDesktopHandle)
        {
            originalDesktop = WindowsApi.GetThreadDesktop(WindowsApi.GetCurrentThreadId());
            if (originalDesktop == IntPtr.Zero)
            {
                WindowsHelpers.CheckLastError();
            }

            if (!WindowsApi.SetThreadDesktop(newDesktopHandle))
            {
                WindowsHelpers.CheckLastError();
            }
        }
        private static List <IntPtr> FindDescendantsBy(IntPtr parent, string className = null, string text = null)
        {
            var matches = new List <IntPtr>();

            WindowsApi.EnumChildWindows(parent, (handle, pointer) =>
            {
                if (
                    (className == null || WindowsHelpers.GetWindowClassName(handle) == className)
                    &&
                    (text == null || WindowsHelpers.GetWindowTextRaw(handle) == text)
                    )
                {
                    matches.Add(handle);
                }

                return(true);
            }, IntPtr.Zero);

            return(matches);
        }
Пример #9
0
        public DesktopProcess(string commandLine, string desktopName)
        {
            var startupInfo = new WindowsApi.STARTUPINFO
            {
                cb        = Marshal.SizeOf <WindowsApi.STARTUPINFO>(),
                lpDesktop = desktopName,
            };

            if (!WindowsApi.CreateProcess(
                    null,
                    commandLine,
                    null,
                    null,
                    true,
                    0,
                    IntPtr.Zero,
                    null,
                    ref startupInfo,
                    out processInformation
                    ))
            {
                WindowsHelpers.CheckLastError();
            }
        }