您的位置:首页 > 其它

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

2016-06-22 22:48 891 查看

问题描述

在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));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  windows