Log out or switch user on Windows using Python

I want to write a small program that has given a time (in minutes) as input, sleeps in the background for that time, and then makes it go back to the "switch screen" (equivalent to Winkey + L) or walks away from the user (maybe another user registered on the same computer).

What functions or libraries in Python can I use for this?

Edit:

  • I only prefer going back to the Switch User screen rather than actually logging out.
  • Maybe there is a simple Windows command for this that I could use
  • I have Windows XP, if relevant
+1


a source to share


4 answers


There seems to be a simple way to lock a computer not using Python libraries other than ctypes:

import ctypes
ctypes.windll.user32.LockWorkStation ()

      



Source: Tim Golden Python

+7


a source


Perhaps you can use os.popen or subprocess to run Windows command prompt to log out. I think logging out is a command. (According to this page )



+1


a source


To switch users you can use this Win32 API function WTSDisconnectSession(HANDLE hServer, DWORD SessionId, BOOL bWait)

#include "WtsApi32.h"

BOOL OsofemSwitchUser()
{
//Switch User
return WTSDisconnectSession(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, TRUE);
}

      

Don't forget to link it to the library WtsApi32.lib

.
This will take you back to the Switch User screen ... Hope this help ...

+1


a source


I think you should be using Windows API and running python. use os.system ('logoff'); (or log out, I forgot) it is not checked because now I am using ubuntu ...

0


a source







All Articles