How to open another interactive windows session using C#

Aditya Sharma 1 Reputation point
2023-03-25T20:37:42.7833333+00:00

Team,

I am looking for a solution to open another interactive windows session for existing logged in user, once the interactive windows session opens, it should display logon screen and once the user has logged in, it should display a new desktop with some application (for e.g. Notepad) running.

I came through the below article that talks about windows station, session and desktop however from a code perspective not sure how to implement.

https://techcommunity.microsoft.com/t5/ask-the-performance-team/sessions-desktops-and-windows-stations/ba-p/37247

Appreciate your help and support on this topic !!!

Windows development | Windows API - Win32
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

2 answers

Sort by: Most helpful
  1. Sedat SALMAN 14,280 Reputation points MVP Volunteer Moderator
    2023-03-25T20:51:09.06+00:00

    you have multiple options you can use APIs

    to open another interactive Windows session for an existing logged-in user, you will need to use the Windows Station, Session, and Desktop APIs.

    • Create a new Windows station using the CreateWindowStation API.
    • Create a new session using the WTSStartRemoteSession API.
    • Connect to the new session using the WTSConnectSession API.
    • Create a new desktop using the CreateDesktop API.
    • Set the input desktop for the new session using the SetThreadDesktop API.
    • Start the application you want to run in the new session, for example, Notepad.
    • Display the logon screen by calling the LockWorkStation API.

    or use third party tools

    for example autoit


  2. Sedat SALMAN 14,280 Reputation points MVP Volunteer Moderator
    2023-03-26T08:07:02.8466667+00:00

    yes all the examples are C++ but
    you can use the P/Invoke feature in C# to call the native Windows functions.

    one example

    
    using System;
    using System.Runtime.InteropServices;
    using System.Diagnostics;
    
    namespace NewDesktop
    {
        class Program
        {
            [DllImport("user32.dll", SetLastError = true)]
            private static extern IntPtr CreateDesktop(string lpszDesktop, IntPtr lpszDevice, IntPtr pDevmode, int dwFlags, uint dwDesiredAccess, IntPtr lpsa);
    
            [DllImport("user32.dll", SetLastError = true)]
            private static extern bool CloseDesktop(IntPtr hDesktop);
    
            [DllImport("user32.dll", SetLastError = true)]
            private static extern bool SwitchDesktop(IntPtr hDesktop);
    
            private const int DESKTOP_CREATEWINDOW = 0x02;
            private const int DESKTOP_READOBJECTS = 0x01;
            private const int DESKTOP_SWITCHDESKTOP = 0x0100;
            private const int DESKTOP_WRITEOBJECTS = 0x0080;
            private const int DESKTOP_ALL_ACCESS = DESKTOP_CREATEWINDOW | DESKTOP_READOBJECTS | DESKTOP_SWITCHDESKTOP | DESKTOP_WRITEOBJECTS;
    
            static void Main(string[] args)
            {
                IntPtr newDesktop = CreateDesktop("NewDesktop", IntPtr.Zero, IntPtr.Zero, 0, (uint)DESKTOP_ALL_ACCESS, IntPtr.Zero);
                if (newDesktop == IntPtr.Zero)
                {
                    Console.WriteLine("Failed to create a new desktop.");
                    return;
                }
    
                if (!SwitchDesktop(newDesktop))
                {
                    Console.WriteLine("Failed to switch to the new desktop.");
                    CloseDesktop(newDesktop);
                    return;
                }
    
                ProcessStartInfo startInfo = new ProcessStartInfo("notepad.exe");
                startInfo.UseShellExecute = false;
                Process process = new Process { StartInfo = startInfo };
    
                if (!process.Start())
                {
                    Console.WriteLine("Failed to start Notepad.");
                }
    
                process.WaitForExit();
                SwitchDesktop(newDesktop);
                CloseDesktop(newDesktop);
            }
        }
    }
    
    

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.