none
Native C++ included in C# - problem with native singleton RRS feed

  • Question

  • Hi all

    i am trying to integrate a static native c++ lib into a c# project. I use a c++/cli intermediate layer
    which wraps the native classes and exposes them to the managed layer. That has worked fine so
    far, i even managed to overcome the problem of using Boost library in the native classes.
    I use VS 2005.

    I now have a problem when i try to instantiate a singleton within the native code, which is implemented
    according to the following principle

    class
     Singleton
    {
    public : static Singleton* exemplar();
    protected
    : Singleton() {}
    private
    : static Singleton * instanz;
    };

    This singleton class is not wrapped in the c++/cli layer but it is constructed within an exposed native class.

    The code compiles and links fine, but when i start the c#-exe i receive the error message:
    ModuleLoadException: The C++ module failed to load during native initialization
    ie the program doesn't even reach any executable sections.

    Is that a known problem?
    Can anybody point me towards a solution?

    Thanks

    Hans

    Thursday, October 14, 2010 5:14 PM

Answers

  • Hi Hans,

    I tried on a very simple solution to call a native singleton class from a cli wrapper and it worked. Maybe you can paste some code so we will have more information on your situation? In the meantime I noticed that you do not generate a .netmodule for wrapper class (or maybe you didn't mention that). So the command line in the first point would be changed to cl.exe /c /clr /LN /MD  wrapper.cpp. And then in point 2 you will have: csc /target:module /addmodule:wrapper.netmodule tool.cs Netmodule will link to the wrapper.obj.

    Best,

    Sebastian

    • Marked as answer by lottha Monday, October 25, 2010 6:19 AM
    Monday, October 18, 2010 2:53 PM

All replies

  • Hi Hans,

    Could you specify how do you import the native library into your C# code? Which attributes do you use?

     

    Best,

    Sebastian

     

    Sunday, October 17, 2010 10:01 AM
  • Hi,

    Thanks for your post. I found this solved thread which addresses a similar symptom. And here is another similar thread. Several possible solutions are provided in the above 2 threads, but none of them seems convincing enough. Could you post some repro code so we can better investigate it.

    Since this problem has been seen several times without a definite solution, please consider contacting the https://connect.microsoft.com with detailed repro steps.


    Please mark the right answer at the right time.
    Thanks,
    Sam
    Monday, October 18, 2010 3:41 AM
  • Hi

    thanks for your replies.

    Sam,

    i had seen the two threads you are referencing too. They were not helpful for my situation,
    however, since my problem seems to be specifically related to the usage of the Singleton
    in the native library. Before running into that I already had other issues which were leading
    to a similar ModuleLoadException error, but I was able to overcome them by linking to
    the Boost library differently, as DLL instead of static lib.

    Seb,

    for linking C# to a native lib i am following (with modifications) the approach described here:
    http://blogs.msdn.com/b/texblog/archive/2007/04/05/linking-native-c-into-c-applications.aspx:

    1. compile c++/cli wrapper (which includes header to the native classes/functions)
    cl.exe /c /clr  /MD  wrapper.cpp
    2. compile c# (managed code in tool.cs)
    csc /target:module /addmodule:wrapper.obj tool.cs
    3. link everything including the native library lib0.lib
    link /LTCG /CLRIMAGETYPE:IJW /ENTRY:Tool.Program.Main /SUBSYSTEM:CONSOLE
    /OUT:tool.exe wrapper.obj tool.netmodule lib0.lib

    I can only emphasize that this has worked fine in the past, where i have used this
    in conjunction with rather simple native C++ stuff. My recent experience with Boost
    and complex C++ structures, however, has been terrifying.

    I have almost given up on this for the time being. My next attempt is to compile two
    executables and exchange data via sockets.

    Regards from Germany
    Hans

    Monday, October 18, 2010 7:29 AM
  • Hi Hans,

    I tried on a very simple solution to call a native singleton class from a cli wrapper and it worked. Maybe you can paste some code so we will have more information on your situation? In the meantime I noticed that you do not generate a .netmodule for wrapper class (or maybe you didn't mention that). So the command line in the first point would be changed to cl.exe /c /clr /LN /MD  wrapper.cpp. And then in point 2 you will have: csc /target:module /addmodule:wrapper.netmodule tool.cs Netmodule will link to the wrapper.obj.

    Best,

    Sebastian

    • Marked as answer by lottha Monday, October 25, 2010 6:19 AM
    Monday, October 18, 2010 2:53 PM
  • Hi Sebastian

    thanks, i will simplify my code and try again at another time. Preparing code
    to post it here is too much work since the project is too complex.

    Regarding the netmodule, my understanding is that I don't need that since
    I link the wrapper.obj in step 3. It works anyway.

    Regards
    Hans

    Monday, October 25, 2010 6:19 AM