Presentation is loading. Please wait.

Presentation is loading. Please wait.

SVR 352 扩展Windows Server 终端服务在企业中的应用

Similar presentations


Presentation on theme: "SVR 352 扩展Windows Server 终端服务在企业中的应用"— Presentation transcript:

1 SVR 352 扩展Windows Server 终端服务在企业中的应用

2 课程内容概述 您可能还不知道终端服务的潜力到底有多大?让我们看看如何设计在终端服务里平稳运行、功能先进的企业应用程序。首先,了解终端服务应用软件的设计规范;其次,研究如何使用终端服务的脚本接口、API与托管代码调用方法;最后,我要介绍一个可以实现灵活定制的库:虚拟通道。 适合听众:企业IT架构师、开发人员 预备知识:终端服务的概念与使用经验,Scripting,Windows开发技能和.NET开发技能。

3 课程内容安排 终端服务的协议、架构与模式 使用脚本或API操作终端服务 使用 .NET Framework操作终端服务
跨越边界-虚拟通道应用程序

4 终端服务的协议、架构和应用程序规范 2/24/2019 12:20 AM 4
© 2006 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

5 终端服务的概念 穿透的客户机/服务器环境 终端服务器环境-基于服务器进行计算

6 终端服务的概念 远程桌面客户端 终端服务器 RDP 协议

7 安装应用程序 终端服务的应用服务器模式 注销目前登录的用户 退出安装过程中会加以修改的服务和程序 安装过程中禁止用户登录
“Change Logon /Disable” 使用控制面板内的“添加删除程序”进行安装,自动会进入安装模式 注意: 如果必要,你可以手工进入安装模式 “Change User /Install”

8 安装应用程序之后的任务 安装完毕后,系统需从“安装模式”恢复到“执行模式” 如果必要的话,也可以手工来做: 重新启动刚才退出的应用程序或服务
“Change user /execute” 重新启动刚才退出的应用程序或服务 允许用户登录 “Change logon /enable”

9 双模式的意义 只有在应用服务器模式才存在安装和执行分开的“双模式”现象。 双模式的分工: 应用程序兼容性
注册表重定向 (又称shadowing) 临时文件重定向 .ini 文件映射 API 重定向

10 安装模式 管理员 用户 Registry: HKCU (User Profile – Admin)
Registry: Shadow Keys – Write Global .ini Files (%WinDir%, \WINDOWS) Global Temp Files (%Temp%) Global API Calls (GetWindowsDirectory) Registry: HKCU (User Profile – User) Registry: Shadow Keys – Read Adapted .ini Files (%WinDir%, \WINDOWS) Specific Temp Files (%Temp%) Specific API Calls (GetWindowsDirectory) 用户

11 注册表重定向 HKCU\Software\MyApplication
HKLM\Software\Microsoft\Windows NT\CurrentVersion\Terminal Server\Install\Software\MyApplication

12 适合多用户同时使用的应用程序 安装和配置设计规范 具有可靠的无值守安装和无界面安装,含默认配置。 默认安装到 Program Files目录
识别和覆盖旧版本 支持“添加/删除程序”界面,以及“All Users”安装。 不替换受Windows文件保护的文件 避免不必要的重新启动 允许管理员根据公司政策设置安全性和用户界面选项。

13 适合多用户同时使用的应用程序 安全性设计规范 可以使用一般或者受限用户的权限运行-不依赖于本地管理员权限。
避免读写本机注册表模块和系统文件夹。 应用程序默认不会跳过系统安全检查步骤。 应用程序能够应付“拒绝访问”的错误。

14 适合多用户同时使用的应用程序 设计上的考虑因素 应用程序应该可以允许多个交互用户同时使用。 不使用全局互斥或者限制实例数量。
不使用系统全局数据,比如IP地址或者主机名,来标识用户。 无共享组件冲突 (DLLs) 全局的数据和单个用户的数据严格区别开来。 单个实例不会独占所有的资源,比如内存、CPU、I/O等等。

15 适合多用户同时使用的应用程序 多用户程序的文件访问设计规范 文件访问锁定– 多个用户可能同时访问应用程序的文件
文件访问权限 – 把普通用户和管理员的访问权限区别开来 (full, read or execute 权限) 文件保存地址– 把用户产生的数据默认保存在用户自己的Profile目录里 (提供定制界面) 防止访问系统文件 – 在运行时不提供向应用程序自身目录和系统目录的写入。 为每个用户使用不同的临时文件。

16 适合多用户同时使用的应用程序 编码指南 大多数兼容性不好的应用程序可以通过外加脚本来解决。
尽量使用环境变量,避免硬编码,比如C:\Windows, C:\Program files 或者C:\Temp。 使用Win32 APIs 去调用打印机、注册表、连接名称或者其它和环境相关的功能。 避免使用 .ini 文件、实在要用,只使用标准Win32 API 去访问.ini 文件 (比如 WritePrivateProfileString) 大多数兼容性不好的应用程序可以通过外加脚本来解决。

17 2/24/ :20 AM 终端服务的编程方法 17 © 2006 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

18 终端服务器脚本 使用脚本可以访问绝大多数系统和终端服务器的设置。 终端服务的WMI接口提供当前连接和活动配置信息。
ADSI Extension for Terminal Services user configuration (TSUSEREX.DLL)可以用来管理和终端服务相关的用户属性。 Microsoft Windows Server 2003 脚本中心:

19 TS WMI Provider Classes
Win32_TerminalServices Win32_TSSessionDirectorySetting Win32_TerminalServiceToSetting Win32_TSSessionDirectory Win32_TerminalServiceSetting Win32_Terminal Win32_TerminalTerminalSetting Win32_TerminalSetting Win32_TSGeneralSetting Win32_TSPermissionSetting Win32_TSRemoteControlSetting Win32_TSSessionSetting Win32_TSAccount Win32_TSClientSetting Win32_TSLogonSetting Win32_TSNetworkAdapterSetting Win32_TSEnvironmentSetting Win32_TSNetworkAdapterListSetting

20 TS WMI 脚本 ‘ 修改终端服务的Home目录。 strComputer = "."
Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}\\„ _ & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery _ ("Select * from Win32_TerminalServiceSetting") For Each objItem in colItems errRes = objItem.SetHomeDirectory("u:\tsusers") Next

21 TS API简介 Terminal Services API 是Wtsapi32.dll提供的函数
可以在自己的应用程序中配置和管理终端服务器

22 TS API简介 下述操作系统提供TS API (Wtsapi32.dll)
Microsoft Windows NT Server 4.0 SP4, Terminal Server Edition Microsoft Windows 2000 Microsoft Windows XP Microsoft Windows Server 2003

23 TS API简介 用户态 核心态 Wtsapi32.dll TS API 应用程序 TS 服务 Winlogon 会话中的应用程序
Rdpwsx Smss Csrss TS 设备驱动程序 Termdd.sys 鼠标, 键盘 内核 Win32k.sys 核心态 Rdpwd.sys 显示驱动 Rdpdd.sys Tdtcp.sys 图像

24 剖析 Wtsapi32.dll 使用各种工具(Visual Studio) 列出Wtsapi32.dll 的引出函数表
Microsoft Binary File Dumper: “dumpbin –exports wtsapi32.dll” Microsoft Linker: “link /dump /exports wtsapi32.dll” Dependency Walker Depends.exe

25 剖析 Wtsapi32.dll

26 TS API 函数 [1/2] 函数 描述 WTSOpenServer 打开到终端服务器的一个句柄 WTSCloseServer
关闭到终端服务器的句柄 WTSDisconnectSession 把某用户从活动会话上断开 WTSLogoffSession 把某用户从活动会话上注销 WTSSendMessage 在会话中显示一个消息框 WTSShutdownSystem 关闭或重新启动终端服务器

27 TS API 函数 [2/2] 函数 描述 WTSEnumerateServers 枚举终端服务器 WTSEnumerateSessions
枚举某终端服务器的会话 WTSEnumerateProcesses 枚举会话中的进程 WTSTerminateProcess 终止会话中的某进程 WTSQuerySessionInformation 查询会话信息 WTSQueryUserConfig 查询用户配置信息 WTSQueryUserToken 获得用户访问令牌

28 TS API 编码 推荐:Visual Studio.Net 加 Platform SDK (终端服务API) C语言开发人员
incldue 头文件:Wtsapi32.h 修改配置属性 | 链接器 | 命令行 | 附加选项 - 加入“Wtsapi32.lib”。 修改配置属性 | 常规 | 字符集 - “使用多字节字符集” 修改配置属性 | C/C++ | 高级 | 编译目标 -“编译为C 代码(/TC)” 修改配置属性 | C/C++ | 代码生成 | 运行时库 - “多线程调试 (/MTd)” 或者 “多线程 (/MT)”

29 TS 编码流程 获取终端服务器的名称 获取该终端服务器的句柄 执行相应的任务,比如枚举和管理用户会话或进程 释放分配的资源,比如缓冲内存
关闭终端服务器的句柄

30 TS API C语言范例代码 #include <stdio.h> #include <windows.h>
#include <wtsapi32.h> int main(int argc, char* argv[]) { char* pszServerName = NULL; HANDLE serverHandle; PWTS_PROCESS_INFO pProcessInfo; serverHandle = WTSOpenServer(pszServerName); // more TS specific code WTSFreeMemory(pProcessInfo); WTSCloseServer(serverHandle); }

31 在.NET语言中使用TS API 终端服务API - 非托管代码 终端服务API 目前没有标准.NET 类库
在托管代码里面需要使用Platform Invoke (P/Invoke)

32 TS API P/Invoke Wrapper Class
Platform Invoke 是用来让托管代码调用DLL中的非托管代码的一个服务。 在包装类中,每一个DLL需要定义一个静态方法。 包装之后,可以像一般的静态函数一样调用。 [DllImport("wtsapi32.dll")] public static extern IntPtr WTSOpenServer(String strServername);

33 TS API Visual C#范例代码 using System;
using System.Runtime.InteropServices; using HANDLE = System.IntPtr; namespace TsNET { class Program static void Main(string[] args) HANDLE hServer = IntPtr.Zero; String strServername = null; hServer = WTS.WTSOpenServer(strServername); WTS.WTSCloseServer(hServer); }

34 2/24/ :20 AM 终端服务的虚拟通道技术 34 © 2006 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

35 虚拟通道 虚拟通道可以扩展终端服务器的功能 客户端组件 服务器端组件 终端服务客户端运行的时候会把DLL 加载进内存。
应用程序嵌入RDP ActiveX 控件。 服务器端组件 在终端服务中的某会话中运行的用户态应用程序 虚拟通道程序可以由登录脚本、启动目录或用户自行启动。

36 虚拟通道 远程桌面客户端 Mstscax.dll 虚拟通道: 额外的功能 终端服务 Wtsapi32.dll RDP

37 终端服务分层架构 RDP 客户端 终端服务 RDP 终端服务客户端应用 Wtsapi32.dll 终端服务API应用 终端服务
Winlogon Per- Session Apps Mstscax.dll Rdpwsx Smss Csrss 网络协议栈 终端服务设备驱动 Termdd.sys Kernel Win32k.sys Rdpwd.sys 显示驱动 Rdpdd.sys Tdtcp.sys RDP

38 虚拟通道客户端 在托管代码Windows 窗体中使用远程桌面控件 使用Oleview.exe 去找暴露的函数
Windows Forms ActiveX Control Importer Aximp.exe 可以ActiveX的COM 的类型库转成Windows窗体控件 “Aximp c:\Windows\system32\mstscax.dll” 生成Mstsclib.dll 和 Axmstsclib.dll 把这些DLLs 加入项目的参考项中

39 代码范例 – TS Client using AxMSTSCLib; ... private AxMsRdpClient m_msTsc;
public RDPClientForm() { m_msTsc = new AxMsRdpClient(); m_msTsc.Dock = DockStyle.Fill; Controls.Add(m_msTsc); InitializeComponent(); } ServerForm srvForm = new ServerForm(); if (srvForm.ShowDialog() != DialogResult.OK) return; m_msTsc.Server = srvForm.ServerName; m_msTsc.Connect();

40 虚拟通道程序设计 客户端 服务器端 创建虚拟通道: m_msTsc.CreateVirtualChannels("VCD");
在虚拟通道上发送... m_msTsc.SendOnVirtualChannel("VCD", "Message“); 服务器端 终端服务API 的包装类

41 虚拟通道-服务器端 函数 描述 WTSVirtualChannelOpen 打开一个虚拟通道的句柄
WTSVirtualChannelClose 关闭某虚拟通道句柄 WTSVirtualChannelQuery 返回虚拟通道的信息 WTSVirtualChannelRead 从虚拟通道的服务器端读取数据 WTSVirtualChannelWrite 向虚拟通道的服务器端写入数据

42 总结 为了提高应用程序的兼容性,在应用服务器模式下, 终端服务的设置会和默认Windows操作系统有所不同。
可以使用WMI或ADSI脚本来控制终端服务器的行为。 利用终端服务的API以及虚拟通道,应用程序可以实现跨越边界无缝计算!

43 Terminal Services Programming Guidelines
Terminal Services Management Applications (WMI and ADSI) Terminal Services API Using P/Invoke to Call Unmanaged APIs from Your Managed Classes Terminal Services ActiveX Control Interface Terminal Services Virtual Channels

44 TechNet Windows Script Center – Terminal Server Code Project
Code Project Pinvoke.net WIndows Terminal Server Technologies

45 胡睿 客户技术经理 大客户及合作伙伴事业部 微软公司 MSN:


Download ppt "SVR 352 扩展Windows Server 终端服务在企业中的应用"

Similar presentations


Ads by Google