Python 3中LogonUserW中的访问冲突 [英] Access Violation in LogonUserW in Python 3

查看:214
本文介绍了Python 3中LogonUserW中的访问冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为某人编写Python3脚本,该脚本通过ctypes利用advapi dll及其LogonUserW函数.

运行代码时

__ init __ 函数中

dll_location = find_library("advapi32");

if (dll_location == None):
    raise FileNotFoundError

adv_dll = WinDLL(dll_location);

#gets the pointer to the function
logonUser = adv_dll.LogonUserW;
self.logonUser = logonUser

登录(用户名,域,密码)功能

#Sets the parameters to call the DLL
loginType = DWORD(2)
loginProvider = DWORD(0)
handle = PHANDLE()
user = LPCSTR(username.encode());
pw = LPCSTR(password.encode());
dom = LPCSTR(domain.encode());

rescode = self.logonUser(user, dom, pw, loginType, loginProvider, handle);

它引发OSError: exception: access violation writing 0x0000000000000000

有什么想法会导致错误以及如何解决?

PS:是的,我知道我没有遵循PEP 8的变量名,我通常是Java程序员.

解决方案

根据 [GitHub]:适用于Windows的Python(pywin32)扩展,它是 WINAPI 上的 Python 包装器.

code.py :

import sys
import ctypes
from ctypes import wintypes


def main():
    advapi32_dll = ctypes.WinDLL("advapi32.dll")
    logon_user_func = advapi32_dll.LogonUserW
    logon_user_func.argtypes = [wintypes.LPCWSTR, wintypes.LPCWSTR, wintypes.LPCWSTR, wintypes.DWORD, wintypes.DWORD, wintypes.PHANDLE]
    logon_user_func.restype = wintypes.BOOL

    user = "dummy_user"
    domain = "dummy_domain"
    pwd = "dummy_pwd"
    logon_type = 2
    provider = 0
    handle = wintypes.HANDLE()
    ret = logon_user_func(user, domain, pwd, logon_type, provider, ctypes.byref(handle))
    print("{:s} returned {:}".format(logon_user_func.__name__, "TRUE" if ret else "FALSE"))


if __name__ == "__main__":
    print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
    main()

注释:

  • 除了argtypes/restypes:
    • Python3 中,字符串默认为 ,因此不需要encode()
    • HANDLE通过byref
    • 传递

输出:

(py35x64_test) e:\Work\Dev\StackOverflow\q051251086>"e:\Work\Dev\VEnvs\py35x64_test\Scripts\python.exe" code.py
Python 3.5.4 (v3.5.4:3f56838, Aug  8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32

LogonUserW returned FALSE

I am writing a Python3 script for someone, that utilizes the advapi dll and its LogonUserW function via ctypes.

When running the code

in the __init__ function

dll_location = find_library("advapi32");

if (dll_location == None):
    raise FileNotFoundError

adv_dll = WinDLL(dll_location);

#gets the pointer to the function
logonUser = adv_dll.LogonUserW;
self.logonUser = logonUser

In login(username, domain, password) function

#Sets the parameters to call the DLL
loginType = DWORD(2)
loginProvider = DWORD(0)
handle = PHANDLE()
user = LPCSTR(username.encode());
pw = LPCSTR(password.encode());
dom = LPCSTR(domain.encode());

rescode = self.logonUser(user, dom, pw, loginType, loginProvider, handle);

It raises OSError: exception: access violation writing 0x0000000000000000

Any idea what could be causing the error and how to fix?

PS: Yes I know I am not following PEP 8 for variable names, I am normally a java programmer.

解决方案

According to [Python]: types - A foreign function library for Python, you should set argtypes and restype (this is one way) for the function you're calling ([MS.Docs]: LogonUserW function).

Below is a minimal example for calling it. If however, you need to call multiple such functions, you could also consider [GitHub]: Python for Windows (pywin32) Extensions, which is a Python wrapper over WINAPIs.

code.py:

import sys
import ctypes
from ctypes import wintypes


def main():
    advapi32_dll = ctypes.WinDLL("advapi32.dll")
    logon_user_func = advapi32_dll.LogonUserW
    logon_user_func.argtypes = [wintypes.LPCWSTR, wintypes.LPCWSTR, wintypes.LPCWSTR, wintypes.DWORD, wintypes.DWORD, wintypes.PHANDLE]
    logon_user_func.restype = wintypes.BOOL

    user = "dummy_user"
    domain = "dummy_domain"
    pwd = "dummy_pwd"
    logon_type = 2
    provider = 0
    handle = wintypes.HANDLE()
    ret = logon_user_func(user, domain, pwd, logon_type, provider, ctypes.byref(handle))
    print("{:s} returned {:}".format(logon_user_func.__name__, "TRUE" if ret else "FALSE"))


if __name__ == "__main__":
    print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
    main()

Notes:

  • Besides argtypes / restypes:
    • In Python3 strings are wide by default, so no need for encode()
    • The HANDLE is passed via byref

Output:

(py35x64_test) e:\Work\Dev\StackOverflow\q051251086>"e:\Work\Dev\VEnvs\py35x64_test\Scripts\python.exe" code.py
Python 3.5.4 (v3.5.4:3f56838, Aug  8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32

LogonUserW returned FALSE

这篇关于Python 3中LogonUserW中的访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆