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
andrestype
(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:
Notes:
argtypes
/restypes
:encode()
HANDLE
is passed viabyref
Output: