Beispiel #1
0
        public static IEnumerable <IntPtr> GetDesktopWindowHandles(IntPtr desktopHandle)
        {
            var handles = new List <IntPtr>();

            WindowsApi.EnumDesktopWindows(desktopHandle, (d, p) => { handles.Add(d); return(true); }, IntPtr.Zero);
            return(handles);
        }
Beispiel #2
0
        public static WindowsApi.PROCESS_INFORMATION CreateProcess(ProcessStartInfo startInfo, string desktopName = null, int?millisecondsToWait = 100)
        {
            var startupInfo = new WindowsApi.STARTUPINFO();

            startupInfo.cb        = Marshal.SizeOf(startupInfo);
            startupInfo.lpDesktop = desktopName;

            var processInformation = new WindowsApi.PROCESS_INFORMATION();

            bool result = WindowsApi.CreateProcess(startInfo.FileName,
                                                   startInfo.Arguments,
                                                   IntPtr.Zero,
                                                   IntPtr.Zero,
                                                   true,
                                                   WindowsApi.NORMAL_PRIORITY_CLASS,
                                                   IntPtr.Zero,
                                                   startInfo.WorkingDirectory,
                                                   ref startupInfo,
                                                   ref processInformation);

            if (result)
            {
                if (millisecondsToWait.HasValue)
                {
                    WindowsApi.WaitForInputIdle(processInformation.hProcess, (uint)millisecondsToWait.Value);
                }

                WindowsApi.CloseHandle(processInformation.hThread);
                return(processInformation);
            }

            return(new WindowsApi.PROCESS_INFORMATION());
        }
        private void AssociateWithJob(WindowsApi.PROCESS_INFORMATION processInformation, bool throwOnError)
        {
            if (_jobHandle == IntPtr.Zero)
            {
                lock (_jobHandleLock)
                {
                    if (_jobHandle == IntPtr.Zero)
                    {
                        _jobHandle = WindowsApi.CreateJobObject(IntPtr.Zero, null);

                        var basic = new WindowsApi.JOBOBJECT_BASIC_LIMIT_INFORMATION {LimitFlags = WindowsApi.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE};
                        var extended = new WindowsApi.JOBOBJECT_EXTENDED_LIMIT_INFORMATION {BasicLimitInformation = basic};

                        int length = Marshal.SizeOf(typeof (WindowsApi.JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
                        IntPtr extendedPointer = Marshal.AllocHGlobal(length);
                        Marshal.StructureToPtr(extended, extendedPointer, false);

                        if (!WindowsApi.SetInformationJobObject(_jobHandle, WindowsApi.JOBOBJECTINFOCLASS.ExtendedLimitInformation, extendedPointer, (uint) length))
                        {
                            throw new TorSharpException($"Unable to set information on the job object. Error: {WindowsUtility.GetLastErrorMessage()}.");
                        }
                    }
                }
            }

            lock (_processIdsLock)
            {
                _processIds.Add(processInformation.dwProcessId);
            }

            if (!WindowsApi.AssignProcessToJobObject(_jobHandle, processInformation.hProcess) && throwOnError)
            {
                throw new TorSharpException($"Unable to assign the process to the job object. Error: {WindowsUtility.GetLastErrorMessage()}.");
            }
        }
Beispiel #4
0
        public static IEnumerable <string> GetDesktopNames(IntPtr windowStationHandle)
        {
            var names = new List <string>();

            WindowsApi.EnumDesktops(windowStationHandle, (n, p) => { names.Add(n); return(true); }, IntPtr.Zero);
            return(names);
        }
Beispiel #5
0
        public static string GetWindowText(IntPtr windowHandle)
        {
            int length        = WindowsApi.GetWindowTextLength(windowHandle) + 1;
            var stringBuilder = new StringBuilder(length);

            WindowsApi.GetWindowText(windowHandle, stringBuilder, length);
            return(stringBuilder.ToString());
        }
Beispiel #6
0
        public static string GetDesktopName(IntPtr desktopHandle)
        {
            uint length;

            WindowsApi.GetUserObjectInformation(desktopHandle, WindowsApi.UOI_NAME, null, 0, out length);
            var stringBuilder = new StringBuilder((int)length + 1);

            WindowsApi.GetUserObjectInformation(desktopHandle, WindowsApi.UOI_NAME, stringBuilder, length, out length);
            return(stringBuilder.ToString());
        }
Beispiel #7
0
        public static IEnumerable <IntPtr> GetWindowHandles(int processId)
        {
            var handles = new List <IntPtr>();

            foreach (var thread in Process.GetProcessById(processId).Threads.OfType <ProcessThread>())
            {
                WindowsApi.EnumThreadWindows(thread.Id, (h, p) => { handles.Add(h); return(true); }, IntPtr.Zero);
            }

            return(handles);
        }
Beispiel #8
0
        public static WindowsApi.PROCESS_INFORMATION CreateProcess(ITorSharpProxy proxy, ProcessStartInfo startInfo, string desktopName = null, int?millisecondsToWait = 100)
        {
            // Source: https://stackoverflow.com/a/59387440/3286975
            var startupInfo = new WindowsApi.STARTUPINFO();

            startupInfo.cb        = Marshal.SizeOf(startupInfo);
            startupInfo.lpDesktop = desktopName;

            var processInformation = new WindowsApi.PROCESS_INFORMATION();

            string command = startInfo.FileName + " " + startInfo.Arguments;

            WindowsApi.SECURITY_ATTRIBUTES saAttr = new WindowsApi.SECURITY_ATTRIBUTES
            {
                nLength              = (uint)Marshal.SizeOf(typeof(WindowsApi.SECURITY_ATTRIBUTES)),
                bInheritHandle       = 0x1,
                lpSecurityDescriptor = IntPtr.Zero
            };

            WindowsApi.CreatePipe(ref out_read, ref out_write, ref saAttr, 0);
            WindowsApi.CreatePipe(ref err_read, ref err_write, ref saAttr, 0);

            WindowsApi.SetHandleInformation(out_read, HANDLE_FLAG_INHERIT, 0);
            WindowsApi.SetHandleInformation(err_read, HANDLE_FLAG_INHERIT, 0);

            bool result = WindowsApi.CreateProcess(null,
                                                   command,
                                                   IntPtr.Zero,
                                                   IntPtr.Zero,
                                                   true,
                                                   WindowsApi.NORMAL_PRIORITY_CLASS,
                                                   IntPtr.Zero,
                                                   startInfo.WorkingDirectory,
                                                   ref startupInfo,
                                                   ref processInformation);

            var thread = new Thread(() => RedirectStd(proxy));

            thread.Start();

            if (result)
            {
                if (millisecondsToWait.HasValue)
                {
                    WindowsApi.WaitForInputIdle(processInformation.hProcess, (uint)millisecondsToWait.Value);
                }

                WindowsApi.CloseHandle(processInformation.hThread);
                return(processInformation);
            }

            return(new WindowsApi.PROCESS_INFORMATION());
        }
Beispiel #9
0
        public static void HideWindow(IntPtr windowHandle)
        {
            // Source: http://stackoverflow.com/a/7219089/52749
            long style = WindowsApi.GetWindowLong(windowHandle, WindowsApi.GWL_STYLE);

            style &= ~(WindowsApi.WS_VISIBLE);
            style |= WindowsApi.WS_EX_TOOLWINDOW;
            style &= ~(WindowsApi.WS_EX_APPWINDOW);
            WindowsApi.ShowWindow(windowHandle, WindowsApi.SW_HIDE);
            WindowsApi.SetWindowLong(windowHandle, WindowsApi.GWL_STYLE, (int)style);
            WindowsApi.ShowWindow(windowHandle, WindowsApi.SW_SHOW);
            WindowsApi.ShowWindow(windowHandle, WindowsApi.SW_HIDE);
        }
Beispiel #10
0
        public static IEnumerable <uint> GetDesktopProcessIds(IntPtr desktopHandle)
        {
            var pids = new HashSet <uint>();

            foreach (var windowHandle in GetDesktopWindowHandles(desktopHandle))
            {
                uint pid;
                WindowsApi.GetWindowThreadProcessId(windowHandle, out pid);
                pids.Add(pid);
            }

            return(pids);
        }
Beispiel #11
0
        private static void RedirectStd(ITorSharpProxy proxy)
        {
            // Source: https://stackoverflow.com/a/59387440/3286975

            byte[] out_buf = new byte[BUFSIZE];
            byte[] err_buf = new byte[BUFSIZE];

            int dwRead = 0;

            string out_str = "";
            string err_str = "";

            bool isOutputSet = proxy.GetHandler(true) != null;
            bool isErrorSet  = proxy.GetHandler(false) != null;

            while (true)
            {
                bool bSuccess;

                if (isOutputSet)
                {
                    bSuccess = WindowsApi.ReadFile(out_read, out_buf, BUFSIZE, ref dwRead, IntPtr.Zero);
                    if (!bSuccess || dwRead == 0)
                    {
                        break;
                    }

                    out_str += System.Text.Encoding.Default.GetString(out_buf);
                    out_str  = PushCallback(proxy, out_str, true);
                }

                if (isErrorSet)
                {
                    bSuccess = WindowsApi.ReadFile(out_read, err_buf, BUFSIZE, ref dwRead, IntPtr.Zero);
                    if (!bSuccess || dwRead == 0)
                    {
                        break;
                    }

                    err_str += System.Text.Encoding.Default.GetString(err_buf);
                    err_str  = PushCallback(proxy, err_str, true);
                }
            }

            WindowsApi.CloseHandle(out_read);
            WindowsApi.CloseHandle(err_read);
            WindowsApi.CloseHandle(out_write);
            WindowsApi.CloseHandle(err_write);
        }
Beispiel #12
0
 public static IntPtr OpenDesktop(string desktopName)
 {
     return(WindowsApi.OpenDesktop(desktopName, 0, true, WindowsApi.GENERIC_ALL));
 }
Beispiel #13
0
 public static IntPtr CreateDesktop(string desktopName)
 {
     return(WindowsApi.CreateDesktop(desktopName, IntPtr.Zero, IntPtr.Zero, 0, WindowsApi.GENERIC_ALL, IntPtr.Zero));
 }
Beispiel #14
0
        public static IEnumerable <string> GetDesktopNames()
        {
            var windowStationHandle = WindowsApi.GetProcessWindowStation();

            return(GetDesktopNames(windowStationHandle));
        }
Beispiel #15
0
 public static IntPtr OpenInputDesktop()
 {
     return(WindowsApi.OpenInputDesktop(0, true, WindowsApi.GENERIC_ALL));
 }