I’m working on a C# application on Windows 11 (version 23H2) that aims to create a user session (remote desktop session) from a Windows service. The goal is to launch an application within that session and then terminate the session.
Here’s where I’m encountering issues:
APIs Used:
LogonUser
(advapi32.dll): This seems to be working fine, presumably creating the user session.DuplicateToken
(advapi32.dll): Successfully duplicates the obtained user token for application launch.CreateProcessAsUser
(advapi32.dll): To launch the desired application within the newly created session and This is where things go wrong.
Errors:
- Launching Calculator (
@"C:windowsSystem32calc.exe"
): Throws aSystem.Runtime.InteropServices.SEHException
with the message “External component has thrown an exception.” - Launching Notepad (
@"C:windowsSystem32notepad.exe"
): Throws aSystem.ComponentModel.Win32Exception
with the message “The system cannot find the file specified.” (Interestingly, this path works when launching from the command prompt.)
What I Expected:
CreateProcessAsUser
to successfully launch the specified application (Calculator or Notepad) within the newly created user session.