近段时间因为公司的要求开发一个在终端环境下进行通信的程序,我查找了很多的资料以及网站,但是几乎都没有这方面的材料,只有MSDN上面有一些英文资 料,我翻译了一部分发表出来与大家共享,希望有这方面经验的高手提出您们宝贵的意见,我在这里谢谢大家。使用终端服务API:这一节描述了这样使用终端服 务API在终端环境下去完成如下任务:l 执行终端服务管理任务。例如:列举在一个域中的所有终端服务器,列举与管理一个终端服务器下的所有会话与进程。l 在终端服务环境中增强一个客户/服务模式程序的功能。l 在一个程序中使用终端虚拟通道使客户与服务组件进行通信。l 设置或者取回一个特殊终端服务注册用户的配置信息。假如你的程序在非终端环境下运行,但是你想在终端环境下利用终端服务API增强程序的功能,请看 Run-time Linking to Wtsapi32.dll。
一、终端服务管理
终端服务API函数 使你能够列举与管理所有的终端服务器、客户会话以及进程。
为了得到一个域中所有终 端服务器的名字,可以调NetServerEnum函数去列举所有SV_TYPE_TERMINALSERVER类型的终端服务器。通过传递一个终端服务 器的名字调用WTSOpenServer函数可以打开一个特殊终端服务器的句柄,当完成操作后可以调用WTSCloseServer函数去关闭它。
你可以使用通过WTSOpenServer函数返回的终端服务器句柄在服务器中执行以下的操 作:
函数操作WTSDisconnectSession从一个特殊的会话中断开用户连接,但是这会话仍然存在,而且用户可以再次登陆并连接这个会 话。WTSEnumerateSessions返回在一个特殊终端服务器中包括所有的会话的一个列表WTSEnumerateProcesses返回在一 个特殊终端服务器中包括所有的进程的一个列表WTSLogoffSession登陆一个特殊的会话WTSQuerySessionInformation 取回一个特殊终端服务器中一个特殊会话的信息WTSSendMessage在一个特殊会话下的客户端显示器上显示一个信息框。 WTSShutdownSystem关闭或者启动一个终端服务器WTSTerminateProcess结束一个特殊终端服务器中特殊的进程 WTSVirtualChannelOpen打开一个特殊虚拟终端服务端的句柄。为了得到更多的信息,请关注Using Terminal Services Virtual Channels. WTSWaitSystemEvent等待一个事件,例如一个客户会话的创建,一个用户登陆到一个终端服务器上。上面的一些函数分配了缓冲区来返回信息给 调用者,在使用完这些缓冲区后请使用WTSFreeMemory函数去释放这些内存资源。
WTSLogoffSession Function
Logs off a specified Remote Desktop Services session.
Syntax
BOOL WTSLogoffSession(
__in HANDLE hServer,
__in DWORD SessionId,
__in BOOL bWait
);
Parameters
hServer [in]
A handle to an RD Session Host server. Specify a handle opened by the WTSOpenServer or WTSOpenServerEx function, or specify WTS_CURRENT_SERVER_HANDLE to indicate the RD Session Host server on which your application is running.
SessionId [in]
A Remote Desktop Services session identifier. To indicate the current session, specify WTS_CURRENT_SESSION. You can use the WTSEnumerateSessions function to retrieve the identifiers of all sessions on a specified RD Session Host server.
To be able to log off another user's session, you need to have the Reset permission. For more information, see Remote Desktop Services Permissions. To modify permissions on a session, use the Remote Desktop Services Configuration administrative tool.
To log off sessions running on a virtual machine hosted on a RD Virtualization Host server, you must be a member of the Administrators group on the RD Virtualization Host server.
bWait [in]
Indicates whether the operation is synchronous.
If bWait is TRUE, the function returns when the session is logged off.
If bWait is FALSE, the function returns immediately. To verify that the session has been logged off, specify the session identifier in a call to the WTSQuerySessionInformation function. WTSQuerySessionInformation returns zero if the session is logged off.
Return Value
If the function succeeds, the return value is a nonzero value.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Requirements
Minimum supported client Windows 2000 Professional
Minimum supported server Windows 2000 Server
Header Wtsapi32.h
Library Wtsapi32.lib
DLL Wtsapi32.dll
WTSOpenServer Function
The WTSOpenServer function opens a handle to the specified Remote Desktop Session Host (RD Session Host) server.
Syntax
HANDLE WTSOpenServer(
__in LPTSTR pServerName
);
Parameters
pServerName [in]
Pointer to a null-terminated string specifying the NetBIOS name of the RD Session Host server.
Return Value
If the function succeeds, the return value is a handle to the specified server.
If the function fails, it returns a handle that is not valid. You can test the validity of the handle by using it in another function call.
Windows Server 2003, Windows XP, and Windows 2000: If the function fails, the return value is NULL. To get extended error information, call GetLastError.
Remarks
When you have finished using the handle returned by WTSOpenServer, release it by calling the WTSCloseServer function.
You do not need to open a handle for operations performed on the RD Session Host server on which your application is running. Use the constant WTS_CURRENT_SERVER_HANDLE instead.
Requirements
Minimum supported client Windows 2000 Professional
Minimum supported server Windows 2000 Server
Header Wtsapi32.h
Library Wtsapi32.lib
DLL Wtsapi32.dll
Unicode and ANSI names WTSOpenServerW(Unicode) and WTSOpenServerA (ANSI)
阅读(1430) | 评论(0) | 转发(1) |