Fix onnx runtime loader for custom model

This commit is contained in:
2026-05-05 16:23:01 +10:00
parent b0002b4a4b
commit 66c67dff26
3 changed files with 64 additions and 12 deletions
+47 -2
View File
@@ -1,5 +1,6 @@
#include "ANSCUSTOMDetector.h"
#include "Utility.h"
#include "EPLoader.h"
#include <windows.h>
namespace ANSCENTER
@@ -279,12 +280,56 @@ namespace ANSCENTER
return false;
}
// 2b. Pre-load ANSCENTER's onnxruntime.dll into the process before
// LoadLibrary on the custom plugin. The plugin DLL has a static
// import of "onnxruntime.dll"; if no module with that base name is
// loaded yet, the Windows loader falls back to the safe DLL search
// path and binds to C:\Windows\System32\onnxruntime.dll (the
// Windows ML build, ORT 1.17). That older runtime does not support
// the API version the plugin's headers ask for, so its static-init
// of Ort::Global<void>::api_ becomes nullptr and the first Ort::
// call inside the plugin crashes. Once EPLoader has loaded the
// matching ORT, by-name match in the loader binds the plugin's
// import to it instead.
try {
const auto& ep = ANSCENTER::EPLoader::Current();
ANS_DBG("ANSCUSTOM_CreateDet",
"EP_PRELOADED type=%s dir=%s fromSubdir=%d",
ANSCENTER::EPLoader::EngineTypeName(ep.type),
ep.libraryDir.c_str(),
ep.fromSubdir ? 1 : 0);
}
catch (const std::exception& e) {
ANS_DBG("ANSCUSTOM_CreateDet",
"ERROR EPLoader::Current threw — plugin may bind to wrong onnxruntime.dll: %s",
e.what());
this->_logger.LogFatal("ANSCUSTOMDETECTOR::Initialize",
(std::string("EPLoader::Current failed: ") + e.what()).c_str(),
__FILE__, __LINE__);
return false;
}
// 3. Load the library
//
// Use LoadLibraryExW with LOAD_LIBRARY_SEARCH_DEFAULT_DIRS so the
// plugin's transitive imports (notably "onnxruntime.dll") are
// resolved against directories added with AddDllDirectory — i.e.
// the staging dir EPLoader copied the matching ORT into. Plain
// LoadLibrary uses legacy safe-DLL search, which only consults
// app dir + System32 + PATH and ignores AddDllDirectory entirely;
// that path picks up C:\Windows\System32\onnxruntime.dll (the
// Windows ML build, ORT 1.17), and the plugin's static-init of
// Ort::Global<void>::api_ becomes nullptr — first Ort:: call
// crashes. LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR also lets a custom
// model bundle its own deps next to the plugin DLL if it wants.
std::wstring dllPath = String2WString(_modelFilePath);
hMod = LoadLibrary(dllPath.c_str());
hMod = LoadLibraryExW(
dllPath.c_str(),
nullptr,
LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR);
if (!hMod) {
DWORD err = GetLastError();
ANS_DBG("ANSCUSTOM_CreateDet", "ERROR LoadLibrary failed path=%s err=%lu",
ANS_DBG("ANSCUSTOM_CreateDet", "ERROR LoadLibraryEx failed path=%s err=%lu",
_modelFilePath.c_str(), (unsigned long)err);
this->_logger.LogFatal("ANSCUSTOMDETECTOR::Initialize", "Cannot open library", __FILE__, __LINE__);
return false;