如何解决vista和win7在windows服务中无法与桌面程序交互的问题?

问题描述

在Windows XP、Windows Server 2003 或早期Windows 系统时代,Windows服务与桌面应用程序的交互非常简单,而vista之后的版本windows加强了权限管理,也就 是所谓的session0隔离,致使在windows服务中调用Process.Start打开桌面程序时,无法看到界面(进程中已经打开了该桌面程序)。

vista/win7系统中的windows服务无法与桌面应用程序交互的原因,请看:
穿透Session 0 隔离(一)
穿透Session 0 隔离(二)

解决方案

使用第三方控件:Cjwdev.WindowsApi.dll,Cjwdev.WindowsApi.xml
下载地址:http://files.cnblogs.com/deepleo/Cjwdev.WindowsApi.dll.zip
示例代码:

using Cjwdev; 
using Cjwdev.WindowsApi;

try 
       {
           appStartPath= ‚C:\\ruhoo.exe; 
           IntPtr userTokenHandle = IntPtr.Zero; 
           ApiDefinitions.WTSQueryUserToken(ApiDefinitions.WTSGetActiveConsoleSessionId(), ref userTokenHandle);
           ApiDefinitions.PROCESS_INFORMATION procInfo = new ApiDefinitions.PROCESS_INFORMATION(); 
           ApiDefinitions.STARTUPINFO startInfo = new ApiDefinitions.STARTUPINFO(); 
           startInfo.cb = (uint)Marshal.SizeOf(startInfo);
           ApiDefinitions.CreateProcessAsUser( 
               userTokenHandle, 
               appStartPath, 
             ‚„, 
               IntPtr.Zero, 
               IntPtr.Zero, 
               false, 
               0, 
               IntPtr.Zero, 
               null, 
               ref startInfo, 
               out procInfo);
           if (userTokenHandle != IntPtr.Zero) 
               ApiDefinitions.CloseHandle(userTokenHandle);
           _currentAquariusProcessId = (int)procInfo.dwProcessId; 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show(string.Format("Start Application failed, its path is {0} ,exception: {1}", appStartPath, ex.Message)); 
       }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值