3

I'm working on windows xp. How can i write a simple script which would automatically log me out of this account and log into another account on this computer upon execution of a trigger point I set in my application??

Please help.

Rohit
  • 196
  • 2
  • 9

3 Answers3

3

What do you need to do on another account? If you want to run some commands on another account os.system and runas will help you.

kichik
  • 33,220
  • 7
  • 94
  • 114
ceth
  • 44,198
  • 62
  • 180
  • 289
2

To logoff, call ExitWindowsEx() with EWX_LOGOFF (0). To automatically login afterwards, write DefaultUserName and DefaultPassword to HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon. You may also need to set AutoAdminLogon to 1. Don't forget to later remove the registry keys or it'll just keep logging into that user forever.

In python, you can use ctypes to call the function and _winreg to write to the registry.

# setup login
from _winreg import *
key = OpenKey(HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon', 0, KEY_ALL_ACCESS)
SetValueEx(key, "DefaultUserName", 0, REG_SZ, "username")
SetValueEx(key, "DefaultPassword", 0, REG_SZ, "password")
SetValueEx(key, "AutoAdminLogon", 0, REG_DWORD, 1)
CloseKey(key)
# logoff
import ctypes
ctypes.windll.user32.ExitWindowsEx(0,0)
kichik
  • 33,220
  • 7
  • 94
  • 114
0

I'm going to go out on a limb here, and suggest there probably isn't any Python library that can do this directly. Instead, you will need to call some C win32 functions from Python.

I think your best bet is to split this up into two separate questions.

The first question is "How do I programmatically, in Win32, initiate a log-out and log in as a different user?"

That will hopefully be answered by some Windows expert, even if they are unfamiliar with Python.

I don't know much about this, but it sounds awfully ambitious. Triggering a log-out might be possible, but triggering a log-in as a different user (presumably stopping to ask the user for a password)

The second question is "How do I call these win32 functions from within Python?"

That is a fairly regular request and there should be the expertise on StackOverflow to help.

Oddthinking
  • 24,359
  • 19
  • 83
  • 121