在Python中为LogonUser函数使用/创建等效函数

2024-04-26 14:12:21 发布

您现在位置:Python中文网/ 问答频道 /正文

直截了当地说,我一直在寻找在Python中使用Windows ^{}函数的方法。在

在看过一个朋友用C语言使用这个函数之后,我想用Python尝试一个类似的过程。有办法吗?或者我是否需要尝试在C中创建脚本的这一部分,然后让Python脚本调用它?在


Tags: 方法函数脚本过程windows朋友办法
2条回答

如果您在Linux上工作,那么与Windows ^{} function大致等价的是^{},如SO_PEERCRED。(感谢Grawity对超级用户peercred)的介绍。在

您可以使用ctypes直接调用Windows API:

import ctypes
import ctypes.wintypes
# create variable that will be filled with a token that represents the user
token = ctypes.wintypes.HANDLE()
# 3 is LOGON32_LOGON_NETWORK, 0 is LOGON32_PROVIDER_DEFAULT
if ctypes.windll.advapi32.LogonUserW(u"username", u"domain", u"password", 3, 0, ctypes.byref(token)) == 0:
    # failed to login if we get 0
    failed_to_login()
# close the handle to avoid leaking it
ctypes.windll.kernel32.CloseHandle(token)

您也可以在失败后调用ctypes.windll.kernel32.GetLastError(),查看错误代码是什么,您可以通过命令提示符中的net helpmsg nnnn来查找。在

^{pr2}$

相关问题 更多 >