Quantcast
Channel: Common Language Runtime Internals and Architecture forum
Viewing all articles
Browse latest Browse all 1710

ModuleLoadException: The C++ module failed to load during native initialization

$
0
0

Hi all

i receive the above error message when i try to run a c# program that calls class member
functions in a c++ static lib (lib0) via a c++/cli bridge. The complete error message is
appended at the bottom of my message. This is on Windows XP

The c++ lib0 is built with VS 2005. It links in a couple of other static native c++ libs lib1
and lib2. I have a native c++ console application, which builds an object of a type defined
in lib0 and calls member functions, this works fine.

To bridge to c#, i have defined a wrapper class (public ref class Xwrapper{};) in
wrapper.cpp, which holds a pointer to the native class X and exposes some native
member functions (void and int arguments). I am using Runtime::InteropServices.
On the c# side i have a simple main in tool.cs, which creates an Xwrapper object
and calls the exposed member functions. The build is done from the command line
using the following sequence of commands:

cl.exe /c /clr  /MD  wrapper.cpp
csc /target:module /addmodule:wrapper.obj tool.cs
link /LTCG /CLRIMAGETYPE:IJW /ENTRY:Tool.Program.Main /SUBSYSTEM:CONSOLE
/OUT:tool.exe wrapper.obj tool.netmodule lib0.lib

Running tool.exe produces the above error message.
I have used this general approach in other projects and it worked fine. It also works
fine if i remove all reference to classes defined in lib1 and lib2 from lib0. The problem
hence seems to be related to those libs (which pose no problem at all in the purely
native setting!).

Is there any way to pin down the problem or am i doing something completely wrong?
Maybe i am missing some linker options?

Thanks for all responses!

full error message:
----------------------
Unhandled Exception: System.TypeInitializationException: The type initializer for '<Module>' threw an exception. --->
<CrtImplementationDetails>.ModuleLoadException: The C++ module failed to load during native initialization.
 ---> System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that oth
er memory is corrupt.
   at _initterm((fnptr)* pfbegin, (fnptr)* pfend)
   at <CrtImplementationDetails>.LanguageSupport.InitializeNative(LanguageSupport* )
   at <CrtImplementationDetails>.LanguageSupport._Initialize(LanguageSupport* )
   at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
   --- End of inner exception stack trace ---
   at <CrtImplementationDetails>.ThrowModuleLoadException(String errorMessage, Exception innerException)
   at <CrtImplementationDetails>.ThrowModuleLoadException(String , Exception )
   at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
   at .cctor()
   --- End of inner exception stack trace ---

 


Viewing all articles
Browse latest Browse all 1710

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>