.NET sometimes cannot load a DLL

3

I'm using Microsoft Visual Studio Express 2012 for Windows Desktop (Administrator)

I have a project library FOO.DLL with "Copy to output directory" clicked to "Copy always".

Most of the time this DLL loads correctly and everybody is happy.

Sometimes, I get a System.DllNotFoundException with message:

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

This error occurs both in Debug and Release. It occurs after a "clean" and after just running the same build a second or eighth time.

Is this an error in the DLL, the .NET runtime, or my .NET code?

c#
.net
asked on Stack Overflow Oct 4, 2013 by Cuadue

2 Answers

4

Invalid access to memory location. (Exception from HRESULT: 0x800703E6)

This error code is returned when code inside that DLL fails with an AccessViolation. Special code, run at a very critical time during DLL loading. The bad code is located in the DllMain() entrypoint of the DLL. That exception is swallowed, necessary to keep the Windows loader stable and all you see is the error code that it produces.

This is a very serious mishap and you'll need to work with the author of the DLL to get this problem resolved. You can debug the exception, use Debug + Exceptions, tick the Thrown checkbox for Win32 Exceptions. Make sure that Just My Code debugging is turned off, Tools + Options, Debugging, General. Enable unmanaged debugging, Project + Properties, Debug tab. The debugger stops when the exception is thrown. If you don't have the source code for the DLL then there's little to look at and nothing you can do to fix the problem, that requires help from the author. He will want the content of the Stack Trace window to have a shot at fixing the problem.

answered on Stack Overflow Oct 4, 2013 by Hans Passant • edited Oct 4, 2013 by Hans Passant
0

Visual Studio is not very good at keeping track of these dependencies from one project to the next. If your project (that includes the FOO.DLL) is later included in another project I suspect that Visual Studio will not always have this extra file carried properly to the top level project.

I`ve had to create a small MSBuild task to scrub the content of a project so these files are carried over properly (and deleted on clean). That said our project layout was a tad bit on the unorthodox complex side of things YMMV.

Long story short that would be the first place I look into and make sure the file follows, shortest (though not cleanest) mean to this is to add the FOO.DLL again to projects where the original project that needs it is included. Works well on small structure, for larger project structure I would look into MSBuild Task or perhaps a packaging deployement solution (a la Nuget).

Hope this helps

answered on Stack Overflow Oct 4, 2013 by Newtopian

User contributions licensed under CC BY-SA 3.0