如何确定当前 Windows 会话是否被锁定?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1016331/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:40:03  来源:igfitidea点击:

How to determine if the current Windows session is locked or not?

c#.netwindowswinapi

提问by Martin

I have an application which runs every hour on a Windows XP Machine. To run properly, this application requires the current session to be unlocked. So I was wondering if there a way to know if the current Windows session is locked or not with C# and .NET 3.5.

我有一个应用程序,它在 Windows XP 机器上每小时运行一次。要正常运行,此应用程序需要解锁当前会话。所以我想知道是否有办法知道当前的 Windows 会话是否被 C# 和 .NET 3.5 锁定。

UPDATE: The application cannot listen to the SessionNotification events. The application can be kicked off anytime and terminates when completed.

更新:应用程序无法侦听 SessionNotification 事件。该应用程序可以随时启动并在完成后终止。

Thanks!

谢谢!

回答by Dan F

These are untested by me, but look interesting. I'd give them a test, but my Visual Studio appears to be less than happy with me at the moment.

这些未经我测试,但看起来很有趣。我会给他们一个测试,但目前我的 Visual Studio 似乎对我不太满意。

回答by Greg Hewgill

I once used the GetForegroundWindow()function to determine whether a screen saver was running. If the return value was NULL, then the screen saver (or, presumably, a locked workstation) was active. Note that this was in the early days of Windows NT, is not documented to do this, and there's no guarantee that it will still do the same thing today.

我曾经使用该GetForegroundWindow()功能来确定屏幕保护程序是否正在运行。如果返回值为NULL,则屏幕保护程序(或者,可能是锁定的工作站)处于活动状态。请注意,这是在 Windows NT 的早期,没有记录可以这样做,并且不能保证它今天仍然会做同样的事情。

Looking at the Win32 API reference, you may be able to use something like OpenInputDesktop()to get the HDESKof the currently active desktop. If you can't get a handle to the desktop or if it's different from your thread desktop, then your app is running in a desktop that is not active (and the workstation is probably either in a screensaver state or is locked). No guarantees that this method will work, but it might be worth investigating.

查看 Win32 API 参考,您可以使用类似的方法OpenInputDesktop()来获取HDESK当前活动桌面的 。如果您无法获得桌面的句柄,或者它与您的线程桌面不同,那么您的应用程序正在非活动的桌面上运行(并且工作站可能处于屏幕保护程序状态或被锁定)。不能保证此方法会起作用,但可能值得研究。