I have dll library with unmanaged C++ API code I need to use in my .NET 4.0 application. But every method i try to load my dll i get an error:

Unable to load DLL ‘MyOwn.dll’: The specified module could not be found. (Exception from HRESULT: 0x8007007E)

I have read and tried severa solutions i have found on the internet. Nothing works..

I have tried using following methods:

When I tried following this article and when I run this example (from the downloaded code) it runs without a problem (the dll used is in the bin/debug folder)

I have copied my dll (along with all files the it depends on into my bin folder).

I also tried this approach but got the same error:

16 Answers 16

From what I remember on Windows the search order for a dll is:

  1. Current Directory
  2. System folder, C:windowssystem32 or c:windowsSysWOW64 (for 32-bit process on 64-bit box).
  3. Reading from the Path environment variable

In addition I’d check the dependencies of the DLL, the dependency walker provided with Visual Studio can help you out here, it can also be downloaded for free: http://www.dependencywalker.com

You can use the dumpbin tool to find out the required DLL dependencies:

This will tell you which DLLs your DLL needs to load. Particularly look out for MSVCR*.dll. I have seen your error code occur when the correct Visual C++ Redistributable is not installed.

You can get the "Visual C++ Redistributable Packages for Visual Studio 2013" from the Microsoft website. It installs c:windowssystem32MSVCR120.dll

In the file name, 120 = 12.0 = Visual Studio 2013.

Be careful that you have the right Visual Studio version (10.0 = VS 10, 11 = VS 2012, 12.0 = VS 2013. ) right architecture (x64 or x86) for your DLL’s target platform, and also you need to be careful around debug builds. The debug build of a DLL depends on MSVCR120d.dll which is a debug version of the library, which is installed with Visual Studio but not by the Redistributable Package.

Try to enter the full-path of the dll. If it doesn’t work, try to copy the dll into the system32 folder.

The DLL has to be in the bin folder.

In Visual Studio, I add the dll to my project (NOT in References, but "Add existing file"). Then set the "Copy to Output Directory" Property for the dll to "Copy if newer".

This is a ‘kludge’ but you could at least use it to sanity-test: Try hard-coding the path to the DLL in your code

Having said that; in my case running dumpbin /DEPENDENTS as suggested by @anthony-hayward, and copying over 32-bit versions of the DLLs listed there into my working directory solved this problem for me.

The message is just a bit misleading, becuase it isn’t "my" dll that can’t be loaded — it’s the dependencies

Ensure that all dependencies of your own dll are present near the dll, or in System32 .

There is one very funny thing (and has a technical relevance) which might waste your hours so thought of sharing it here —

I created a console application project ConsoleApplication1 and a class library project ClassLibrary1 .

All the code which was making the p/invoke was present in ClassLibrary1.dll . So before debugging the application from visual studio I simply copied the C++ unmanaged assembly ( myUnmanagedFunctions.dll ) into the indebug directory of ClassLibrary1 project so that it can be loaded at run-time by the CLR.

I kept getting the

error for hours. Later on I realized that all such unmanaged assemblies which are to be loaded need to be copied into the indebug directory of the start-up project ConsoleApplication1 which is usually a win form, console or web application.

So please be cautious the Current Directory in the accepted answer actually means Current Directory of main executable from where you application process is starting. Looks like an obvious thing but might not be so at times.

Lesson Learnt — Always place the unamanaged dlls in the same directory as the start-up executable to ensure that it can be found.

When loading a managed C++ assembly, you may receive the following exception:

System.IO.FileNotFoundException: The specified module could not be found. (Exception from HRESULT: 0x8007007E)

В В at System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Bool

ean throwOnFileNotFound, Boolean forIntrospection)

В В at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)

В В at System.Reflection.Assembly.InternalLoadFrom(String assemblyFile, EvidencesecurityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean

В forIntrospection, StackCrawlMark& stackMark)

В В at System.Reflection.Assembly.LoadFrom(String assemblyFile)

0x8007007E is a Win32 error ERROR_MOD_NOT_FOUND.

// MessageText:
// The specified module could not be found.

The error is returned by LoadLibrary.В Usually it is because one of the assembly’s native dependencies(static imports)В cannot be found by the OS.

If the assembly is built as debug by Visual Studio 2005 toolset, make sure the debug version of VC80 CRT runtime is installed on the target machine before load the assembly.


Copy link Quote reply

nefcanto commented Dec 23, 2017

VS 2017 Community edition 15.5.2
dotnet —version => 2.1.2
Windows 10 Enterprise