System.DllNotFoundException in .NET Core application


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.

asked on Stack Overflow Mar 19, 2020 by see sharper

2 Answers


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.

answered on Stack Overflow Mar 19, 2020 by David Heffernan

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.

answered on Stack Overflow Mar 20, 2020 by see sharper • edited Mar 20, 2020 by see sharper

User contributions licensed under CC BY-SA 3.0