System.DllNotFoundException in .NET Core application
Asked Answered
S

2

2

In a .NET Core application, I'm getting this error when I try to run my unit test project:

System.DllNotFoundException: Unable to load DLL 'opencv_core246' or one of its dependencies: The specified module could not be found. (0x8007007E)

The DLLImport line simply uses 'opencv_core246' (no path), so my understanding is that the runtime should look in the same directory as the executable itself (or failing that Windows/System32 or Windows/SystemWOW64). It is now in all three locations just for the hell of it, but still no dice. My colleague has the same set-up (mine is a Windows VM running in Parallels on a Mac whereas as his is native Windows but that shouldn't matter) and his tests run OK. Any ideas about how to debug this one appreciated.

Sloane answered 19/3, 2020 at 5:19 Comment(0)
C
2

First of all, please don't spray DLLs all over your system. That just makes life complicated. Remove the DLLs in the system directories and never modify those directories again. They belong to the system, not you.

Now, you will get the DllNotFoundException if the DLL cannot be found, or one of its dependencies cannot be found. The DLL will be found since its alongside the executable. Ergo, one of the dependencies cannot be found. Find out what the dependencies are, and make sure they can be resolved.

Civism answered 19/3, 2020 at 8:54 Comment(0)
S
2

I'm adding my own answer in case this is helpful to someone else. As the accepted answer indicates, I was indeed missing dependencies of the native dll (opencv_core246). The thing is how to find out what these dependencies are. I used a utility called dependency walker, but this gave rather confusing results - it seemed to indicate I was missing the windows kernel! What clarified the issue was using the dumpbin utility which comes with Microsoft's C++ compiler. You can use this like so:

dumpbin /DEPENDENTS <my.dll>

This indicated that my dll had three dependencies, the kernel plus two that are part of the C++ redist package which was not installed on my system. Installing that fixed the missing dependency issue.

EDIT: Actually dependency walker results dig deeper into the dependency tree than dumpbin, so you can use it to get the same results. The dll dependencies are at the top level of the results tree.

Sloane answered 20/3, 2020 at 0:53 Comment(2)
Hi @David Heffernana, I followed see sharper's instructions to resolve my issue but i am getting new error. I have found that my vendor's .dll file depends on 2 files using Dependencies or dumnbin tools. Tool shows as follows A. \Windows\SysWOW64\Kernel32.dll B. \Windows\SysWOW64\msvcr110.dll. Both files are available under "\Windows\SysWOW64". Now, I did the following tests.Mina
Test 1: I copied both files(Kernal, msvcr110) from "\Windows\SysWOW64" into my Lib folder(contains Vendors' .dll file) which is under project. but I am getting exceptions. Test 2: I copied my vendor's .dll file into "\Windows\SysWOW64" or "\Windows\System32" but I am getting error. Both cases i got the following error. "System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (0x8007000B)". It is really so urgent. Would you please give me any suggestions/hints/idea/solution?Mina

© 2022 - 2024 — McMap. All rights reserved.