Access Violation In Logonuserw In Python 3
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_l
Solution 1:
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 
HANDLEis passed viabyref 
 - In Python3 strings are wide by default, so no need for 
 
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  82017, 02:17:05) [MSC v.190064 bit (AMD64)] on win32
LogonUserW returned FALSE
Post a Comment for "Access Violation In Logonuserw In Python 3"