FreeLibrary takes a handle defined as a pointer to C void * . See Windows Data Types . Set this in the argtypes function argtypes :
import ctypes from ctypes import wintypes kernel32 = ctypes.WinDLL('kernel32', use_last_error=True) kernel32.FreeLibrary.argtypes = [wintypes.HMODULE]
The default conversion of Python int or long (renamed int in Python 3) refers to C long , which is then passed to C int . Microsoft uses a 32-bit long even on 64-bit Windows, so the conversion raises an OverflowError .
On platforms with a 64-bit long (i.e. almost any other 64-bit OS), passing the pointer as a Python integer without defining the argtypes function can actually secure the process. The initial conversion to long works fine because it is the same size as the pointer. However, subsequent casting to a 32-bit C int may silently truncate the value.
source share