none
<CrtImplementationDetails>.ModuleLoadException: The C++ module failed to load during native initialization. RRS feed

  • Frage

  • Hallo

    habe das unten beschriebene Problem im US Forum gepostet, allerdings ohne
    Resonanz. Wenn jemand einen Tip dazu hatte, waere ich sehr dankbar
    Viele Gruesse

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

    Donnerstag, 14. Oktober 2010 06:59

Antworten

Alle Antworten

  • Meine Vermutung ist, dass in der DLL ein Fehler drin ist...
    Kannst Du es nicht debuggen? Öffne das c++/CLI Projekt und trage das C#-Projekt als Start-Programm ein. Dann solltest Du es debuggen können, wenn das C#-Programm die gleiche Assembly (DLL) verwendet!

    Warum verwendest Du InteropServices um die C++/CLI Assembly einzubinden? Die kannst Du doch direkt referenzieren!


    Jochen Kalmbach (MVP VC++)
    Donnerstag, 14. Oktober 2010 07:16
  • Danke für die schnelle Antwort! ok, InteropService brauche ich nicht, das Verhalten
    ändert sich allerdings nicht, wenn ich die weglasse.

    Das funktionale Debugging der nativen library (ist keine dll sondern .lib) habe ich
    mit einem native c++ Client gemacht, wie ich geschrieben habe, funktioniert die library
    im rein nativen Setting einwandfrei.

    Allerdings habe ich beim Kompilieren der library die Compiler-option
    _SECURE_SCL=0 gesetzt. Könnte das ein Problem sein, wenn ich die
    library in ein gemanagtes Projekt einbinde?

    Donnerstag, 14. Oktober 2010 07:35
  • Das funktionale Debugging der nativen library (ist keine dll sondern .lib) habe ich

    Du kannst keine LIB in C# verwenden!
    Deswegen debugge bitte die C++/CLI-Assembly, welche die LIB verwendet!!!

    Allerdings habe ich beim Kompilieren der library die Compiler-option
    _SECURE_SCL=0 gesetzt. Könnte das ein Problem sein, wenn ich die
    library in ein gemanagtes Projekt einbinde?

    Die LIB muss die gleichen Projekteinstellungen verwenden wie die C++/CLI Assembly.


    Jochen Kalmbach (MVP VC++)
    Donnerstag, 14. Oktober 2010 07:39
  • Danke,

    gibt's irgendwo ein Tutorial, in dem man die emfohlene Vorgehensweise nachlesen kann
    (Einbinden einer nativen c++-library in C#)?

     

    Donnerstag, 14. Oktober 2010 07:53
  • gibt's irgendwo ein Tutorial, in dem man die emfohlene Vorgehensweise nachlesen kann
    (Einbinden einer nativen c++-library in C#)?

    Diese wird es nicht geben, da man eine LIB in C# nicht verwenden kann.
    Du musst dazu einen C++/CLI Wrapper bauen. Und den kann man dann debuggen...

    Ansonsten:
    http://lmgtfy.com/?q=c%2B%2B%2Fcli+wrapper+example


    Jochen Kalmbach (MVP VC++)
    Donnerstag, 14. Oktober 2010 07:59
  • ok, wahrscheinlich war ich zu unpräzise:

    1. ich habe eine native c++ lib xToolLib.lib, die Klasse Tool, die ich verwenden will, ist im header
    xtool.h definiert.

    2. habe ich eine wrapper klasse: public ref class ToolWrapper, die einen pointer auf ein Tool
    objekt enthält. Das ist mE ein c++/cli wrapper.
    ich kompiliere den wrapper mit: cl.exe /c /clr  /MD /D  ToolWrapper.cpp
    Ergebnis ist eine .obj Datei ToolWrapper.obj
    (in ToolWrapper.cpp wird xtool.h includiert)

    3. eine .cs datei, in der ein wrapper-objekt gebaut wird, kompiliere ich mit:
    csc /target:module /addmodule:ToolWrapper.obj xTool.cs

    4. ich linke das alles mit
    link /LTCG /CLRIMAGETYPE:IJW /ENTRY:xToolCS.Program.Main /SUBSYSTEM:CONSOLE
    /OUT:xTool.exe xToolWrapper.obj xTool.netmodule xToolLib.lib

    An welcher Stelle ist diese Vorgehensweise falsch?

    Donnerstag, 14. Oktober 2010 08:18
  • > ich kompiliere den wrapper mit: cl.exe /c /clr  /MD /D  ToolWrapper.cpp

    Das geht so nicht, da der CRT-Einstiegspunkt nicht aufgerufen wird!

    Erstelle eine Wrapper-Assembly!!! Aso ein ganz normale C++/CLI DLL!


    Jochen Kalmbach (MVP VC++)
    Donnerstag, 14. Oktober 2010 10:59
  • habe das problem jetzt eingegrenzt. meine native c++ library includiert
    die Boost library. Wenn ich diese includes weglasse, funktioniert es.
    Sobald ich z.B. <boost/filesystem.hpp> include, crasht die applikation wie
    oben beschrieben. Das executable findet scheinbar die referenz auf die
    boost-lib nicht.

     

    Donnerstag, 14. Oktober 2010 11:43
  • 1. Was heißt crashed? Was sagt der Debugger?
    2. Was will die DLL an sonstigen DLLs noch haben? (führe DEPENDS aus).


    Martin Richter -- MVP for VC++ [Germany] -- http://blog.m-ri.de
    Donnerstag, 14. Oktober 2010 12:03
    Moderator
  • Donnerstag, 14. Oktober 2010 14:27