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

  • Question

  • 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 ---

     

    Wednesday, October 13, 2010 3:44 PM

Answers

  • Problem was related to Boost libraries used in native code.

    This was helpful:

    http://marc.info/?l=boost-users&m=123425857320026

    • Marked as answer by lottha Thursday, October 14, 2010 3:52 PM
    Thursday, October 14, 2010 3:52 PM

All replies

  • Problem was related to Boost libraries used in native code.

    This was helpful:

    http://marc.info/?l=boost-users&m=123425857320026

    • Marked as answer by lottha Thursday, October 14, 2010 3:52 PM
    Thursday, October 14, 2010 3:52 PM
  • 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 ---


    I had a similar problem and fixed it by changing my DLL; instead of using an MFC DLL, I created a Win32 DLL.
    • Proposed as answer by danr98133 Thursday, August 22, 2013 11:22 PM
    Thursday, August 22, 2013 11:22 PM