Control-Alt-Delete from python or command line

2020-05-01 06:48发布

I've done some research but I would like to be able to call control-alt-delete from python. If that is not possible is it possible to call it from command line because then I could just use that command in python because I can call command lines in python. If someone could point me in the right direction that would be great. this is for a task manager written with wxPython. edit: im trying to launch the windows security and from a user answer i tried

import win32com.client

shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys("{CONTROL}{ALT}{DELETE}")

and i get this error

Traceback (most recent call last):
    File "C:/Python27/tescontrol.py", line 4, in <module>
      shell.SendKeys("{CONTROL}{ALT}{DELETE}")
    File "<COMObject WScript.Shell>", line 2, in SendKeys
com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147024809), None)

6条回答
对你真心纯属浪费
2楼-- · 2020-05-01 06:58

If what you want to do is to shutdown or restart the system, Windows has a 'shutdown' command and linux's typically have 'shutdown' and 'reboot' commands.

查看更多
Bombasti
3楼-- · 2020-05-01 07:01

Check out the following thread:

According to it, VNC uses something like this:

PostMessage HWND_BROADCAST, WM_HOTKEY, 0, MakeLong(MOD_ALT Or MOD_CONTROL, VK_DELETE)

I suspect you would need to use ctypes or PyWin32 to do something like this. I would probably go with ctypes since it's cross-platform, however, even with ctypes you would probably need to write a special method for each OS that you support.

查看更多
做自己的国王
4楼-- · 2020-05-01 07:07

You surely mean activating the Windows Security window. In this case:

import win32com.client

shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys("^(%{DELETE})")

UPDATE

The above code seems not to work because of the reasons described in other posts. In that case, the alternative is to create a similar window and call from Python the different programs/functions called by the real Windows Security window.

On reading OP's comments to the original question, OP's final need is to change a user's password. This can be done with:

from win32com import adsi
ads_obj = adsi.ADsGetObject("WinNT://localhost/%s,user" % username)
ads_obj.SetPassword(password)

I just tested this in my PC, so is final information (though not necessarily correct; this is up to the OP :-) ).

UPDATE 2: Copying the later as a separate answer as comments seem to indicate that all of the answer doesn't work. This is correct for the SendKeys proposition, which doesn't work.

查看更多
别忘想泡老子
5楼-- · 2020-05-01 07:08

You can use vncdotool library At:

lib

And use vncdotool by:

os.system("vncdotool key ctrl-alt-del")
查看更多
相关推荐>>
6楼-- · 2020-05-01 07:13

As far as I know, Ctrl-Alt-Delete is protected for security reasons, so programs cannot use it. (At least in Windows 7 and before.)

查看更多
\"骚年 ilove
7楼-- · 2020-05-01 07:14

On reading OP's comments, his/her original need was to change the user's password. In fact, this can be done with:

from win32com import adsi
ads_obj = adsi.ADsGetObject("WinNT://localhost/%s,user" % username)
ads_obj.SetPassword(password)
查看更多
登录 后发表回答