| 51 | |
| 52 | |
| 53 | def check_windows(): |
| 54 | if sys.platform == "win32": |
| 55 | lib_path = os.path.join(os.path.dirname(__file__), "core/lib") |
| 56 | dll_paths = list(filter(os.path.exists, [lib_path,])) |
| 57 | assert len(dll_paths) > 0 |
| 58 | |
| 59 | kernel32 = ctypes.WinDLL("kernel32.dll", use_last_error=True) |
| 60 | has_load_library_attr = hasattr(kernel32, "AddDllDirectory") |
| 61 | old_error_mode = kernel32.SetErrorMode(0x0001) |
| 62 | |
| 63 | kernel32.LoadLibraryW.restype = ctypes.c_void_p |
| 64 | if has_load_library_attr: |
| 65 | kernel32.AddDllDirectory.restype = ctypes.c_void_p |
| 66 | kernel32.LoadLibraryExW.restype = ctypes.c_void_p |
| 67 | |
| 68 | for dll_path in dll_paths: |
| 69 | if sys.version_info >= (3, 8): |
| 70 | os.add_dll_directory(dll_path) |
| 71 | elif has_load_library_attr: |
| 72 | res = kernel32.AddDllDirectory(dll_path) |
| 73 | if res is None: |
| 74 | err = ctypes.WinError(ctypes.get_last_error()) |
| 75 | err.strerror += ' Error adding "{}" to the DLL search PATH.'.format( |
| 76 | dll_path |
| 77 | ) |
| 78 | raise err |
| 79 | else: |
| 80 | print("WARN: python or OS env have some issue, may load DLL failed!!!") |
| 81 | |
| 82 | dlls = glob.glob(os.path.join(lib_path, "*.dll")) |
| 83 | path_patched = False |
| 84 | for dll in dlls: |
| 85 | is_loaded = False |
| 86 | if has_load_library_attr: |
| 87 | res = kernel32.LoadLibraryExW(dll, None, 0x00001100) |
| 88 | last_error = ctypes.get_last_error() |
| 89 | if res is None and last_error != 126: |
| 90 | err = ctypes.WinError(last_error) |
| 91 | err.strerror += ' Error loading "{}" or one of its dependencies.'.format( |
| 92 | dll |
| 93 | ) |
| 94 | err.strerror += " \nplease install VC runtime from: " |
| 95 | err.strerror += " \nhttps://docs.microsoft.com/en-us/cpp/windows/latest-supported-vc-redist?view=msvc-160" |
| 96 | raise err |
| 97 | elif res is not None: |
| 98 | is_loaded = True |
| 99 | if not is_loaded: |
| 100 | if not path_patched: |
| 101 | os.environ["PATH"] = ";".join(dll_paths + [os.environ["PATH"]]) |
| 102 | path_patched = True |
| 103 | res = kernel32.LoadLibraryW(dll) |
| 104 | if res is None: |
| 105 | err = ctypes.WinError(ctypes.get_last_error()) |
| 106 | err.strerror += ' Error loading "{}" or one of its dependencies.'.format( |
| 107 | dll |
| 108 | ) |
| 109 | err.strerror += " \nplease install VC runtime from: " |
| 110 | err.strerror += " \nhttps://docs.microsoft.com/en-us/cpp/windows/latest-supported-vc-redist?view=msvc-160" |