shimat / opencvsharp

OpenCV wrapper for .NET
Apache License 2.0
5.39k stars 1.15k forks source link

Unable to load 'OpenCvSharpExtern' from subfolder using OpenCvSharp 4.5.2.20210404 #1257

Closed moleson21 closed 3 years ago

moleson21 commented 3 years ago

Summary of your issue

Upgraded version of OpenCvSharp from 4.5.1.20210210 to 4.5.2.20210404 and started getting System.TypeInitializationException due to "System.DllNotFoundExecption: Unable to load DLL 'OpenCvSharpExtern'" with no changes in our applications code.

Looking into the commit changes between the OpenCvSharp versions, think the error is most likely due to the addition of this line:

Change Commit Link: https://github.com/shimat/opencvsharp/blob/110a77369c45761172253fa9db8b8d16fa847088/src/OpenCvSharp/Internal/PInvoke/NativeMethods/NativeMethods.cs#L15

Master branch link: https://github.com/shimat/opencvsharp/blob/master/src/OpenCvSharp/Internal/PInvoke/NativeMethods/NativeMethods.cs#L15

This is preventing the DLL from using its current directory as a search path. Since our application uses a nested folder structure with DLLs in subfolders, this is causing the DLL load to fail and the application to crash. By adding OpenCvSharpExtern.dll to the same folder as the applications main executable, the application successfully runs.

Environment

Visual Studio 2019 on Windows 10 .NetFramework 4.8

OpenCvSharp4.runtime.win OpenCvSharp4 OpenCvSharp4.Windows OpenCvSharp4.WpfExtensions

OpenCvSharp4 Version: 4.5.2.20210404

What did you do when you faced the problem?

Added the OpenCvSharpExtern.dll to the same folder as the applications main executable. This successfully loaded the DLL and started the application.

Example code:

paste your core code

Output:

paste your core code

What did you intend to be?

moleson21 commented 3 years ago

Built OpenCVSharp locally with the line in question commented out and was able to successfully load OpenCvSharpExtern.dll from the sub-directory again. Building locally should be an acceptable solution for me since I am going to look into using GPU as well. Not sure if not being able to load from a sub-directory is an intended change but will mark this as issue as closed. Thanks!