Skip to content

Commit a576327

Browse files
committed
Bug fix in load_dl_windows.py: ctypes.windll.kernel32.LoadLibraryW() returns an incompatible handle. Use win32api.LoadLibraryEx() instead to ensure self-consistency.
1 parent 55583d9 commit a576327

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

cuda_bindings/cuda/bindings/_path_finder/load_dl_common.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,19 @@
55
from typing import Callable, Optional
66

77
from cuda.bindings._path_finder.run_python_code_safely import run_python_code_safely
8-
from cuda.bindings._path_finder.supported_libs import DIRECT_DEPENDENCIES
8+
from cuda.bindings._path_finder.supported_libs import DIRECT_DEPENDENCIES, IS_WINDOWS
99

10+
if IS_WINDOWS:
11+
import pywintypes
1012

11-
@dataclass
12-
class LoadedDL:
13-
"""Represents a loaded dynamic library.
13+
HandleType = pywintypes.HANDLE
14+
else:
15+
HandleType = int
1416

15-
Attributes:
16-
handle: The library handle (can be converted to void* in Cython)
17-
abs_path: The absolute path to the library file
18-
was_already_loaded_from_elsewhere: Whether the library was already loaded
19-
"""
2017

21-
# ATTENTION: To convert `handle` back to `void*` in cython:
22-
# Linux: `cdef void* ptr = <void*><uintptr_t>`
23-
# Windows: `cdef void* ptr = <void*><intptr_t>`
24-
handle: int
18+
@dataclass
19+
class LoadedDL:
20+
handle: HandleType
2521
abs_path: Optional[str]
2622
was_already_loaded_from_elsewhere: bool
2723

cuda_bindings/cuda/bindings/_path_finder/load_dl_windows.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def add_dll_directory(dll_abs_path: str) -> None:
3535
os.environ["PATH"] = dirpath if curr_path is None else os.pathsep.join((curr_path, dirpath))
3636

3737

38-
def abs_path_for_dynamic_library(libname: str, handle: int) -> str:
38+
def abs_path_for_dynamic_library(libname: str, handle: pywintypes.HANDLE) -> str:
3939
"""Get the absolute path of a loaded dynamic library on Windows.
4040
4141
Args:
@@ -117,8 +117,11 @@ def load_with_system_search(libname: str, _unused: str) -> Optional[LoadedDL]:
117117
from cuda.bindings._path_finder.supported_libs import SUPPORTED_WINDOWS_DLLS
118118

119119
for dll_name in SUPPORTED_WINDOWS_DLLS.get(libname, ()):
120-
handle = ctypes.windll.kernel32.LoadLibraryW(ctypes.c_wchar_p(dll_name))
121-
if handle:
120+
try:
121+
handle = win32api.LoadLibraryEx(dll_name, 0, 0)
122+
except pywintypes.error:
123+
continue
124+
else:
122125
return LoadedDL(handle, abs_path_for_dynamic_library(libname, handle), False)
123126

124127
return None

0 commit comments

Comments
 (0)