From 555f0901a3906b363e767b6209a8da91191953d1 Mon Sep 17 00:00:00 2001 From: Varun Jaiswal <96684656+varun-jaiswal17@users.noreply.github.com> Date: Tue, 16 Jun 2026 21:24:18 +0530 Subject: [PATCH] Merge pull request #29309 from varun-jaiswal17:fix_dll_mismatch Fix ONNXRuntime dll path mismatch #29309 **1. C2664 build error in `net_impl_backend.cpp`** `EnableProfiling()` expects `const wchar_t*` on Windows (`ORTCHAR_T`), but was passed `const char*`. Fixed by converting to `std::wstring`, as suggested in #29278 --- **2. Wrong ORT DLL loaded at runtime (`modules/dnn/CMakeLists.txt`)** With `DOWNLOAD_ONNXRUNTIME=ON`, the DLL glob only searched `bin/` but the downloaded package places DLLs in `lib/`. This left the build tree with no ORT DLL, causing Windows to fall back to the stale `System32\onnxruntime.dll` (1.17.1), crashing against the ORT 1.25.1 API. Fixed by adding `lib/` as fallback and staging DLLs into the build bin directory at configure time. Closes : #29278 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake --- modules/dnn/CMakeLists.txt | 14 ++++++++++++++ modules/dnn/src/net_impl_backend.cpp | 5 +++++ 2 files changed, 19 insertions(+) diff --git a/modules/dnn/CMakeLists.txt b/modules/dnn/CMakeLists.txt index a76548363c..358920fb3d 100644 --- a/modules/dnn/CMakeLists.txt +++ b/modules/dnn/CMakeLists.txt @@ -340,9 +340,23 @@ if(WITH_ONNXRUNTIME) if(WIN32) if(ONNXRT_ROOT_DIR) file(GLOB _ort_runtime_libs "${ONNXRT_ROOT_DIR}/bin/onnxruntime*.dll") + if(NOT _ort_runtime_libs) + file(GLOB _ort_runtime_libs "${ONNXRT_ROOT_DIR}/lib/onnxruntime*.dll") + endif() endif() if(_ort_runtime_libs) install(FILES ${_ort_runtime_libs} DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT libs) + foreach(_ort_dll ${_ort_runtime_libs}) + if(MSVC_IDE) + execute_process( + COMMAND ${CMAKE_COMMAND} -E copy_if_different "${_ort_dll}" "${EXECUTABLE_OUTPUT_PATH}/Release/" + COMMAND ${CMAKE_COMMAND} -E copy_if_different "${_ort_dll}" "${EXECUTABLE_OUTPUT_PATH}/Debug/") + elseif(MSVC AND (CMAKE_GENERATOR MATCHES "Visual")) + execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different "${_ort_dll}" "${EXECUTABLE_OUTPUT_PATH}/${CMAKE_BUILD_TYPE}/") + else() + execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different "${_ort_dll}" "${EXECUTABLE_OUTPUT_PATH}/") + endif() + endforeach() endif() elseif(APPLE) if(ONNXRT_ROOT_DIR) diff --git a/modules/dnn/src/net_impl_backend.cpp b/modules/dnn/src/net_impl_backend.cpp index 0e4bdf1f41..47536c0ebf 100644 --- a/modules/dnn/src/net_impl_backend.cpp +++ b/modules/dnn/src/net_impl_backend.cpp @@ -96,7 +96,12 @@ void Net::Impl::finalizeOrt() ort_profile_data.clear(); if (profilingMode != DNN_PROFILE_NONE) { ort_profile_path_prefix = cv::tempfile("opencv_ort_profile_"); +#ifdef _WIN32 + std::wstring w_profile_path(ort_profile_path_prefix.begin(), ort_profile_path_prefix.end()); + opts.EnableProfiling(w_profile_path.c_str()); +#else opts.EnableProfiling(ort_profile_path_prefix.c_str()); +#endif } #ifdef _WIN32