none
how can I use unmanaged c++/cli classes in c#? RRS feed

  • Question

  • I compiled box2d in a CLR Library project( and it did work ), then i reference this project in C#( as shown in the first block of code). So by now I can declare these objects. But when I want to set the values of members of these classes, I cannot get access to them!  I think it is possibly because of using unmanaged code in managed code, but how can I fix this?

    ///inside a function of game class( C# )
    unsafe
    {
         b2Body* body;
         b2BodyDef def;
    //these four methods are the only ones shown by IntelliSense while def has many more methods
         def.Equals
             GetHashCode
             GetType
             ToString
    }
    
    
    ///box2d header file
    public class b2Body
    {
    public:
    	
    	b2Fixture* CreateFixture(const b2FixtureDef* def);
    
    	
    	b2Fixture* CreateFixture(const b2Shape* shape, float32 density);
         
            //other public functions and memebers...
    
    
    public struct b2BodyDef
    {
    		
    
    		b2BodyType type;
    
    		b2Vec2 position;
    
    		float32 angle;
    
    		b2Vec2 linearVelocity;
    
    		float32 angularVelocity;
    
    		float32 linearDamping;
    
    		//other members...
    };

    Saturday, October 17, 2015 12:58 AM

Answers

  • The whole point of using C++/CLI is that you can create C++ managed types (as in "ref class" rather than just "class") that wrap native types. Those managed types may offer access to the information stored in native types via properties and/or by copying the necessary information back and forth between native and managed types.

    For example, you could have a managed version of b2BodyDef that can be initialized in C# and passed to a managed member function of a managed C++/CLI type. That managed function will convert the managed b2BodyDef to the native version and call the native CreateFixture function or whatever native function needs to be called.

    Have you considered using a managed 2D physics engine instead? Might work better without this "interop" work. Apparently Farseer is a managed port of Box2D.

    • Marked as answer by leaf-leaf Saturday, October 17, 2015 6:11 AM
    Saturday, October 17, 2015 5:51 AM
    Moderator

All replies

  • The whole point of using C++/CLI is that you can create C++ managed types (as in "ref class" rather than just "class") that wrap native types. Those managed types may offer access to the information stored in native types via properties and/or by copying the necessary information back and forth between native and managed types.

    For example, you could have a managed version of b2BodyDef that can be initialized in C# and passed to a managed member function of a managed C++/CLI type. That managed function will convert the managed b2BodyDef to the native version and call the native CreateFixture function or whatever native function needs to be called.

    Have you considered using a managed 2D physics engine instead? Might work better without this "interop" work. Apparently Farseer is a managed port of Box2D.

    • Marked as answer by leaf-leaf Saturday, October 17, 2015 6:11 AM
    Saturday, October 17, 2015 5:51 AM
    Moderator
  • thanks, maybe I should use farseer. It seems to be very nice.
    Saturday, October 17, 2015 6:11 AM