|
| 1 | +# Copyright 2025 NVIDIA Corporation. All rights reserved. |
| 2 | +# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE |
| 3 | + |
| 4 | +import ctypes |
| 5 | +import ctypes.wintypes |
| 6 | +import functools |
| 7 | +from typing import Optional |
| 8 | + |
| 9 | +import pywintypes |
| 10 | +import win32api |
| 11 | + |
| 12 | +from .load_dl_common import LoadedDL, add_dll_directory |
| 13 | + |
| 14 | +# Mirrors WinBase.h (unfortunately not defined already elsewhere) |
| 15 | +WINBASE_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR = 0x00000100 |
| 16 | +WINBASE_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS = 0x00001000 |
| 17 | + |
| 18 | + |
| 19 | +def abs_path_for_dynamic_library(handle: int) -> str: |
| 20 | + """Get the absolute path of a loaded dynamic library on Windows. |
| 21 | +
|
| 22 | + Args: |
| 23 | + handle: The library handle |
| 24 | +
|
| 25 | + Returns: |
| 26 | + The absolute path to the DLL file |
| 27 | +
|
| 28 | + Raises: |
| 29 | + OSError: If GetModuleFileNameW fails |
| 30 | + """ |
| 31 | + buf = ctypes.create_unicode_buffer(260) |
| 32 | + n_chars = ctypes.windll.kernel32.GetModuleFileNameW(ctypes.wintypes.HMODULE(handle), buf, len(buf)) |
| 33 | + if n_chars == 0: |
| 34 | + raise OSError("GetModuleFileNameW failed") |
| 35 | + return buf.value |
| 36 | + |
| 37 | + |
| 38 | +@functools.cache |
| 39 | +def cuDriverGetVersion() -> int: |
| 40 | + """Get the CUDA driver version. |
| 41 | +
|
| 42 | + Returns: |
| 43 | + The CUDA driver version number |
| 44 | +
|
| 45 | + Raises: |
| 46 | + AssertionError: If the driver version cannot be obtained |
| 47 | + """ |
| 48 | + handle = win32api.LoadLibrary("nvcuda.dll") |
| 49 | + |
| 50 | + kernel32 = ctypes.WinDLL("kernel32", use_last_error=True) |
| 51 | + GetProcAddress = kernel32.GetProcAddress |
| 52 | + GetProcAddress.argtypes = [ctypes.wintypes.HMODULE, ctypes.wintypes.LPCSTR] |
| 53 | + GetProcAddress.restype = ctypes.c_void_p |
| 54 | + cuDriverGetVersion = GetProcAddress(handle, b"cuDriverGetVersion") |
| 55 | + assert cuDriverGetVersion |
| 56 | + |
| 57 | + FUNC_TYPE = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.POINTER(ctypes.c_int)) |
| 58 | + cuDriverGetVersion_fn = FUNC_TYPE(cuDriverGetVersion) |
| 59 | + driver_ver = ctypes.c_int() |
| 60 | + err = cuDriverGetVersion_fn(ctypes.byref(driver_ver)) |
| 61 | + assert err == 0 |
| 62 | + return driver_ver.value |
| 63 | + |
| 64 | + |
| 65 | +def check_if_already_loaded(libname: str) -> Optional[LoadedDL]: |
| 66 | + """Check if the library is already loaded in the process. |
| 67 | +
|
| 68 | + Args: |
| 69 | + libname: The name of the library to check |
| 70 | +
|
| 71 | + Returns: |
| 72 | + A LoadedDL object if the library is already loaded, None otherwise |
| 73 | +
|
| 74 | + Example: |
| 75 | + >>> loaded = check_if_already_loaded("cudart") |
| 76 | + >>> if loaded is not None: |
| 77 | + ... print(f"Library already loaded from {loaded.abs_path}") |
| 78 | + """ |
| 79 | + from .supported_libs import SUPPORTED_WINDOWS_DLLS |
| 80 | + |
| 81 | + for dll_name in SUPPORTED_WINDOWS_DLLS.get(libname, ()): |
| 82 | + try: |
| 83 | + handle = win32api.GetModuleHandle(dll_name) |
| 84 | + except pywintypes.error: |
| 85 | + continue |
| 86 | + else: |
| 87 | + return LoadedDL(handle, abs_path_for_dynamic_library(handle), True) |
| 88 | + return None |
| 89 | + |
| 90 | + |
| 91 | +def load_with_system_search(name: str, _unused: str) -> Optional[LoadedDL]: |
| 92 | + """Try to load a DLL using system search paths. |
| 93 | +
|
| 94 | + Args: |
| 95 | + name: The name of the library to load |
| 96 | + _unused: Unused parameter (kept for interface consistency) |
| 97 | +
|
| 98 | + Returns: |
| 99 | + A LoadedDL object if successful, None if the library cannot be loaded |
| 100 | + """ |
| 101 | + from .supported_libs import SUPPORTED_WINDOWS_DLLS |
| 102 | + |
| 103 | + driver_ver = cuDriverGetVersion() |
| 104 | + del driver_ver # Keeping this here because it will probably be needed in the future. |
| 105 | + |
| 106 | + dll_names = SUPPORTED_WINDOWS_DLLS.get(name) |
| 107 | + if dll_names is None: |
| 108 | + return None |
| 109 | + |
| 110 | + for dll_name in dll_names: |
| 111 | + handle = ctypes.windll.kernel32.LoadLibraryW(ctypes.c_wchar_p(dll_name)) |
| 112 | + if handle: |
| 113 | + return LoadedDL(handle, abs_path_for_dynamic_library(handle), False) |
| 114 | + |
| 115 | + return None |
| 116 | + |
| 117 | + |
| 118 | +def load_with_abs_path(libname: str, found_path: str) -> LoadedDL: |
| 119 | + """Load a dynamic library from the given path. |
| 120 | +
|
| 121 | + Args: |
| 122 | + libname: The name of the library to load |
| 123 | + found_path: The absolute path to the DLL file |
| 124 | +
|
| 125 | + Returns: |
| 126 | + A LoadedDL object representing the loaded library |
| 127 | +
|
| 128 | + Raises: |
| 129 | + RuntimeError: If the DLL cannot be loaded |
| 130 | + """ |
| 131 | + from .supported_libs import LIBNAMES_REQUIRING_OS_ADD_DLL_DIRECTORY |
| 132 | + |
| 133 | + if libname in LIBNAMES_REQUIRING_OS_ADD_DLL_DIRECTORY: |
| 134 | + add_dll_directory(found_path) |
| 135 | + |
| 136 | + flags = WINBASE_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | WINBASE_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR |
| 137 | + try: |
| 138 | + handle = win32api.LoadLibraryEx(found_path, 0, flags) |
| 139 | + except pywintypes.error as e: |
| 140 | + raise RuntimeError(f"Failed to load DLL at {found_path}: {e}") from e |
| 141 | + return LoadedDL(handle, found_path, False) |
0 commit comments