Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

CreateDesktop (user32)
 
.
Summary

C# Signature:

     // ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.WIN32COM.v10.en/dllproc/base/createdesktop.htm
    [DllImport("user32.dll", EntryPoint="CreateDesktop", CharSet=CharSet.Unicode, SetLastError=true)]
    public static extern IntPtr CreateDesktop(
                    [MarshalAs(UnmanagedType.LPWStr)] string desktopName,
                    [MarshalAs(UnmanagedType.LPWStr)] string device, // must be null.
                    [MarshalAs(UnmanagedType.LPWStr)] string deviceMode, // must be null,
                    [MarshalAs(UnmanagedType.U4)] int flags,  // use 0
                    [MarshalAs(UnmanagedType.U4)] ACCESS_MASK accessMask,
                    [MarshalAs(UnmanagedType.LPStruct)] SECURITY_ATTRIBUTES attributes);


    // ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.WIN32COM.v10.en/dllproc/base/closedesktop.htm
    [DllImport("user32.dll", EntryPoint="CloseDesktop", CharSet =  CharSet.Unicode, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool CloseDesktop(IntPtr handle);

VB.NET Signature:

    ' ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.WIN32COM.v10.en/dllproc/base/createdesktop.htm
    <DllImport("user32.dll", EntryPoint:="CreateDesktop", CharSet:=CharSet.Unicode, SetLastError:=True)> _
    Public Shared Function CreateDesktop(ByVal desktopName As String, ByVal device As String,
                           ByVal deviceMode As String, ByVal flags As Integer,
                           ByVal accessMask As ACCESS_MASK, ByVal attributes As SECURITY_ATTRIBUTES) As IntPtr
    End Function

C# Signature:

       // ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.WIN32COM.v10.en/dllproc/base/createdesktop.htm
    [DllImport("user32.dll", EntryPoint = "CreateDesktop", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern IntPtr CreateDesktop(
        string desktopName,
        string device, // must be null.
        string deviceMode, // must be null,
        int flags,  // use 0
        ACCESS_MASK accessMask,
        ref SECURITY_ATTRIBUTES attributes);

    ' ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.WIN32COM.v10.en/dllproc/base/closedesktop.htm
    <DllImport("user32.dll", EntryPoint:="CloseDesktop", CharSet:=CharSet.Unicode, SetLastError:=True)> _
    Public Shared Function CloseDesktop(ByVal handle As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean

VB.NET Signature:

    ' ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.WIN32COM.v10.en/dllproc/base/createdesktop.htm
    <DllImport("user32.dll", EntryPoint:="CreateDesktop", CharSet:=CharSet.Unicode, SetLastError:=True)> _
    Public Shared Function CreateDesktop(ByVal desktopName As String, ByVal device As String,
                           ByVal deviceMode As String, ByVal flags As Integer,
                           ByVal accessMask As ACCESS_MASK, ByVal attributes As SECURITY_ATTRIBUTES) As IntPtr
    End Function

User-Defined Types:

ACCESS_MASK

SECURITY_ATTRIBUTES

Notes:

    ' ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.WIN32COM.v10.en/dllproc/base/closedesktop.htm
    <DllImport("user32.dll", EntryPoint:="CloseDesktop", CharSet:=CharSet.Unicode, SetLastError:=True)> _
    Public Shared Function CloseDesktop(ByVal handle As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

User-Defined Types:

ACCESS_MASK

SECURITY_ATTRIBUTES

If you come across this error "System.Runtime.InteropServices.MarshalDirectiveException: Cannot marshal 'parameter #6': Invalid managed/unmanaged type combination (this value type must be paired with Struct).", you may want to use

Notes:

    static extern IntPtr CreateDesktop(..., [In] ref SECURITY_ATTRIBUTES lpsa);

If you come across this error "System.Runtime.InteropServices.MarshalDirectiveException: Cannot marshal 'parameter #6': Invalid managed/unmanaged type combination (this value type must be paired with Struct).", you may want to use

instead of

    static extern IntPtr CreateDesktop(..., [In] ref SECURITY_ATTRIBUTES lpsa);

    [MarshalAs(UnmanagedType.LPStruct)] SECURITY_ATTRIBUTES attributes);

instead of

cf. http://stackoverflow.com/questions/1434363/pinvoke-createdesktop

    [MarshalAs(UnmanagedType.LPStruct)] SECURITY_ATTRIBUTES attributes);

Tips & Tricks:

DesktopClass on dotnet-snippets.de

cf. http://stackoverflow.com/questions/1434363/pinvoke-createdesktop

Sample Code:

public class SafeDesktopHandle : BaseSafeHandle
    {
    public SafeDesktopHandle(IntPtr handle, bool ownsHandle)
        : base(handle, ownsHandle)
    { }

Tips & Tricks:

DesktopClass on dotnet-snippets.de

    protected override bool CloseNativeHandle(IntPtr handle)
    {
        return WindowStationAndDesktop.CloseDesktop(handle);
    }
    }
[Flags]
    internal enum ACCESS_MASK : uint

Sample Code:

public class SafeDesktopHandle : BaseSafeHandle
    {
    DESKTOP_NONE        = 0,
    DESKTOP_READOBJECTS     = 0x0001,
    DESKTOP_CREATEWINDOW    = 0x0002,
    DESKTOP_CREATEMENU      = 0x0004,
    DESKTOP_HOOKCONTROL     = 0x0008,
    DESKTOP_JOURNALRECORD       = 0x0010,
    DESKTOP_JOURNALPLAYBACK     = 0x0020,
    DESKTOP_ENUMERATE       = 0x0040,
    DESKTOP_WRITEOBJECTS    = 0x0080,
    DESKTOP_SWITCHDESKTOP       = 0x0100,
    public SafeDesktopHandle(IntPtr handle, bool ownsHandle)
        : base(handle, ownsHandle)
    { }

    GENERIC_ALL         = ( DESKTOP_READOBJECTS | DESKTOP_CREATEWINDOW | DESKTOP_CREATEMENU |
                    DESKTOP_HOOKCONTROL | DESKTOP_JOURNALRECORD | DESKTOP_JOURNALPLAYBACK |
                    DESKTOP_ENUMERATE | DESKTOP_WRITEOBJECTS | DESKTOP_SWITCHDESKTOP |
                    STANDARD_ACCESS.STANDARD_RIGHTS_REQUIRED),
    protected override bool CloseNativeHandle(IntPtr handle)
    {
        return WindowStationAndDesktop.CloseDesktop(handle);
    }
    }
[Flags]
    internal enum ACCESS_MASK : uint
    {
    DESKTOP_NONE        = 0,
    DESKTOP_READOBJECTS     = 0x0001,
    DESKTOP_CREATEWINDOW    = 0x0002,
    DESKTOP_CREATEMENU      = 0x0004,
    DESKTOP_HOOKCONTROL     = 0x0008,
    DESKTOP_JOURNALRECORD       = 0x0010,
    DESKTOP_JOURNALPLAYBACK     = 0x0020,
    DESKTOP_ENUMERATE       = 0x0040,
    DESKTOP_WRITEOBJECTS    = 0x0080,
    DESKTOP_SWITCHDESKTOP       = 0x0100,

    GENERIC_ALL         = ( DESKTOP_READOBJECTS | DESKTOP_CREATEWINDOW | DESKTOP_CREATEMENU |
                    DESKTOP_HOOKCONTROL | DESKTOP_JOURNALRECORD | DESKTOP_JOURNALPLAYBACK |
                    DESKTOP_ENUMERATE | DESKTOP_WRITEOBJECTS | DESKTOP_SWITCHDESKTOP |
                    STANDARD_ACCESS.STANDARD_RIGHTS_REQUIRED),
    }

Alternative Managed API:

Do you know one? Please contribute it!

Alternative Managed API:

Do you know one? Please contribute it!

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions