cuda-python icon indicating copy to clipboard operation
cuda-python copied to clipboard

[pathfinder] Use `LOAD_WITH_ALTERED_SEARCH_PATH` for system DLL search on Windows

Open rwgk opened this issue 1 month ago • 2 comments

WIP WIP WIP — Bug discovered while testing suggested code for NVIDIA/numba-cuda#308

This fix was generated by Cursor. I still need to review it myself.

The bug went unnoticed because on Windows we always have CUDA_HOME or CUDA_PATH set in our testing.


When loading CUDA DLLs via system search on Windows, the previous approach using LoadLibraryExW with flags=0 would find the DLL on PATH but fail to locate its co-located dependencies (error 126).

This fix uses SearchPathW to first find the DLL's full path, then loads it with LOAD_WITH_ALTERED_SEARCH_PATH so Windows searches for dependencies starting from the DLL's directory.

rwgk avatar Jan 16 '26 06:01 rwgk

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

copy-pr-bot[bot] avatar Jan 16 '26 06:01 copy-pr-bot[bot]

Archiving proof-of-concept code:

# LoadLibraryExW_nvrtc64_130_0_dll.py
import ctypes
kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)

dll_path = r"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.1\bin\x64\nvrtc64_130_0.dll"
LOAD_WITH_ALTERED_SEARCH_PATH = 0x8

print("Trying with full path + LOAD_WITH_ALTERED_SEARCH_PATH...")
handle = kernel32.LoadLibraryExW(dll_path, None, LOAD_WITH_ALTERED_SEARCH_PATH)
if handle:
    print(f"SUCCESS: handle={handle}")
else:
    error = ctypes.get_last_error()
    print(f"Error code: {error}")
    print(f"Error message: {ctypes.FormatError(error)}")

rwgk avatar Jan 16 '26 06:01 rwgk