C# – Unable to load DLL (Module could not be found HRESULT: 0x8007007E)

cdllpinvoke

I have a 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 several solutions I have found on the internet. Nothing works..

I have tried using following methods:

[DllImport("MyOwn.dll",  CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs((UnmanagedType.I4))]
public static extern Int32 MyProIni(string DBname, string DBuser_pass,
    string WorkDirectory, ref StringBuilder ErrorMessage);

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 the files the it depends on into my bin folder).

I also tried this approach but got the same error:

[DllImportAttribute(MyOwnLibDllPath, EntryPoint="TMproIni")]
[return: MarshalAs(UnmanagedType.I4)]
public static extern  int MyproIni(string DBname, string DBuser_pass, 
    string WorkDirectory, ref StringBuilder ErrorMessage);

Any suggestions?

Best Answer

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

  1. Current Directory
  2. System folder, C:\windows\system32 or c:\windows\SysWOW64 (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

Related Topic