Skip to main content

CreateDesktop

Definitions

HWINSTA NtUserGetProcessWindowStation(VOID)
HDESK NtUserCreateDesktopEx(POBJECT_ATTRIBUTES ObjectAttributes, PUNICODE_STRING DeviceName, PDEVMODEW DeviceMode, DWORD Flags, ACCESS_MASK DesiredAccess, ULONG HeapSize);

Function

HDESK CreateDesktopNt(IN CONST PUNICODE_STRING DesktopName, 
IN CONST PUNICODE_STRING DeviceName,
IN CONST PDEVMODEW DeviceMode,
IN CONST DWORD Flags,
IN CONST ACCESS_MASK DesiredAccess,
IN OPTIONAL CONST LPSECURITY_ATTRIBUTES SecurityAttributes,
IN CONST ULONG HeapSize)
{
//
// The calling process must be associated with a window station to create a desktop
//

CONST HWINSTA ProcessWindowStation{ NtUserGetProcessWindowStation() };

if (ProcessWindowStation == nullptr)
{
return nullptr;
}

ULONG Attributes{ OBJ_OPENIF | OBJ_CASE_INSENSITIVE };

if (SecurityAttributes != nullptr && SecurityAttributes->bInheritHandle == TRUE)
{
Attributes |= OBJ_INHERIT;
}

LPVOID SecurityDescriptor{};

if (SecurityAttributes)
{
SecurityDescriptor = SecurityAttributes->lpSecurityDescriptor;
}

OBJECT_ATTRIBUTES DesktopAttributes{};

InitializeObjectAttributes(&DesktopAttributes, DesktopName, Attributes, ProcessWindowStation, SecurityDescriptor)

return NtUserCreateDesktopEx(&DesktopAttributes, DeviceName, DeviceMode, Flags, DesiredAccess, HeapSize);
}