opencvsharp: Unable to load DLL 'OpenCvSharpExtern' in 4.2.0
Summary of your issue
System.DllNotFoundException: Unable to load DLL 'OpenCvSharpExtern': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
Environment
Windows 10 1903 18362.535 64-bit VS 2019 16.4.2 Target .NET Framework 4.7.2 OpenCvSharp4.Windows 4.2.0.20191223
What did you do when you faced the problem?
Created a new solution (class library), ran a test for the code below.
Example code:
public class Class42
{
public Class42()
{
new Mat();
}
}
Output:
System.TypeInitializationException: The type initializer for 'OpenCvSharp.Mat' threw an exception. ---> System.TypeInitializationException: The type initializer for 'OpenCvSharp.NativeMethods' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'OpenCvSharpExtern': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
Stack Trace:
NativeMethods.redirectError(CvErrorCallback errCallback, IntPtr userdata, IntPtr& prevUserdata)
NativeMethods.LoadLibraries(IEnumerable`1 additionalPaths) line 79
NativeMethods.cctor() line 40
--- End of inner exception stack trace ---
NativeMethods.core_Mat_sizeof()
Mat.cctor() line 714
--- End of inner exception stack trace ---
Mat.ctor()
What did you intend to be?
No exception.
The exact same setup works with OpenCvSharp4.Windows 4.1.1.20191216.
Also, OpenCvSharpExtern.dll files are present in both [project]\bin\Debug\dll\x86 and [project]\bin\Debug\dll\x64. I opened them in Dependencies and didn’t see any errors; don’t know what to look for beyond that.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 2
- Comments: 46 (10 by maintainers)
In Microsoft Visual Studio 2019, be sure that you get the Runtime DLLs with using Nuget Packet Manager : Install-Package OpenCvSharp4.runtime.win -Version 4.3.0.20200524
Have a nice coding…
I have released the new OpenCvSharp(4.3.0). Does the problem still occur in 4.3.0?
@ArXen42 I used the OpenCvSharp4.Windows with Version 4.0.1.20190326 and that worked for me. (Before we had and older version of the deprecated Nuget package OpenCvSharp3-AnyCpu which lead to an services crash. In the first step i updated this package which lead to the “Missing OpenCvSharpExtern” exception.) Hope this helps.
No problem. I’ve enabled native code debugging in the project properties for the console app, caught the exception and looked at the disassembly at the point where the exception was thrown (which, in hindsight, I should have done immediately). It turns out exception 0xC000001D means Illegal Instruction, and the instruction in question is
vpxor, which, according to Intel 64 and IA-32 Architectures Software Developer’s Manual, is an AVX/AVX2 instruction (and my CPU does not support AVX).I suppose the question now is why does DLL initialization code need to run AVX instructions, and why doesn’t it check for CPU support before trying to run them?
Also,
0xc65836is a 12MB offset, so the problem might not actually be inxphoto_oilPainting2. If I had a debug build ofOpenCvSharpExtern.dlland its PDB file, I might be able to see which function contains the offending code.I’ve checked in Task Manager, and the console app is 32-bit. According to this, the reason is that “Prefer 32-bit” is checked in the project properties.
Copying the 64-bit dll to the exe folder causes the program to crash with a
System.BadImageFormatException(as might be expected). Copying the 32-bit dll to the exe folder results in a new exception:System.DllNotFoundException: Unable to load DLL 'OpenCvSharpExtern': A dynamic link library (DLL) initialization routine failed. (Exception from HRESULT: 0x8007045A).After I unchecked “Prefer 32-bit” and rebuilt the solution, the console app showed as 64-bit in Task Manager. Without copying any dlls, running it caused the original exception. After I copied the 64-bit dll to the exe folder, I got the “initialization routine failed” exception again.
I’m guessing there’s something different about
OpenCvSharpExtern.dllbetween 4.1.1.20191216 and 4.2.0.20191223 that’s causing this, but I can’t figure out what.Hi, I have same error like that. I’ve try to some version from 4.4 down to 4.2, but still error, when I’ve changed to version 4.1, It run perfect. I don’t know why
Thanks 🙆♂️ https://github.com/shimat/opencvsharp/pull/947
I checked with ProcessExplorer and yes, Extern.dll is loaded by itself , but after that exception is thrown and swallowed. Looks like TryPInvoke() loads it as inteded.
So maybe you should skip https://github.com/shimat/opencvsharp/blob/master/src/OpenCvSharp/PInvoke/NativeMethods/NativeMethods.cs#L92 for .netcore (maybe only when additionalPaths variable is empty, if that’ll be safer).
Thank you, works for me even on older CPUs.
thank you and Stay at home 🍡
Is there any official response from the authors maintaining OpenCvSharp? I am trying to make this work for OpenCvSharp4, UWP, x86 build.
@Pavelyev as far as I understand, current version of OpenCVSharp won’t work on processors without AVX2 instruction set. If you can’t change your server to something with more modern CPU, the only solution (aside from actually managing to fix it in OpenCV(Sharp) sources) is to downgrade to something like 4.1 or 4.0 version.
If you will do this, please tell what version don’t have this issue. I’m planning to downgrade OpneCV in my project too, but don’t have suitable PC to reproduce the issue, so can’t know for sure which version started this.
Upd. I can confirm that version 4.0 works on user PC where 4.2 don’t. Too bad it doesn’t have that neat Canon RAW support 😦
I’ve built
OpenCvSharpExtern.dllusing these. However, with this DLL, I can not get VS to break on the actual offending instruction, despite having native code debugging enabled. It just shows the DLL not found exception, like it did previously when I had native code debugging disabled (and I’ve verified in the output window that it’s repeatedly loading and immediately unloading the correct DLL). I have no idea why this is happening. Analyzing in DebugDiag didn’t help either.I tried doing this, but I was unable to build a debug version of
OpenCvSharpExtern.dllequivalent to your release version. (I was able to build, with much trickery, a ~20MB debug version ofOpenCvSharpExtern.dllwhich didn’t exhibit the original problem, presumably because it didn’t include the AVX-containing function.)It seems this problem is another instance of opencv/opencv#15690, as @janecek1980 mentioned.
I have this problem as well.
OpenCV should check if AVX is available when loading the libraries, however there seems to be a bug in an initializer which runs an AVX instruction which than causes the crash: _https://github.com/opencv/opencv/issues/15690_
This is related to OpenCV 4.1.1 and 4.1.2. If I understand it correctly, it should have been resolved for 4.2 - but it does not seem that way.
x86??? Your environment is x64, but the x86 DLL is loaded…
OpenCvSharp controls the target DLL file path for C# DllImport by loading the
dll/[x64 or x86]/OpenCvSharpExtern.dllin advance with the Win32APILoadLibraryfunction. If the platform setting is AnyCPU, the program must determine whether the native DLL to be loaded at runtime should be x64 or x86.As a workaround, manually copying
dll/x64/OpenCvSharpExtern.dllto the same folder as the exe file may make the program work.copy dll/x64/OpenCvSharpExtern.dll ..\..\