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

来源:互联网 发布:java修改tomcat端口号 编辑:程序博客网 时间:2024/04/26 02:05

问题描述

在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));        }
0 0
原创粉丝点击