/// <summary> /// Get the desktop for a thread. /// </summary> /// <param name="thread_id">The thread ID of the thread.</param> /// <param name="throw_on_error">True to throw on error.</param> /// <returns>The desktop result.</returns> public static NtResult <NtDesktop> GetThreadDesktop(int thread_id, bool throw_on_error) { var handle = NtSystemCalls.NtUserGetThreadDesktop(thread_id); if (handle == IntPtr.Zero) { return(NtObjectUtils.CreateResultFromDosError <NtDesktop>(Marshal.GetLastWin32Error(), throw_on_error)); } return(new NtDesktop(new SafeKernelObjectHandle(handle, false)).CreateResult()); }
/// <summary> /// Open a window station by name. /// </summary> /// <param name="object_attributes">The object attributes for opening.</param> /// <param name="desired_access">Desired access.</param> /// <param name="throw_on_error">True to throw on error.</param> /// <returns>The instance of the window station</returns> /// <exception cref="NtException">Thrown on error.</exception> public static NtResult <NtWindowStation> Open(ObjectAttributes object_attributes, WindowStationAccessRights desired_access, bool throw_on_error) { SafeKernelObjectHandle handle = NtSystemCalls.NtUserOpenWindowStation(object_attributes, desired_access); if (handle.IsInvalid) { return(NtObjectUtils.CreateResultFromDosError <NtWindowStation>(Marshal.GetLastWin32Error(), throw_on_error)); } return(new NtResult <NtWindowStation>(NtStatus.STATUS_SUCCESS, new NtWindowStation(handle))); }
/// <summary> /// Open the current process Window Station. /// </summary> /// <param name="throw_on_error">True to throw on error.</param> /// <returns>The instance of the window station</returns> /// <remarks>The returned object is no owned by the caller.</remarks> /// <exception cref="NtException">Thrown on error.</exception> public static NtResult <NtWindowStation> OpenCurrent(bool throw_on_error) { var handle = NtSystemCalls.NtUserGetProcessWindowStation(); if (handle == IntPtr.Zero) { return(NtObjectUtils.CreateResultFromDosError <NtWindowStation>(Marshal.GetLastWin32Error(), throw_on_error)); } return(new NtResult <NtWindowStation>(NtStatus.STATUS_SUCCESS, new NtWindowStation(new SafeKernelObjectHandle(handle, false)))); }
/// <summary> /// Create a new desktop. /// </summary> /// <param name="object_attributes">The object attributes for opening.</param> /// <param name="flags">Flags for opening the desktop.</param> /// <param name="desired_access">Desired access.</param> /// <param name="throw_on_error">True to throw on error.</param> /// <param name="device">Device name.</param> /// <param name="dev_mode">Device mode.</param> /// <param name="heap_size">Heap size.</param> /// <returns>An instance of NtDesktop.</returns> public static NtResult <NtDesktop> Create(ObjectAttributes object_attributes, string device, IntPtr dev_mode, CreateDesktopFlags flags, DesktopAccessRights desired_access, int heap_size, bool throw_on_error) { SafeKernelObjectHandle handle = NtSystemCalls.NtUserCreateDesktopEx(object_attributes, device == null ? null : new UnicodeString(device), dev_mode, flags, desired_access, heap_size); if (handle.IsInvalid) { return(NtObjectUtils.CreateResultFromDosError <NtDesktop>(Marshal.GetLastWin32Error(), throw_on_error)); } return(new NtResult <NtDesktop>(NtStatus.STATUS_SUCCESS, new NtDesktop(handle))); }
private NtResult <string> GetClassName(bool real_name, bool throw_on_error) { using (var str = new UnicodeStringAllocated()) { int length = NtSystemCalls.NtUserGetClassName(Handle, real_name, str); if (length == 0) { return(NtObjectUtils.CreateResultFromDosError <string>(throw_on_error)); } str.String.Length = (ushort)(length * 2); return(str.ToString().CreateResult()); } }
/// <summary> /// Create a Window Station by name. /// </summary> /// <param name="object_attributes">Object attributes for the Window Station.</param> /// <param name="desired_access">Desired access for the Window Station.</param> /// <param name="kbd_dll_path">Path to Keyboard DLL e.g. kbusa.dll.</param> /// <param name="keyboard_locale">Locale ID, e.g. 0x4090409.</param> /// <param name="language_id">Language ID e.g. 0x409.</param> /// <param name="throw_on_error">True to throw on error.</param> /// <returns>The Window Station.</returns> public static NtResult <NtWindowStation> Create(ObjectAttributes object_attributes, WindowStationAccessRights desired_access, string kbd_dll_path, int language_id, int keyboard_locale, bool throw_on_error) { string dll_path; IntPtr layout_offset; IntPtr nls_offset; using (var kbd_dll = SafeLoadLibraryHandle.LoadLibrary(kbd_dll_path, LoadLibraryFlags.None, throw_on_error)) { if (!kbd_dll.IsSuccess) { return(kbd_dll.Cast <NtWindowStation>()); } dll_path = kbd_dll.Result.FullPath; layout_offset = GetKdbLayoutOffset(kbd_dll.Result, 1); nls_offset = GetKdbLayoutOffset(kbd_dll.Result, 2); } using (var buffer = new SafeHGlobalBuffer(0x318)) { BufferUtils.FillBuffer(buffer, 0); using (var file = NtFile.Open(NtFileUtils.DosFileNameToNt(dll_path), null, FileAccessRights.GenericRead | FileAccessRights.Synchronize, FileShareMode.Read | FileShareMode.Delete, FileOpenOptions.NonDirectoryFile | FileOpenOptions.SynchronousIoNonAlert, throw_on_error)) { if (!file.IsSuccess) { return(file.Cast <NtWindowStation>()); } var handle = NtSystemCalls.NtUserCreateWindowStation(object_attributes, desired_access, file.Result.Handle, layout_offset, nls_offset, buffer, new UnicodeString($"{language_id:X08}"), keyboard_locale); if (handle.IsInvalid) { return(NtObjectUtils.CreateResultFromDosError <NtWindowStation>(throw_on_error)); } return(new NtWindowStation(handle).CreateResult()); } } }