From ff1b4eeea91b5823602cb84066fdb30c5745e9da Mon Sep 17 00:00:00 2001
From: Ashley <38720975+ashley-pillay@users.noreply.github.com>
Date: Tue, 23 Nov 2021 11:13:29 +0200
Subject: [PATCH] Added functions to create custom desktops
---
NtApiDotNet/Win32/Win32NativeMethods.cs | 21 ++++++++++++++++++
NtApiDotNet/Win32/Win32Utils.cs | 29 +++++++++++++++++++++++++
2 files changed, 50 insertions(+)
diff --git a/NtApiDotNet/Win32/Win32NativeMethods.cs b/NtApiDotNet/Win32/Win32NativeMethods.cs
index acada9dff..81fab2c05 100644
--- a/NtApiDotNet/Win32/Win32NativeMethods.cs
+++ b/NtApiDotNet/Win32/Win32NativeMethods.cs
@@ -1336,6 +1336,27 @@ internal static extern SafeKernelObjectHandle CreateWindowStation(
SECURITY_ATTRIBUTES lpsa
);
+ [DllImport("user32.dll", EntryPoint = "CreateDesktop", CharSet = CharSet.Unicode, SetLastError = true)]
+ internal static extern SafeKernelObjectHandle CreateDesktop(
+ string desktopName,
+ IntPtr device, // must be null.
+ IntPtr deviceMode, // must be null,
+ int flags, // use 0
+ AccessMask dwDesiredAccess,
+ SECURITY_ATTRIBUTES attributes);
+
+ [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
+ internal static extern SafeKernelObjectHandle CreateDesktopEx(
+ string desktopName,
+ IntPtr device, // must be null.
+ IntPtr deviceMode, // must be null,
+ int flags, // use 0
+ AccessMask dwDesiredAccess,
+ SECURITY_ATTRIBUTES attributes,
+ ulong heapSize,
+ IntPtr pVoid);
+
+
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern SafeKernelObjectHandle CreateRemoteThreadEx(
SafeKernelObjectHandle hProcess,
diff --git a/NtApiDotNet/Win32/Win32Utils.cs b/NtApiDotNet/Win32/Win32Utils.cs
index 4decc277c..b3b9191c1 100644
--- a/NtApiDotNet/Win32/Win32Utils.cs
+++ b/NtApiDotNet/Win32/Win32Utils.cs
@@ -525,6 +525,35 @@ public static NtWindowStation CreateWindowStation(string name)
return new NtWindowStation(handle);
}
+ ///
+ /// This creates a Desktop using the User32 API.
+ ///
+ /// The name of the Desktop.
+ /// The Desktop.
+ public static NtDesktop CreateDesktop(string name)
+ {
+ var handle = Win32NativeMethods.CreateDesktop(name, IntPtr.Zero, IntPtr.Zero, 0, WindowStationAccessRights.MaximumAllowed, null);
+ if (handle.IsInvalid)
+ throw new SafeWin32Exception();
+ return new NtDesktop(handle);
+ }
+
+ ///
+ /// This creates a Desktop using the User32 API.
+ ///
+ /// The name of the Desktop.
+ /// The size of the desktop heap, in kilobytes.
+ /// The Desktop.
+ public static NtDesktop CreateDesktop(string name, ulong heapSize)
+ {
+ var handle = Win32NativeMethods.CreateDesktopEx(name, IntPtr.Zero, IntPtr.Zero, 0, WindowStationAccessRights.MaximumAllowed, null, heapSize,
+ IntPtr.Zero);
+ if (handle.IsInvalid)
+ throw new SafeWin32Exception();
+ return new NtDesktop(handle);
+ }
+
+
///
/// Create a remote thread.
///