开发者

Swig c++/c# keep getting "Type Initialized Exception"

开发者 https://www.devze.com 2023-03-14 18:44 出处:网络
I am currently creating my first swig project. I have some c++ code, where i am using 1 of the classes functions in my c# UI. I have created a .i file that looks something like this:

I am currently creating my first swig project. I have some c++ code, where i am using 1 of the classes functions in my c# UI. I have created a .i file that looks something like this:

%module mymodule
%{
    #include "Simulation.h"
%}

/*****************************************************************************
 *                            SOME DEFINITIONS                               *
 *****************************************************************************/

开发者_Go百科#define EXAMPLE_DEFINITION 'a'
#define EXAMPLE_DEFINITION_2 'x'


class Simulation
{
public:
    // BUNCH OF FUNCTIONS
    static unsigned int getTick();
    static void tick();
// BUNCH MORE FUNCTIONS
private:
    // A BUNCH MORE THINGS
};

I added the cs files using:

swig -c++ -csharp -outdir UI -o cpp/simulation_wrap.cpp Simulation.i

It produces several files, which i add to my c# project. in my code if i add the lines:

Simulation.getTick();

or

Simulation.tick();

I get no compile errors, but the following run time error:

The type initializer for 'mymodulePINVOKE' threw an exception.

have I missed out any steps? I have tried searching for this but haven't managed to get any success.

(Perhaps it is important to note that i have tried compiling the c++ as a dll and a lib).

Thanks in advance


What worked for me was to:

  1. add ####.cxx to c++ project and rebuild to get a new dll(maybe you need to correct some error)
  2. Copy that new dll file to [C#]\bin\x86\Debug (Release).
  3. Go to Build->Configuration Manager. For the C# project change AnyCpu to x86 and rebuild.

    Reference

0

精彩评论

暂无评论...
验证码 换一张
取 消