Author David Heffernan
Recipients David Heffernan, paul.moore, steve.dower, tim.golden, zach.ware
Date 2020-01-07.10:53:45
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
When creating an instance of CDLL (or indeed WinDLL) for a DLL that is already loaded, you pass the HMODULE in the handle argument to the constructor.

In older versions of ctypes you could pass None as the name argument when doing so. However, the changes in now mean that such code fails with a NoneType is not iterable error.

The relevant change is in __init__ for CDLL. The code inside the if == "nt" block sets up mode, but this is pointless is handle is not None. Because the mode variable is never used, rightly so because the DLL is already loaded.

The issue could be resolved by changing

if == "nt":


if == "nt" and handle is not None:

The following program demonstrates the issue:

import ctypes

handle = ctypes.windll.kernel32._handle
lib = ctypes.WinDLL(name=None, handle=handle)

This runs to completion in Python 3.7 and earlier, but fails in Python 3.8 and later:

Traceback (most recent call last):
  File "", line 5, in <module>
    lib = ctypes.WinDLL(name=None, handle=handle)
  File "C:\Program Files (x86)\Python\38\lib\ctypes\", line 359, in __init__
    if '/' in name or '\\' in name:
TypeError: argument of type 'NoneType' is not iterable
Date User Action Args
2020-01-07 10:53:45David Heffernansetrecipients: + David Heffernan, paul.moore, tim.golden, zach.ware, steve.dower
2020-01-07 10:53:45David Heffernansetmessageid: <>
2020-01-07 10:53:45David Heffernanlinkissue39243 messages
2020-01-07 10:53:45David Heffernancreate