A Python ctypes call into a DLL raises OSError: access violation reading
Fix it fast
Most common cause, 1 of 3A pointer argument reaches the C code as NULL
Hand the function memory it can write to.
from ctypes import CDLL, byref, c_int, create_string_buffer
lib = CDLL(r".\mylib.dll")
count = c_int()
name = create_string_buffer(256)
lib.get_info(byref(count), name, 256)
print(count.value, name.value)Short answer
OSError: exception: access violation reading 0x0000000000000000 means the C function followed a NULL or broken pointer. With ctypes the usual causes are passing None or an empty POINTER() where the DLL needs real memory, or a missing restype or argtypes that squeezes a 64-bit pointer into a C int. Declare the prototype and pass byref() or a buffer. Python 3.14-3.15.
Which cause do you have?
Print the prototype ctypes is using for the failing function. argtypes printed as None means no prototype was declared (cause 2 or 3). If it is set, look at the arguments you pass for None or an empty pointer (cause 1).
python -c "import ctypes; f = ctypes.CDLL(r'.\mylib.dll').my_func; print(f.argtypes, f.restype)"Then find the line you see in this list and open its fix:
- 1A pointer argument reaches the C code as NULL
The address in the message is 0x0000000000000000, and one argument is None, 0 or a pointer object created with no target - 2restype is not set, so a returned pointer comes back as a C int
The failing call receives a handle or struct pointer that an earlier ctypes call returned, and that earlier function has no restype - 3argtypes is missing, so arguments are converted the wrong way
argtypes prints None, and the call passes Python ints for handles or sizes, or a different number of arguments than the C header lists
What does the error look like?
OSError: exception: access violation reading 0x0000000000000000OSError: exception: access violation writing 0x0000000000000000OSError: exception: access violation reading 0x00000020
Cause 1 of 3
A pointer argument reaches the C code as NULL
You seeThe address in the message is 0x0000000000000000, and one argument is None, 0 or a pointer object created with no target
Check
An access violation reading 0x0000000000000000 from a ctypes call means the C side dereferenced a NULL pointer. ctypes sends None as a C NULL pointer, and calling a pointer type with no argument, such as POINTER(c_int)(), also gives a NULL pointer. Such an object is False when tested with bool().
from ctypes import POINTER, c_int
p = POINTER(c_int)()
print(bool(p)) # False: this pointer is NULLFix
Hand the function memory it can write to. Create a ctypes value and pass it with byref() for an output number, and pass a buffer from create_string_buffer() sized as the C header says for an output string.
from ctypes import CDLL, byref, c_int, create_string_buffer
lib = CDLL(r".\mylib.dll")
count = c_int()
name = create_string_buffer(256)
lib.get_info(byref(count), name, 256)
print(count.value, name.value)Verify
The call returns normally, count.value holds the number the DLL wrote and name.value holds the text.
python call_dll.pyConfirmed by docs.python.org
Cause 2 of 3
restype is not set, so a returned pointer comes back as a C int
You seeThe failing call receives a handle or struct pointer that an earlier ctypes call returned, and that earlier function has no restype
Check
The ctypes docs state that foreign functions are assumed to return a C int unless restype says otherwise. On Windows a C int is 4 bytes, so a function that returns a 64-bit pointer hands Python a value that is no longer the real address, and the next call that uses it reads the wrong memory.
python -c "import ctypes; print(ctypes.CDLL(r'.\mylib.dll').open_handle.restype)"Fix
Declare restype as c_void_p (or POINTER(YourStruct)) on every function that returns a pointer, and declare argtypes on the function that receives it, so the full value travels both ways.
from ctypes import CDLL, c_int, c_void_p
lib = CDLL(r".\mylib.dll")
lib.open_handle.restype = c_void_p
lib.read_value.argtypes = [c_void_p]
lib.read_value.restype = c_int
h = lib.open_handle()
print(lib.read_value(h))Verify
read_value prints its number and the OSError no longer appears.
python call_dll.pyConfirmed by docs.python.org
Cause 3 of 3
argtypes is missing, so arguments are converted the wrong way
You seeargtypes prints None, and the call passes Python ints for handles or sizes, or a different number of arguments than the C header lists
Check
Without argtypes, ctypes cannot know the C signature. It does not check how many arguments you pass, and every Python int goes out as a C int with its value masked to fit. The docs warn that a call with incorrect arguments can corrupt memory or crash Python.
python -c "import ctypes; print(ctypes.CDLL(r'.\mylib.dll').scale.argtypes)"Fix
Copy each prototype from the C header into argtypes and restype. ctypes then converts every value to the declared type, accepts a matching array where a pointer is declared, and raises TypeError when too few arguments arrive.
# C header: int scale(double *values, size_t n, double factor);
from ctypes import CDLL, POINTER, c_double, c_int, c_size_t
lib = CDLL(r".\mylib.dll")
lib.scale.argtypes = [POINTER(c_double), c_size_t, c_double]
lib.scale.restype = c_int
data = (c_double * 3)(1.0, 2.0, 3.0)
lib.scale(data, len(data), 2.0)
print(list(data))Verify
The list prints the values the C function changed, and calling scale with a missing argument now raises TypeError instead of crashing.
python call_dll.pyConfirmed by docs.python.org
How do I stop it coming back?
- Keep every ctypes prototype in one module that mirrors the C header, so argtypes and restype are set before the first call.
- Give each pointer-returning function an errcheck callable that raises when the result is None, so a NULL never travels into the next call.
- Add a small test that calls each exported function once with known inputs, so a changed DLL signature fails in CI and not in production.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.