Skip to content

ctypes.util.find_library() does not find DLLs in directories added by os.add_dll_directory() #132328

@davidva-ecco

Description

@davidva-ecco

Bug report

Bug description:

Python version: 3.12.6
OS: Windows 11 64-bit

Behavior

When I use ctypes.util.find_library(<DLL name>) to search for a DLL in a Windows directory added to the search path with os.add_dll_directory(<DLL folder>), it is not found. However, if I provide find_library() with the path and name of the DLL, it is found.

Unfortunately this breaks a third-party module I am importing that uses find_library(<DLL name>). I can open a ticket with the module developers, but it did work previously when I first wrote the script, although I don't remember which Python version I had installed at the time (I will try older versions to see if I can find where it broke and update this ticket).

Expected Behavior

ctypes.util.find_library(<DLL name>) locates <DLL name>.dll in a directory added with os.add_dll_directory().

Example Script

Copy this script to a file in the same directory as vxlapi64.dll (Vector XL Driver DLL) and run it with python <script name>.py. I believe the issue is reproduceable with a different DLL in the same directory, but the script will need the DLL names updated. The DLL should also not be present in the system location, e.g. C:/Windows/System32.

import os
from ctypes.util import find_library

# Add the directory this script is in (also contains vxlapi64.dll)
add_obj = os.add_dll_directory(os.path.dirname(__file__))

# Check for library using DLL name
if find_library("vxlapi64"):
    print("vxlapi64.dll from add_dll_directory found")
else:
    print("vxlapi64.dll from add_dll_directory NOT FOUND")

# Check for library using DLL path + name
if find_library(os.path.dirname(__file__) + "/vxlapi64"):
    print("vxlapi64.dll from absolute path found")
else:
    print("vxlapi64.dll from absolute path NOT FOUND")

Output:

vxlapi64.dll from add_dll_directory NOT FOUND
vxlapi64.dll from absolute path found

Is there another way to add a DLL so an imported module can locate it with find_library(<DLL name>)?

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    OS-windowstype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions