Lock windows session programmatically




















The LockWorkStation function is callable only by processes running on the interactive desktop. In addition, the user must be logged on, and the workstation cannot already be locked. Common reasons the workstation might not be locked even if the function succeeds include the following: no user is logged on, the workstation is already locked, the process is not running on the interactive desktop, or the request is denied by the Graphical Identification and Authentication GINA DLL.

To unlock the workstation, the user must log in. There is no function you can call to determine whether the workstation is locked. You can use session notifications to track the desktop state so you know whether it is possible to interact with the user. Improve this answer. Remy Lebeau k 29 29 gold badges silver badges bronze badges. Thank you for your reply, very helpful, but are there any variants to do not turn off the screen, just make it unresponsive?

Honestly, I'm not sure. You might be able to make a popup window that steals mouse and keyboard focus and is unresponsive until the timeout occurs. You should not mix them. I've removed this from your answer.

Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. Obviously your application will need to be running, but so long as it is: Microsoft. Improve this answer. Termininja 6, 12 12 gold badges 43 43 silver badges 46 46 bronze badges. Timothy Carter Timothy Carter Wow, works amazing! This is the right way to do it. According to this Microsoft article , "There is no function you can call to determine whether the workstation is locked.

Dan Atkinson 11k 14 14 gold badges 79 79 silver badges bronze badges. WriteLine "Locked WriteLine "Unlocked Handle ;. This is a good option if you find that the SessionSwitch event from other answers doesn't fire e. For future readers Form, as in you might write a class like this : public class Form1 : System. Form — granadaCoder. This works for me when SystemEvents. Locked; default: return LockState. Unlocked; default: return LockState. Community Bot 1 1 1 silver badge. Robert Robert 3, 2 2 gold badges 23 23 silver badges 25 25 bronze badges.

This works windows 7 tested so far. Thanks, we've been looking for this for the last few weeks and your answer has come in the nick of time! There are few errors in the code: 1. In this case compiler will do correct padding alignment of fields inside the structure. Hemang Rami, no problem. Just know that it works only on Windows OS. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog.

Podcast Making Agile work for data science. Stack Gives Back Featured on Meta.



0コメント

  • 1000 / 1000