CreateProcess function creates the process in de the default dekstop instead of creating it inside the new desktop

1

I am working on an small C# application and found a weird error. The goal of this application is to create a new Desktop and launch a process inside it.

For this purpose, I am relying on the following win32 functions:

[DllImport("user32.dll")]
        public static extern IntPtr CreateDesktop(string lpszDesktop,
            IntPtr lpszDevice,
            IntPtr pDevmode,
            uint dwFlags,
            uint dwDesiredAccess,
            IntPtr lpsa);

[DllImport("user32.dll")]
        public static extern bool SwitchDesktop(IntPtr hDesktop);

  [DllImport("kernel32.dll")]
        private static extern bool CreateProcess(
         string lpApplicationName,
         string lpCommandLine,
         IntPtr lpProcessAttributes,
         IntPtr lpThreadAttributes,
         bool bInheritHandles,
         int dwCreationFlags,
         IntPtr lpEnvironment,
         string lpCurrentDirectory,
         ref STARTUPINFO lpStartupInfo,
         ref PROCESS_INFORMATION lpProcessInformation);

Now the situation is that I can create the desktop, switch to it and finally launch correctly some applications inside the new created desktop, for example: internet explorer or Notepad++ or putty.exe etc ... but for other applications this simply does not work. i.e. Chrome, Firefox, Edge etc etc... When i try with firefox, the process starts but in the default desktop, despite the fact that i am specifying the name of the new desktop in the CreateProcess function.

All the code I am using is very straightforward:

 const int NORMAL_PRIORITY_CLASS =  0x0020;
static void Main(string[] args){
 var desktopName = "mynewdesktopinwindowsten";
            bool result = true;
            //create the desktop:
            var intHandler = CreateDesktop(desktopName, 
                IntPtr.Zero,//Reserved; must be NULL.
                IntPtr.Zero, //Reserved; must be NULL.
                0x0001, //DF_ALLOWOTHERACCOUNTHOOK  optional
                (uint)AccessRight.GENERIC_ALL,
                IntPtr.Zero);

            //Switch to the new desktop:
            result = SwitchDesktop(intHandler);
            SetThreadDesktop(intHandler);
         
            if (result)
            {
                 //launch a process inside the new desktop:
                createProcessInsideDesktop(desktopName);
            } 
}

 private static void createProcessInsideDesktop(string desktopName)
        {
            ////set startup parameters:
            STARTUPINFO si = new STARTUPINFO();
            si.cb = (uint)Marshal.SizeOf(si);
            si.lpDesktop = desktopName;
             
            PROCESS_INFORMATION pi = new PROCESS_INFORMATION();

            var filePath = @"C:\Program Files (x86)\Notepad++\notepad++.exe";
            
           
            bool resultCreateProcess = CreateProcess(
                null,
                filePath,
                IntPtr.Zero,
                IntPtr.Zero,
                true,
                NORMAL_PRIORITY_CLASS,
                IntPtr.Zero,
                null,
                ref si,
                ref pi);

              //do something to close the desktop and switch to default.......
        }

   [Flags]
    public enum AccessRight : uint
    {
        DESKTOP_READOBJECTS = 0x00000001,
        DESKTOP_CREATEWINDOW = 0x00000002,
        DESKTOP_CREATEMENU = 0x00000004,
        DESKTOP_HOOKCONTROL = 0x00000008,
        DESKTOP_JOURNALRECORD = 0x00000010,
        DESKTOP_JOURNALPLAYBACK = 0x00000020,
        DESKTOP_ENUMERATE = 0x00000040,
        DESKTOP_WRITEOBJECTS = 0x00000080,
        DESKTOP_SWITCHDESKTOP = 0x00000100,

        GENERIC_ALL = (DESKTOP_READOBJECTS | DESKTOP_CREATEWINDOW | DESKTOP_CREATEMENU |
            DESKTOP_HOOKCONTROL | DESKTOP_JOURNALRECORD | DESKTOP_JOURNALPLAYBACK |
            DESKTOP_ENUMERATE | DESKTOP_WRITEOBJECTS | DESKTOP_SWITCHDESKTOP)
    };

the code above works fine with Notepad++ but when I change this line. lets say I want to launch Firefox: var filePath = @"C:\Program Files\Mozilla Firefox\firefox.exe"; the process is not created inside the new desktop but in the Default desktop. So please if anyone can spot some important thing here to overcome the issue it would be appreciated.

c#
windows
winapi
createprocess
asked on Stack Overflow Feb 12, 2021 by vhakti

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0