如何判断进程是否基于windows终端服务运行?

jixingzhong 2009-01-19 10:36:55
最原始的问题是,一个程序运行时刻,需要判断当前运行是基于终端服务的,还是本机运行的,不知是否有巧妙的办法?

试验了 WTSQuerySessionInformation API,
1、是取终端IP。当本机运行的时候, WTSQuerySessionInformation取到的IP是0.0.0.0,如果是基于终端服务运行的,取到的值是终端IP。
问题:基于Dialog的一个测试程序,发现在OnInitDialog中取得的IP是0.0.*.*,*是随机整数,每次运行都回变化;而在对话框生成后的按钮响应中取得的IP是恒为0.0.0.0的,这是为何?如果判断是否基于终端服务依据为WTSQuerySessionInformation获得的IP第一字节是否为0,是否准确、可靠?

2、取进程sessionID。个人认为WTSQuerySessionInformation获取sessionID,本机运行的情况下,应当得到恒为0,但是msdn中的描述似乎不是这样?请教实际情况如何?

此外,是否有其他办法呢?首先要确保判断方式准确可靠,然后考虑简便、高效性。
...全文
219 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenyu2202863 2009-01-20
  • 打赏
  • 举报
回复
本机运行的情况下,WTSQuerySessionInformation获取sessionID确实为0

Task Manager中也是这样
dch4890164 2009-01-20
  • 打赏
  • 举报
回复
If it is specified when querying session information on a remote server, the returned session information will be inconsistent. Do not use the returned data in this situation.
看了半天好像这个最有用,这个返回值好像还真的不是固定的而且还不让用
这个时候必须得用这个才行
You can use the WTSEnumerateSessions function to retrieve the identifiers of all sessions on a specified terminal server.

oyljerry 2009-01-20
  • 打赏
  • 举报
回复
To retrieve the session ID for the current session when Terminal Services is running, call WTSQuerySessionInformation and specify WTS_CURRENT_SESSION for the SessionId parameter and WTSSessionId for the WTSInfoClass parameter. The session ID will be returned in the ppBuffer parameter. If Terminal Services is not running, calls to WTSQuerySessionInformation fail. In this situation, you can retrieve the current session ID by calling the ProcessIdToSessionId function.

To determine whether your application is running on the physical console, you must specify WTS_CURRENT_SESSION for the SessionId parameter, and WTSClientProtocolType as the WTSInfoClass parameter. If ppBuffer is "0", the session is attached to the physical console.
dch4890164 2009-01-20
  • 打赏
  • 举报
回复
对ppbuffer进行memset试验一下看还是随机数嘛,基本上就能判断出来了
再次帮顶
dch4890164 2009-01-20
  • 打赏
  • 举报
回复
我靠,好久没有见过这么大的场面了
穿着裤衩顶猩猩,感觉还是比较爽的
====================================
看样子应当是调用失败了,所以ppBuffer里并没有返回值,只是它原先内存当中的值,因为调用
失败所以函数并没有对它做什么,值就是内存的值,所以看起来像随机数一样,建议用GetLastError看一下,是不是有错误码
calls to WTSQuerySessionInformation fail. In this situation, you can retrieve the current session ID by calling the ProcessIdToSessionId function.
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.

jyh_baoding 2009-01-20
  • 打赏
  • 举报
回复
帮顶一个
hjcy_2002 2009-01-20
  • 打赏
  • 举报
回复
sessionID
tanmeining 2009-01-20
  • 打赏
  • 举报
回复
都是强人啊,混个脸熟^_^
fhinalibob 2009-01-20
  • 打赏
  • 举报
回复
学习,帮顶
yjgx007 2009-01-19
  • 打赏
  • 举报
回复
在Windows NT下引入了进程特权Token(通过OpenProcessToken得到),通过Token可以查到运行该进程Token信息(用GetTokenInformation得到):

BOOL GetTokenInformation(
HANDLE TokenHandle, // handle to access token
TOKEN_INFORMATION_CLASS TokenInformationClass, // token type
LPVOID TokenInformation, // buffer
DWORD TokenInformationLength, // size of buffer
PDWORD ReturnLength // required buffer size
);

得到sessionId,就要设置TokenInformationClass = TokenSessionId
TokenSessionId The buffer receives a DWORD value that indicates the Terminal Services session identifier associated with the token. If the token is associated with the Terminal Server console session, the session identifier is zero. A nonzero session identifier indicates a Terminal Services client session. In a non-Terminal Services environment, the session identifier is zero.
晨星 2009-01-19
  • 打赏
  • 举报
回复
咦,MSDN上不是提到了嘛(WTSQuerySessionInformation),最后一段:

To determine whether your application is running on the physical console, you must specify WTS_CURRENT_SESSION for the SessionId parameter, and WTSClientProtocolType as the WTSInfoClass parameter. If ppBuffer is "0", the session is attached to the physical console.
cnzdgs 2009-01-19
  • 打赏
  • 举报
回复
1、如果是本机运行,得到的AddressFamily成员是0。也可以取Client的其它信息来判断,例如WTSClientName,本机运行时返回空字符串。
2、系统中建立的第1个session的ID是0,其它session则可能是1、2、3等,例如XP系统中登录两个用户,第2个用户的sessionID就是1,如果是Vista系统,第1个用户的sessionID就是1,所以无法根据sessionID来判断。
晨星 2009-01-19
  • 打赏
  • 举报
回复
帮虫虫顶一顶,家里没装MSDN,NND。

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧