C# 从 Windows 服务启动 GUI 应用程序 - 窗口不出现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1109271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Launching GUI App from Windows Service - Window Does Not Appear
提问by
I have written a simple windows service which will launch a exe specified in the
onstart()
method of the service. After starting the service the exe got launched it only
presents in the memory but it doesnt show in the explorer.
我编写了一个简单的 Windows 服务,它将启动onstart()
服务方法中指定的 exe
。启动服务后,exe 被启动,它只出现在内存中,但不显示在资源管理器中。
I'm trying to launch a calc.exe
from my code.it shows the exe in the memory but it
doesnt comes into my view(i.e) in the explorer.
我正在尝试calc.exe
从我的代码中启动一个。它在内存中显示了 exe,但它没有进入我在资源管理器中的视图(即)。
Below is my code to launch the exe in the onStart() method
下面是我在 onStart() 方法中启动 exe 的代码
Process pr=new Process();
pr.StartInfo.FileName="calc.exe";
pr.StartInfo.WindowStyle=ProcessWindowStyle.Maximized;
pr.StartInfo.CreateNoWindow=false;
pr.Start();
// pr.WaitForExit();
回答by Philippe Leybaert
Services are not interactive by definition, so you shouldn't expect any user interface elements to show when you launch an application from a service.
根据定义,服务不是交互式的,因此当您从服务启动应用程序时,您不应期望显示任何用户界面元素。
It's by design...
这是设计的...
回答by Hemant
Services are run under different account privileges (LocalService/NetworkService etc)and hence they don't have access to your desktop (under yourlogin account's control).
服务在不同的帐户权限(LocalService/NetworkService 等)下运行,因此它们无权访问您的桌面(在您的登录帐户的控制下)。
Services are meant to do their job silently and thats what they should do. (with the exception of logging something in windows event log when they have something important to say)
服务旨在默默地完成他们的工作,这就是他们应该做的。(除了当他们有重要的事情要说时在 Windows 事件日志中记录一些东西)
回答by devdimi
Services run in other session on Vista or later and applications started directly from services are started in the same session by default. Starting applications in other sessions is possible - you have to find the id of the user session and use CreateProcessAsUser.
默认情况下,服务在 Vista 或更高版本上的其他会话中运行,并且直接从服务启动的应用程序在同一会话中启动。可以在其他会话中启动应用程序 - 您必须找到用户会话的 ID 并使用 CreateProcessAsUser。
If more than one user is logged in and you need to start your program for all users you must find the ids of all sessions.
如果有多个用户登录并且您需要为所有用户启动您的程序,您必须找到所有会话的 ID。
Here is sample code:
这是示例代码:
int session = Win32.WTSGetActiveConsoleSessionId();
if (session == 0xFFFFFFFF)
{
return false;
}
IntPtr userToken;
bool res = Win32.WTSQueryUserToken(session, out userToken);
if (!res)
{
this.log.WriteEntry("Error WTSQueryUserToken");
return false;
}
string path = GetPath();
string dir = Path.GetDirectoryName(path);
Win32.STARTUPINFO si = new Win32.STARTUPINFO();
si.lpDesktop = "winsta0\default";
si.cb = Marshal.SizeOf(si);
Win32.PROCESS_INFORMATION pi = new Win32.PROCESS_INFORMATION();
Win32.SECURITY_ATTRIBUTES sa = new Win32.SECURITY_ATTRIBUTES();
sa.bInheritHandle = 0;
sa.nLength = Marshal.SizeOf(sa);
sa.lpSecurityDescriptor = IntPtr.Zero;
if (!Win32.CreateProcessAsUser(userToken, // user token
path, // exexutable path
string.Empty, // arguments
ref sa, // process security attributes ( none )
ref sa, // thread security attributes ( none )
false, // inherit handles?
0, // creation flags
IntPtr.Zero, // environment variables
dir, // current directory of the new process
ref si, // startup info
out pi)) // receive process information in pi
{
int error = Marshal.GetLastWin32Error();
this.log.WriteEntry("Error CreateProcessAsUser:" + error);
return false;
}
回答by Alexander
Like already mentioned from the others a windows service is "normally" running under a separate account ("LocalSystem" or "NetworkService"). This is the reason why you might no see the UI of the program started by your service. Also services are not intended to have a UI, they act as a background service.
就像其他人已经提到的那样,Windows 服务“通常”在单独的帐户(“LocalSystem”或“NetworkService”)下运行。这就是为什么您可能看不到由您的服务启动的程序的 UI 的原因。此外,服务并不打算拥有 UI,它们充当后台服务。
But also note that starting a application by a service can be a high security risk, because the application is running with the same privileges than your service is. Normally this would be the local system account.
但还要注意,通过服务启动应用程序可能存在很高的安全风险,因为该应用程序以与您的服务相同的权限运行。通常这将是本地系统帐户。
I don't know what your are trying to achieve with your service, but consider to use the autostart function of windows instead of a service to run your application.
我不知道你想用你的服务实现什么,但考虑使用 Windows 的自动启动功能而不是服务来运行你的应用程序。
回答by HasaniH
If you open your service's properties window, go to the Log On tab then check the "Allow service to interact with desktop" check box you will get the behavior you want. Also depending on what app you what to run you may need to change the log on account.
如果您打开服务的属性窗口,请转到“登录”选项卡,然后选中“允许服务与桌面交互”复选框,您将获得所需的行为。此外,根据您要运行的应用程序,您可能需要更改登录帐户。