微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Windows 10 中从 Windows 服务启动交互式应用程序

如何解决在 Windows 10 中从 Windows 服务启动交互式应用程序

我正在以低权限用户登录的 Windows 10 计算机上运行 Windows 服务,我正在尝试在登录用户的上下文中启动交互式应用程序(例如记事本)。

Windows 服务正在使用管理员凭据运行

enter image description here

我在我的服务中使用下一个代码

   [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;

函数响应成功,我也可以看到记事本正在运行,但没有出现窗口(似乎在另一个用户上下文中运行)

enter image description here

谁能告诉我我错过了什么?

最好的问候

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。