Unmanaged DLL is not loading with DLLImport C#

3

I must be missing something here; going crazy.. I'm using VS2010 C#, developing a Windows Form application in C#. In the same solution I have a c++ dll project that outputs it's DLL to the same "/bin/" folder as the C# executable. I'm looking at the folder and the file is definitely there. I even used the following to make sure it existed at run-time:

// yep.. it's there...
string s = System.IO.Directory.GetCurrentDirectory() + @"\Foo.dll";
System.Diagnostics.Debug.Assert(System.IO.File.Exists(s));

my DLL has this:

extern "C" __declspec(dllexport) int test_func();

which I confirmed with a DLL explorer utility to have the correct name inside. The function itself does nothing but "return 4;"

C# code is:

[DllImport("Foo.dll")]
private static extern int test_func();

I get this error:

An unhandled exception of type 'System.DllNotFoundException' occurred in .exe

Additional information: Unable to load DLL 'Foo.dll': Invalid access to memory location. (Exception from HRESULT: 0x800703E6)

c#
dll
asked on Stack Overflow Nov 25, 2013 by Ben

2 Answers

2

The error is occurring when the framework calls LoadLibrary. That can be deduced from the error message:

Unable to load DLL 'Foo.dll'

The error code that is reported is a COM error code that wraps Win32 error code 998, aka ERROR_NOACCESS. The scenario that leads to a call to LoadLibrary throwing ERROR_NOACCESS is described here: http://support.microsoft.com/kb/196069

SYMPTOMS

The LoadLibrary() API may sometimes encounter an access violation while attempting to map a specified module into the address space of the calling process. In this event, LoadLibrary() returns a value of NULL and GetLastError() returns an error code of 998 (ERROR_NOACCESS). Back to the top | Give Feedback

CAUSE

The Windows NT status code STATUS_ACCESS_VIOLATION is mapped to the Win32 error code ERROR_NOACCESS. As a result, if the operating system loader encounters an access violation (exception C0000005) while mapping the specified DLL file image or executing the startup code, the loader will set the last error to 998 (ERROR_NOACCESS) and the LoadLibrary() function will fail with a return value of NULL. Back to the top | Give Feedback

MORE INFORMATION

When an access violation occurs anywhere in the startup code, the exception dispatcher detects whether the process that encountered this exception is being debugged. If so, this first chance exception is sent to the debugger.

To troubleshoot the LoadLibrary() failure, run the application under a debugger and enable first chance exception handling for the C0000005 Access Violation exception. If an access violation occurs when the LoadLibrary() function is called, the application will break into the debugger. The debugger's call stack can then be used to trace where the exception occurred. The stack trace should help you narrow down the actual problem related to the exception being encountered.

For information on how to enable first chance exception handling for the C0000005 Access violation exception, please refer to the debugger documentation.

In essence, your DLL is producing an access violation in its startup code. Many things could cause that and you'll need to do some debugging of the DLL. I suggest that you attempt to debug this from a native C++ host. It will likely be easier to debug the DLL that way.

As an aside, your function uses the cdecl calling convention, and once you get past the current hurdle, you'll need to modify your C# p/invoke to specify CallingConvention.Cdecl.

answered on Stack Overflow Nov 25, 2013 by David Heffernan
0

Assume you already saw this link.

Have you opened "foo.dll" with depends.exe to ensure you have all the dependencies satisfied?

answered on Stack Overflow Nov 25, 2013 by tobyb • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0