windows 如何检查程序是否在本地控制台中运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/244397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if program is running in local console?
提问by Harriv
In Windows Server 2003, how I can check if my program is running in local console ("on the screen of the server machine") instead of remote session?
在 Windows Server 2003 中,如何检查我的程序是否在本地控制台(“在服务器计算机的屏幕上”)而不是远程会话中运行?
I'm using Delphi Win32, so any Windows API based method should work..
我正在使用 Delphi Win32,所以任何基于 Windows API 的方法都应该可以工作..
回答by Bob Moore
Wouldn't the session number tell you this ?
会话编号不会告诉您这一点吗?
ProcessIdToSessionId (GetCurrentProcessId(),&dwSessionNum)
ProcessIdToSessionId (GetCurrentProcessId(),&dwSessionNum)
You'd have to check the OS version as well, using GetVersionEx: for everything up to XP/Server 2003 session 0 is local (service or interactive console), anything higher is virtual. For Vista/2008 session 0 and 1 are both local (0 is service, 1 is console), everything else is virtual.
您还必须使用 GetVersionEx 检查操作系统版本:对于 XP/Server 2003 会话 0 之前的所有内容是本地的(服务或交互式控制台),任何更高的都是虚拟的。对于 Vista/2008,会话 0 和 1 都是本地的(0 是服务,1 是控制台),其他一切都是虚拟的。
I'm guessing your Delphi units would declare the session number as var, so you wouldn't need the ampersand.
我猜你的 Delphi 单元会将会话号声明为 var,所以你不需要&符号。
回答by Brian
WTSGetActiveConsoleSessionId()should return the ID of the session attached to the console. You can then compare that session id with your application's current session ID to determine whether you are running on the console or not. Vista (not sure about Windows Server 2008) does not necessarily give the console session the ID of 1 (Fast User Switching, anyone?).
WTSGetActiveConsoleSessionId()应该返回附加到控制台的会话的 ID。然后,您可以将该会话 ID 与应用程序的当前会话 ID 进行比较,以确定您是否在控制台上运行。Vista(不确定 Windows Server 2008)不一定会给控制台会话 ID 1(快速用户切换,有人吗?)。
回答by Anders ?hrt
For me, ProcessIdToSessionId returned 0 both when run directly at the physical console and when logged in to the administrative session (mstsc /admin).
对我来说,ProcessIdToSessionId 在直接在物理控制台上运行和登录到管理会话 (mstsc /admin) 时都返回 0。
However, when you login via RDP, Windows (XP Pro in this case) creates a new session which it shows on the physical console which just has the "this computer is locked" display. WTSGetActiveConsoleSessionId returns the session id for that second session which in my case was 2.
但是,当您通过 RDP 登录时,Windows(在本例中为 XP Pro)会创建一个新会话,该会话会显示在物理控制台上,该物理控制台仅显示“此计算机已锁定”。WTSGetActiveConsoleSessionId 返回第二个会话的会话 ID,在我的例子中是 2。
So even though your app is running on the console, there are now two console sessions and your app is not running on the active one. In my code I compare session id against 0 instead.
因此,即使您的应用程序在控制台上运行,现在也有两个控制台会话并且您的应用程序没有在活动的一个会话上运行。在我的代码中,我将会话 ID 与 0 进行比较。
回答by Bob Moore
Brian is correct, I have since encountered Vista reporting a session id of 2 for an interactive console, despite the fact that Fast User Switching was not in use. Of course, this may be just be a bug :-)
布赖恩是正确的,尽管没有使用快速用户切换,但我还是遇到了 Vista 报告交互式控制台会话 ID 为 2 的情况。当然,这可能只是一个错误:-)