Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functions to create custom desktops with CreateDesktop and CreateDesktopEx #44

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions NtApiDotNet/Win32/Win32NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,27 @@ internal static extern SafeKernelObjectHandle CreateWindowStation(
SECURITY_ATTRIBUTES lpsa
);

[DllImport("user32.dll", EntryPoint = "CreateDesktop", CharSet = CharSet.Unicode, SetLastError = true)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need to specify the EntryPoint property.

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(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should just define the Ex version which is supported on Vista+, no need to specify both versions of the function.

string desktopName,
IntPtr device, // must be null.
IntPtr deviceMode, // must be null,
int flags, // use 0
AccessMask dwDesiredAccess,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use DesktopAccessRights for this type rather than AccessMask.

SECURITY_ATTRIBUTES attributes,
ulong heapSize,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the definition of the function the heapSize is ULONG, which is 32-bit in the native APIs. ulong in C# is 64bit. Use int or uint instead.

IntPtr pVoid);


[DllImport("kernel32.dll", SetLastError = true)]
internal static extern SafeKernelObjectHandle CreateRemoteThreadEx(
SafeKernelObjectHandle hProcess,
Expand Down
29 changes: 29 additions & 0 deletions NtApiDotNet/Win32/Win32Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,35 @@ public static NtWindowStation CreateWindowStation(string name)
return new NtWindowStation(handle);
}

/// <summary>
/// This creates a Desktop using the User32 API.
/// </summary>
/// <param name="name">The name of the Desktop.</param>
/// <returns>The Desktop.</returns>
public static NtDesktop CreateDesktop(string name)
{
var handle = Win32NativeMethods.CreateDesktop(name, IntPtr.Zero, IntPtr.Zero, 0, WindowStationAccessRights.MaximumAllowed, null);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call the CreateDesktop(string name, ulong heapsize) to use the Ex version of the function with the heapsize set to 0.

if (handle.IsInvalid)
throw new SafeWin32Exception();
return new NtDesktop(handle);
}

/// <summary>
/// This creates a Desktop using the User32 API.
/// </summary>
/// <param name="name">The name of the Desktop.</param>
/// <param name="heapSize">The size of the desktop heap, in kilobytes.</param>
/// <returns>The Desktop.</returns>
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);
}


/// <summary>
/// Create a remote thread.
/// </summary>
Expand Down