Windows Service launching and Exe

拜拜、爱过 提交于 2019-12-10 16:27:34

问题


I am currently working on a project that contains a WCF service, a Windows service and a WPF application. The Windows service communicates with the WCF, and under a certain circumstance, must launch the WPF application for the user to receive messages. (WCF is on a remote server, the rest is on the client). I've hit a bit of a snag with the launch. I have the services writing messages to the application logs so that I can somewhat 'debug' along the way. The Windows service runs the following code with no problems.

C# code, Windows Service:

WriteLog.WriteString("PostOffice.MessagesWaiting: Inside, starting up.", EventLogEntryType.Warning);
// Call the WPF Application
var messagingProcess = new Process();
var messageProcessStartInfo = new ProcessStartInfo(@"""C:\GoldenEyeMessages.exe""");
messageProcessStartInfo.CreateNoWindow = false;
messageProcessStartInfo.UseShellExecute = false;
messageProcessStartInfo.FileName = @"""C:\GoldenEyeMessages.exe""";
messageProcessStartInfo.WindowStyle = ProcessWindowStyle.Normal;
messageProcessStartInfo.Verb = "runas";

messageProcessStartInfo.RedirectStandardOutput = true;
messagingProcess.StartInfo = messageProcessStartInfo;
messagingProcess.Start();
StreamReader daStreamReaderMan = messagingProcess.StandardOutput;
string newString = daStreamReaderMan.ReadLine();

WriteLog.WriteString("PostOffice.MessagesWaiting: The Eagle has landed.", EventLogEntryType.Warning);

The WPF application doesn't execute in the current user's session. Instead, I get a popup to view the message. Here is a picture of it:

Once selecting the 'View the message' option, it of course switches me to a different session and then runs the WPF application.

My question is, how should I go about getting the WPF application to launch within the current user's session, or the 'active' session?


回答1:


You are getting this because the user that is trying the launch the WPF executable does not have permission to interact with the desktop.

Windows Services typically do not run under an account with that permission, and it is considered a security vulnerability to do so.

In most cases, it is recommended that you not change the Allow service to interact with desktop setting. If you allow the service to interact with the desktop, any information that the service displays on the desktop will also be displayed on an interactive user's desktop. A malicious user could then take control of the service or attack it from the interactive desktop.

http://technet.microsoft.com/en-us/library/cc782435(v=ws.10).aspx

A more secure alternative would be to have the WPF application always run in the system tray and arrange a mechanism for the Windows Service to signal the WPF application that a message needs to be shown. One simple mechanism is to write a file to an agreed location and use a file watcher in the WPF app to look for that file (and delete it after displaying). Note that the windows service might be running long before a user logs in (so long before the WPF app is running), so whatever notification mechanism you use needs to allow for messages to accumulate and be displayed at once after login.



来源:https://stackoverflow.com/questions/11548758/windows-service-launching-and-exe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!