Launch Interactive App From Windows Service in Windows 10

0

I'm running a Windows Service in a Windows 10 machine logged with a low privileges User, I'm trying to launch an interactive application (ex. notepad) in the context of the logged user.

The windows service is running with the admin credentials

enter image description here

I use the next code inside my service.

   [DllImport("Kernel32.dll", SetLastError = true)]
   private static extern uint WaitForSingleObject(IntPtr handle, uint milliseconds);

   var startupInfo = new StartupInfo();         // StartupInfo struct
   var processInfo = new ProcessInformation();  // ProcessInfo struct

   // COMMAND TO EXECUTE
   const string command = "cmd.exe /C c:\\windows\\system32\\notepad.exe";

   var response = CreateProcessWithLogonW(
                    "adminUserName", //user
                    ".",             //domain
                    "adminUserPwd",  //password
                    (uint)0,         //logonFlags
                    null,            //appName
                    command,         //command
                    (uint)0,         //creationFlags
                    (uint)0,         //environment
                    null,            //currentDirectory
                    ref startupInfo,
                    out processInfo);

   if (!response)
   {
       Log($"Error: {Marshal.GetLastWin32Error()}");
       return false;
   }

   const uint infinite = 0xFFFFFFFF;
   const uint waitFailed = 0xFFFFFFFF;

   var uiResultWait = WaitForSingleObject(processInfo.process, infinite);
   
   if (uiResultWait == waitFailed)
   {
       Log($"Error: {Marshal.GetLastWin32Error()}");
       return false;
   }

   return true;

The function responses successfully and I can see the notepad running too, but not appears the window (seems like running in another user context)

enter image description here

Someone can tell me what i'm missing?

Best regards

c#
windows
windows-services
impersonation
user-permissions
asked on Stack Overflow May 19, 2021 by mmuniz

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0