Fix onnxruntime search dll path.#689
Merged
Merged
Conversation
xieofxie
approved these changes
May 21, 2026
Contributor
xieofxie
left a comment
There was a problem hiding this comment.
will it slow much? do we need to find the exact place before ort is loaded?
Contributor
Author
Didn't feel slow. Almost the same. |
DingmaomaoBJTU
approved these changes
May 21, 2026
timenick
reviewed
May 21, 2026
Collaborator
timenick
left a comment
There was a problem hiding this comment.
Review: missing regression coverage for the DLL preload mechanism.
timenick
approved these changes
May 21, 2026
Collaborator
|
Do we have proof that "C:\Windows\System32\onnxruntime.dll" is indeed loaded? how can I repro if yest? |
Collaborator
|
At least we should try this first, and see which DLLs get loaded import os, ctypes, onnxruntime as ort
from ctypes import wintypes
print("ort version:", ort.__version__)
print("ort package:", ort.__file__)
capi = os.path.join(os.path.dirname(ort.__file__), "capi")
for name in ("onnxruntime.dll", "onnxruntime_pybind11_state.pyd", "onnxruntime_providers_shared.dll", "DirectML.dll"):
p = os.path.join(capi, name)
print(f" {'OK ' if os.path.isfile(p) else 'NO '} {p}")
k32 = ctypes.WinDLL("kernel32")
k32.GetCurrentProcess.restype = wintypes.HANDLE
psapi = ctypes.WinDLL("psapi")
psapi.EnumProcessModules.argtypes = [wintypes.HANDLE, ctypes.POINTER(ctypes.c_void_p), wintypes.DWORD, ctypes.POINTER(wintypes.DWORD)]
psapi.EnumProcessModules.restype = wintypes.BOOL
psapi.GetModuleFileNameExW.argtypes = [wintypes.HANDLE, ctypes.c_void_p, wintypes.LPWSTR, wintypes.DWORD]
mods = (ctypes.c_void_p * 4096)(); need = wintypes.DWORD()
psapi.EnumProcessModules(k32.GetCurrentProcess(), mods, ctypes.sizeof(mods), ctypes.byref(need))
print("--- onnxruntime* modules currently mapped in process ---")
for i in range(need.value // ctypes.sizeof(ctypes.c_void_p)):
buf = ctypes.create_unicode_buffer(32768)
psapi.GetModuleFileNameExW(k32.GetCurrentProcess(), mods[i], buf, len(buf))
if "onnxruntime" in os.path.basename(buf.value).lower():
print(" ", buf.value) |
Contributor
timenick
added a commit
that referenced
this pull request
May 22, 2026
- Document that _IMAGE_FILE_MACHINE_TO_NAME is intentionally limited to the host architectures Windows 11 ships on (no 32-bit ARM, no IA64); unmapped values fall through to None and log at debug level. - Log ctypes.get_last_error() when IsWow64Process2 returns 0 so a regression does not silently fall back to platform.machine(). - Hoist kernel32 + IsWow64Process2 to module scope, gated by sys.platform == "win32", so LoadLibrary runs once per process and non-Windows callers don't hit a ctypes.WinDLL error path. - Add an explicit `if sys.platform != "win32": return None` guard in _get_windows_native_machine to match the pattern used in _preload_bundled_onnxruntime_dll (#689). - Split the raw Win32 query (_query_native_machine_via_win32) from the name mapping (_get_windows_native_machine) and parametrize tests over IMAGE_FILE_MACHINE_* integers so a rename of "ARM64" → "Arm64" in the table fails the test instead of silently shipping.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Windows ships C:\Windows\System32\onnxruntime.dll (older API version) as part of the system WindowsML component.
When WinML EP plugin DLLs are loaded (via EpCatalog), they import "onnxruntime.dll" by base name and the loader binds them to the system copy, producing "The requested API version [N] is not available" errors.
Loading the wheel-bundled DLL by full path first makes later base-name imports resolve to the already-loaded module.