This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: ctypes FreeLibrary fails on Windows 64-bit
Type: Stage: resolved
Components: ctypes Versions: Python 3.7, Python 3.6, Python 3.4, Python 3.5
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, belopolsky, eryksun, giampaolo.rodola, meador.inge, theller
Priority: normal Keywords:

Created on 2017-05-05 18:59 by giampaolo.rodola, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg293135 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2017-05-05 18:59
>>> import ctypes
>>> path = 'C:\\Python35-64\\vcruntime140.dll'
>>> cfile = ctypes.CDLL(path)
>>> cfile._handle
>>> ctypes.windll.kernel32.FreeLibrary(cfile._handle)
Traceback (most recent call last):
ctypes.ArgumentError: argument 1: <class 'OverflowError'>: int too long to convert

Everything is fine on 32-bit.
msg293136 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2017-05-05 19:08

Adding "ctypes.windll.kernel32.FreeLibrary.argtypes = [wintypes.HMODULE]" fixes the issue. This works: 

import ctypes
from ctypes import wintypes
path = 'C:\\Python35-64\\vcruntime140.dll'
cfile = ctypes.CDLL(path)
ctypes.windll.kernel32.FreeLibrary.argtypes = [wintypes.HMODULE]
msg293149 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2017-05-06 02:48
It's documented that the default conversion for integer arguments is a 32-bit C int, which is also the default for result types. Whenever pointers (and Windows handles, which are sometimes 64-bit pointers, such as HMODULE values) are passed as arguments, integer values either need to be manually wrapped as ctypes pointer instances, or you need to set the function's argtypes to override the default behavior. Whenever a pointer is returned, you must set the function's restype.

BTW, _ctypes.FreeLibrary and _ctypes.dlclose already exist as built-in functions. They're just not imported to the main ctypes namespace, most likely because there's nothing in the design to prevent crashing Python if the shared library's reference count drops to 0 and the loader unmaps it from the process while there's remaining references to its address range. 

Also, I recommend using ctypes.WinDLL and avoiding ctypes.windll, especially for common Windows APIs. It caches libraries, which cache function pointer instances. What if my library also uses it and customizes FreeLibrary.argtypes or FreeLibrary.errcheck in a way that breaks yours? It also doesn't allow setting use_last_error=True to capture the last error value in a thread-local variable that can be accessed via ctypes.get_last_error() and ctypes.set_last_error(). Capturing the last error value is a good idea in a high-level language like Python -- especially when working interactively in the REPL. This way ctypes.WinError(ctypes.get_last_error()) will reliable create an exception for whatever the error value was for the last FFI call.
msg293188 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2017-05-07 12:21
> It's documented that the default conversion for integer arguments is a 32-bit C int. [...] Whenever a pointer is returned, you must set the function's restype.

OK, clear. Closing this out.
Date User Action Args
2022-04-11 14:58:46adminsetgithub: 74472
2017-05-07 12:21:44giampaolo.rodolasetstatus: open -> closed
resolution: not a bug
messages: + msg293188

stage: resolved
2017-05-06 02:48:30eryksunsetnosy: + eryksun
messages: + msg293149
2017-05-05 19:08:55giampaolo.rodolasetmessages: + msg293136
2017-05-05 19:00:38giampaolo.rodolasetnosy: + theller, amaury.forgeotdarc, belopolsky, meador.inge
2017-05-05 18:59:55giampaolo.rodolacreate