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:
- add ####.cxx to c++ project and rebuild to get a new dll(maybe you need to correct some error)
- Copy that new dll file to [C#]\bin\x86\Debug (Release).
Go to Build->Configuration Manager. For the C# project change AnyCpu to x86 and rebuild.
Reference
精彩评论