ctypes.windll is a loader object in Python's ctypes module that opens Windows DLLs and lets you call their exported functions with the stdcall calling convention, which most Win32 API functions use. It exists only on Windows. On Linux and macOS, ctypes.windll is not defined, so code that touches it fails with AttributeError.
It is a thin way to reach the Windows API from Python without installing anything, but you must declare argument and return types yourself, or calls can fail in confusing ways.
ctypes.windll is a LibraryLoader for ctypes.WinDLL. Accessing an attribute loads the DLL of that name, so ctypes.windll.user32 opens user32.dll and ctypes.windll.kernel32 opens kernel32.dll.
| Loader | Convention | Platform |
|---|---|---|
ctypes.windll / WinDLL | stdcall | Windows only |
ctypes.oledll / OleDLL | stdcall, raises OSError on failed HRESULT | Windows only |
ctypes.cdll / CDLL | cdecl | All platforms |
On 64-bit Windows there is a single calling convention, so windll and cdll behave the same for 64-bit DLLs. The difference matters for 32-bit Python, where calling a stdcall function through cdll corrupts the stack.
For portable code, guard the access:
Here is a message box through user32.dll. The W suffix selects the Unicode version of the function, which is the right choice in current code:
The return value 1 is IDOK. Passing None as the window handle gives a message box with no owner. Using ctypes.WinDLL("user32", use_last_error=True) is equivalent to ctypes.windll.user32, but it lets you ask for private error state.
Without declarations, ctypes assumes every argument is a C int and the result is an int. That truncates 64-bit handles and pointers. Types from ctypes.wintypes (HWND, DWORD, HANDLE, LPCWSTR, BOOL and others) map directly to the Windows headers.
Your numbers will differ. Setting restype to the true return type is what makes GetTickCount64 return a 64-bit value instead of a truncated one.
Many Win32 functions return 0 or NULL and store the reason in a per-thread error code. With use_last_error=True, read it with ctypes.get_last_error() and turn it into an exception with ctypes.WinError:
Do not call kernel32.GetLastError() directly. The Python interpreter may run other Windows calls between your call and the next one, which overwrites the value. get_last_error() returns a copy that ctypes saved immediately after your call.
str where the function expects ANSI text. The A functions take bytes, the W functions take str. Prefer W.argtypes on 64-bit Python and crashing on handle arguments.PATH. Call os.add_dll_directory() for the folder holding them.ctypes cannot check it.For common tasks, use maintained wrappers instead: the standard library (os, msvcrt, winreg, winsound), pywin32 for the broad Win32 and COM surface, or comtypes for COM automation.
No. windll is defined only on Windows. On other systems use ctypes.cdll or ctypes.CDLL to load shared libraries.
windll uses the stdcall convention and cdll uses cdecl. On 64-bit Windows they are the same in practice, but 32-bit code must use the matching one.
Load the DLL with use_last_error=True, then raise ctypes.WinError(ctypes.get_last_error()) when the function reports failure.
It works, but mistakes crash the interpreter, so declare every signature and wrap calls in small tested functions. For broad API use, a maintained package such as pywin32 is safer.
ctypes.windll opens Windows DLLs and calls stdcall functions, and only on Windows. Use the Unicode W functions, declare argtypes and restype with ctypes.wintypes, load with use_last_error=True and report failures through ctypes.WinError, and reach for pywin32 or the standard library when a wrapper already exists.