none
CreateDesktop() function in powershell ? RRS feed

  • Question

  • I'd like to learn how to use these:
    http://msdn.microsoft.com/en-us/library/windows/desktop/ms682124%28v=vs.85%29.aspx

    Inside windows powershell. I learned the other day how to create a shell object and use shell.application to mess around with some methods of the windows shell, but there are other things like createdesktop() that don't have easily found answers.
    windows 7 - powershell 2.0

    Thursday, March 1, 2012 3:54 PM

Answers

  • Add-Type "
    	public 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,
    		STANDARD_RIGHTS_REQUIRED = 0x000f0000,
    
    	    GENERIC_ALL         = ( DESKTOP_READOBJECTS | DESKTOP_CREATEWINDOW | DESKTOP_CREATEMENU |
    	                    DESKTOP_HOOKCONTROL | DESKTOP_JOURNALRECORD | DESKTOP_JOURNALPLAYBACK |
    	                    DESKTOP_ENUMERATE | DESKTOP_WRITEOBJECTS | DESKTOP_SWITCHDESKTOP |
    	                    STANDARD_RIGHTS_REQUIRED),
        }
    "
    
    
    $code = @"
    	[StructLayout(LayoutKind.Sequential)]
        public struct SECURITY_ATTRIBUTES
        {
            public int nLength;
            public IntPtr lpSecurityDescriptor;
            public int bInheritHandle;
        }
    
    	[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, 
                        [MarshalAs(UnmanagedType.LPWStr)] string deviceMode, 
                        [MarshalAs(UnmanagedType.U4)] int flags,  
                        [MarshalAs(UnmanagedType.U4)] int accessMask,
                        [In] ref SECURITY_ATTRIBUTES lpsa);
    
        [DllImport("user32.dll", EntryPoint="CloseDesktop", CharSet =  CharSet.Unicode, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool CloseDesktop(IntPtr handle);
    "@
    
    Add-Type -MemberDefinition $code -Name Desktop -Namespace System
    
    $name = "NewDesktop"
    $sec = New-Object Desktop+SECURITY_ATTRIBUTES
    
    $dhandle = [Desktop]::CreateDesktop($name,$null,$null,0,[ACCESS_MASK]::DESKTOP_CREATEWINDOW,[ref]$sec)
    [Desktop]::CloseDesktop($dhandle)

    • Marked as answer by WhiteBabylon Thursday, March 1, 2012 4:33 PM
    Thursday, March 1, 2012 4:18 PM

All replies

  • I could be incorrect, but, I want to say I recall something from the Windows Internals 5th edition mentioning that these could not be created from user mode.  So, unless you are executing from kernel, those are inaccessible.  Others...please correct me if I am in error.
    Thursday, March 1, 2012 4:01 PM
  • Sorry, I seemed to have missed the question.  The article referred to is in C.  C => Powershell?  Have fun!

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)


    • Edited by Bigteddy Thursday, March 1, 2012 4:13 PM
    Thursday, March 1, 2012 4:11 PM
  • Add-Type "
    	public 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,
    		STANDARD_RIGHTS_REQUIRED = 0x000f0000,
    
    	    GENERIC_ALL         = ( DESKTOP_READOBJECTS | DESKTOP_CREATEWINDOW | DESKTOP_CREATEMENU |
    	                    DESKTOP_HOOKCONTROL | DESKTOP_JOURNALRECORD | DESKTOP_JOURNALPLAYBACK |
    	                    DESKTOP_ENUMERATE | DESKTOP_WRITEOBJECTS | DESKTOP_SWITCHDESKTOP |
    	                    STANDARD_RIGHTS_REQUIRED),
        }
    "
    
    
    $code = @"
    	[StructLayout(LayoutKind.Sequential)]
        public struct SECURITY_ATTRIBUTES
        {
            public int nLength;
            public IntPtr lpSecurityDescriptor;
            public int bInheritHandle;
        }
    
    	[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, 
                        [MarshalAs(UnmanagedType.LPWStr)] string deviceMode, 
                        [MarshalAs(UnmanagedType.U4)] int flags,  
                        [MarshalAs(UnmanagedType.U4)] int accessMask,
                        [In] ref SECURITY_ATTRIBUTES lpsa);
    
        [DllImport("user32.dll", EntryPoint="CloseDesktop", CharSet =  CharSet.Unicode, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool CloseDesktop(IntPtr handle);
    "@
    
    Add-Type -MemberDefinition $code -Name Desktop -Namespace System
    
    $name = "NewDesktop"
    $sec = New-Object Desktop+SECURITY_ATTRIBUTES
    
    $dhandle = [Desktop]::CreateDesktop($name,$null,$null,0,[ACCESS_MASK]::DESKTOP_CREATEWINDOW,[ref]$sec)
    [Desktop]::CloseDesktop($dhandle)

    • Marked as answer by WhiteBabylon Thursday, March 1, 2012 4:33 PM
    Thursday, March 1, 2012 4:18 PM
  • I don't seem to understand then. I thought powershell was capable of doing everything that windows 7 does graphically except through the command line. Like how windows creates a desktop object and generates icons, a task bar, start menu, system tray etc. I wanted to attempt making my own UI with the command line by using the same things explorer.exe is using. Is this just too absurd?
    Thursday, March 1, 2012 4:21 PM
  • That's amazing. How do I LEARN this? So I can do other functions/methods etc inside user32 and other dlls?

    Thursday, March 1, 2012 4:27 PM
  • It's called P/Invoke. Here's a good article to get your started: http://jrich523.wordpress.com/2012/02/06/powershell-getting-started-with-win32-api/  This allows you to use unmanaged code (normally not accessible in Powershell which is .NET (managed)).
    • Edited by Will Steele Thursday, March 1, 2012 4:50 PM extra
    Thursday, March 1, 2012 4:50 PM
  • yes and no...
     
    not to be mean, but its probably a bit much for you to tackle, im pretty
    experienced in powershell and im ok in C/C# and I wouldn’t attempt it..
     
    however you could replace your UI with just straight powershell :)
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, March 1, 2012 5:44 PM
  • did you just write that or did you find it some place?
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, March 1, 2012 5:44 PM
  • I agree with Justin.  That code is just plain crazy.  You need to be an experienced C developer to produce something like that.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Thursday, March 1, 2012 5:46 PM
  • I'm all about ambitious efforts.  So, to be sure we're on the same page, check out what Desktops are in the shell context:

    http://msdn.microsoft.com/en-us/library/windows/desktop/ms682573(v=vs.85).aspx

    Thursday, March 1, 2012 5:48 PM
  • Yes, thanks for that article. I wasn't aware that log-on and screensaver was a type of 'desktop'. I read the blog you posted as well, and I tried to produce OPENDESKTOP, because the awesome code posted above, I think creates a desktop, but I can't 'view' it, or I'm just confused.
    In an effort to attempt it like the blog describes, I came up with this:
      public static extern IntPtr OpenDesktop(
                [MarshalAs(UnmanagedType.LPWStr)] string DesktopName,
                [MarshalAs(UnmanagedType.U4)] Flags,
                bool Inherit,
                [MarshalAs(UnmanagedType.U4)] Access
            );
    But I got about 100 errors lol.
    I have been playing around with WASP and SHOWUI for powershell and have came up with a seizure inducing background, with a 'list' of buttons for my desktop icons, a control for minimizing maximizing windows (buttons on the task bar) and some temporarily-broken RAM, disk space and CPU % meters on the side.
    If you'd like to see a very poor quality-not-very-educating video of what i've got so-far:
    http://www.youtube.com/watch?v=zVe-hwLWmCo&context=C3816107ADOEgsToPDskJFfz9VYDyq9QVu4GDTInMf

    I'm in my 3rd year of school and every day my mind is blown attempting this crazy stuff in powershell. I don't learn nearly anything as hard/cool in school. It is very hard but that's why it's been keeping me hooked. One problem I've had is knowing what to even research, this topic has given me the biggest boost thus far in terms of what-is-this and where-am-I.


    Thursday, March 1, 2012 6:22 PM
  • If anyone has more reading on this stuff, even if it's extremely hard, every line of code leads me to discover more things, even if it isn't direct or sudden. Please post even if you don't think I'll be able to understand it.
    Thursday, March 1, 2012 6:26 PM
  • Jumping in and trying to learn things along the way is good.  The topic you picked is just a pretty deep one.  It really gets into, as BigTeddy alluded, C++, which is one of the languages used to build the GUI's you see.  One of the challenges for folks new to Windows is realizing there are all sorts of levels to this one single OS you see when you pop on your machine.  Powershell is great for giving scriptable shell access to the OS, but, if you want to write OS code itself, Powershell is really not the right too.  To get into OS development the language to focus on, with a few exceptions (others correct me if I am wrong) are C++, C and ASM with the .NET languages tagged on for a little bit here and there.  This kind of conversation could go a LOT of directions.  Powershell, specifically, is a great tool to introduce you to how to manage a machine (or many machines) as well as get a little familiar with .NET.  But, to get into OS level stuff you need to look a lot deeper.  Shell is a COM interface for the Windows API's...  One analogy I heard recently to explain this term shell is to think of the ring-model of architecture.  In the core (ring 0) is the middle of the system.  Outside of that is a "shell".  As in kernel of corn with a shell outside.  The shell is a user mode interface to access to core.  This is a grossly oversimplified explanation, but, I wanted to highlight where Powershell fits in the grand scheme of things. This may help explain why the Desktops question, from a Powershell perspective, is hard to fathom.
    Thursday, March 1, 2012 6:35 PM
  • I find that its typically easier (in the case of win32) to create a c# app
    and get it working and then port it... you probably want to line up all the
    tools (win32 api's /namespaces) that you'll need and start to knock them
    off... pinvoke might have some of them ported for you...
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, March 1, 2012 6:36 PM
  • I find that its typically easier (in the case of win32) to create a c# app
    and get it working and then port it... you probably want to line up all the
    tools (win32 api's /namespaces) that you'll need and start to knock them
    off... pinvoke might have some of them ported for you...
    http://pinvoke.net/
    Thursday, March 1, 2012 6:38 PM