从服务以交互方式启动.Net winforms应用程序 [英] Launching a .Net winforms application interactively from a service

查看:132
本文介绍了从服务以交互方式启动.Net winforms应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境 - VS2008,Vista SP1。

Environment - VS2008, Vista SP1.

我编写了一个流程管理服务,可以在会话0或交互式控制台(通常为1)中启动应用程序。请注意,这不是正常的操作模式,它仅用于内部调试目的。在现场,这些进程将安全地隐藏在会话0.安全问题不适用。

I have written a process management service which can launch applications either in session 0 or the interactive console (usually 1). Please note this is NOT the normal mode of operation, it's for in-house debug purposes only. In the field, these processes will be safely hidden away in session 0. Security concerns do not apply.

显然,人们不会读这个:安全问题不适用。我们有几十个现有的服务器应用程序(NOT服务)这样写。我们不打算彻底改造这些应用程序,我们只需要能够在内部运行发行版本时获得其内置的调试对话框。我已经知道所有关于规范的解决方案和管道等。如果可以接受添加远程接口到所有这些应用程序,这是我们将要做的。

我使用下面的代码:

ZeroMemory (&sui, sizeof(STARTUPINFO));
sui.cb = sizeof (STARTUPINFO);
sui.wShowWindow = pTask->GetWinStartState() ;
sui.dwFlags     = STARTF_USESHOWWINDOW ;
ZeroMemory (&pi,sizeof(pi));

if (bInteractive)
{
   HANDLE  hToken = NULL;
   DWORD dwSessionId = WTSGetActiveConsoleSessionId();
   WTSQueryUserToken (dwSessionId, &hToken);
   sui.lpDesktop = TEXT("winsta0\\default"); 
   LPVOID  pEnv = NULL;
   DWORD dwCreationFlag = NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE;
   HMODULE hModu = LoadLibrary(TEXT("Userenv.dll"));

   if (hModu )
   {
      if (CreateEnvironmentBlock (&pEnv, hToken, FALSE))
         dwCreationFlag |= CREATE_UNICODE_ENVIRONMENT;    
      else
         pEnv = NULL;
   }

   bCreatedOk = CreateProcessAsUser (hToken,
                                     NULL,
                                     (LPTSTR)(pTask->GetExeName()),
                                     NULL,
                                     NULL,
                                     FALSE,
                                     dwCreationFlag,
                                     pEnv,
                                     NULL,
                                     &sui,
                                     &pi);
}
else
{
   bCreatedOk = CreateProcess (NULL, ... blah...);
}

这一切都很好,我可以在Vista中运行和监视本地进程服务会话和控制台。大。蛋糕和啤酒。

This all works fine and I can run and monitor native processes both in the Vista service session and the console. Great. Cakes and ale for everyone.

这里是问题。如果我尝试以交互方式运行 winforms (C#)应用程序,它似乎运行,在进程资源管理器中显示为运行在会话1,但在桌面上... nada。没有窗口出现。该过程运行起来并关闭所有罚款,但没有窗口出现。

So here's the problem. If I try to run a winforms (C#) app interactively like this, it appears to run, shows up in Process Explorer as running in session 1, but on the desktop... nada. No window appears at all. The process runs up and shuts down all fine, but no window ever appears. The exact same winform exe run from explorer also shows up in session 1, but this time appears on the desktop just fine.

任何想法?

推荐答案

尽管有明显的歇斯底里,但是从服务启动一个应用程序到交互式会话没有什么问题,只要使用互动用户或更低。由于您是作为交互式用户启动的,因此没有权限升级。

Despite the evident hysteria there is nothing wrong with launching an application from a service into an interactive session provided it is done with the same privileges as the interactive user or lower. Since you are launching as the interactive user there can be no privilege escalation.

您正在做什么工作。我怀疑这个问题与你的STARTUPINFO结构有关。你似乎在堆栈上创建你的sui,但你不显示你正在做什么。你正在初始化它的所有0,如果不是你可能会从堆栈中得到一些垃圾,导致窗口不显示或显示在屏幕的某些坐标。

What you are doing does work. I suspect that the issue has something to do with your STARTUPINFO struct. You appear to be creating your sui on the stack but you don't show what you are doing with it. Are you initializing it to all 0s, if not you may be getting some garbage from the stack that is causing the window not to show or to show at some co-ordinates off the screen.

这篇关于从服务以交互方式启动.Net winforms应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆