windows服务与前台交互

1.问题描述

最近遇上一个难题,我写了一个windows服务,想要与桌面可以进行交互,设置了属性在运行一点反应也没有。

2.解决方案

   查找出来的解决方案有以下这些(一个代码块一个):

 

 1 [RunInstaller(true)]
 2     public partial class ProjectInstaller : System.Configuration.Install.Installer
 3     {
 4         public ProjectInstaller()
 5         {
 6             InitializeComponent();
 7         }
 8 
 9         protected override void OnAfterInstall(IDictionary savedState)
10         {
11             try
12             {
13                 base.OnAfterInstall(savedState);
14                 System.Management.ManagementObject myService = new System.Management.ManagementObject(
15                     string.Format("Win32_Service.Name='{0}'", this.serviceInstaller1.ServiceName));
16                 System.Management.ManagementBaseObject changeMethod = myService.GetMethodParameters("Change");
17                 changeMethod["DesktopInteract"] = true;
18                 System.Management.ManagementBaseObject OutParam = myService.InvokeMethod("Change", changeMethod, null);
19             }
20             catch (Exception)
21             {
22             }
23         }
24     }
View Code
 1 protected override void OnCommitted(System.Collections.IDictionary savedState)
 2         {
 3             base.OnCommitted(savedState);
 4 
 5             using (RegistryKey ckey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\" + Constants.ServiceName, true))
 6             {
 7                 if (ckey != null)
 8                 {
 9                     if (ckey.GetValue("Type") != null)
10                     {
11                         ckey.SetValue("Type", (((int)ckey.GetValue("Type")) | 256));
12                     }
13                 }
14             }
15         }
View Code
1 @echo "准备停止服务程序..."
2 sc stop TestService 
3 @echo "设置允许与桌面进行交互方式允许"
4 sc config TestService type= interact type= own
5 @echo "正在重新启动服务..."
6 sc start TestService 
7 @echo "启动服务成功!"
View Code

经验证,那个都无法实现服务与前台交互,桑心T0T........

3.最终版

   问题解决方案:由于windwos系统的升级,对权限划分更加明确,我们需要进行Sessoin 0穿透隔离,但是什么是Session 0呢?

   http://www.cnblogs.com/gnielee/archive/2010/04/07/session0-isolation-part1.html(大神详解,就不班门弄斧了)

ps:部分代码及结果展示

 1  //个人理解Session0是开启系统就出现的系统级别的“用户”,即使没有任何用户登录,这个“用户”也是自动“登录”的,这个应该叫“老板”即 Session 0,其他的才叫桌面用户:管理员、A用户、B用户、其他的远程用户等等,都是非Session0级别的。
 2     class Interop
 3     {
 4         #region  简单
 5         public static IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero;
 6 
 7         public static void ShowMessageBox(string message, string title)
 8         {
 9             try
10             {
11                 int resp = 0;
12                 File.AppendAllText(@"C:\log.txt", WTSGetActiveConsoleSessionId().ToString());
13                 WTSSendMessage(
14                     WTS_CURRENT_SERVER_HANDLE,
15                     WTSGetActiveConsoleSessionId()+1,
16                     title, title.Length,
17                     message, message.Length,
18                     0, 0, out resp, false);
19             }
20             catch (Exception ex) {
21                 File.AppendAllText(@"C:\log.txt", ex.Message);
22             }
23         }
24 
25         [DllImport("kernel32.dll", SetLastError = true)]
26         public static extern int WTSGetActiveConsoleSessionId();
27 
28         [DllImport("wtsapi32.dll", SetLastError = true)]
29         public static extern bool WTSSendMessage(
30             IntPtr hServer,
31             int SessionId,
32             String pTitle,
33             int TitleLength,
34             String pMessage,
35             int MessageLength,
36             int Style,
37             int Timeout,
38             out int pResponse,
39             bool bWait);
40         #endregion
41 }
View Code

 

posted @ 2017-02-21 17:31  努力的小样  阅读(1531)  评论(0编辑  收藏  举报
Copyright ©2017 爱睡觉的程序猿