2

I want to use C++ class in C#.

In C++ my .h file is like this:

struct XMFloat3
{
public:
    float x;  float y; float z;

    XMFloat3(){}

    XMFloat3(float _x, float _y,float _z)
    {  
        x = _x;
        y = _y; z = _z; 
    }  
};

class Car
{
public:
    XMFloat3 m_position;
    char* m_name;
    int m_model;
    double m_price;

    void setPosition(XMFloat3);
    void setName(char* );
    void setModel(int);
    void setPrice(double);

    XMFloat3 getPosition() { return m_position; }
    char* getName() { return m_name; }
    int getModel() { return m_model; }
    double getPrice() { return m_price; }

    void SetInformation(XMFloat3 position,char* name, int model,double price);
    ~Car(void);
    static Car* getInstance();

private:
    Car(void){ }
    Car(XMFloat3 position,char* name,int model,double price){ }
    static Car* instance;
};

I create clr\library project with Wrapper Class definition like:

public ref class WrapperClass
{ 
public:
    WrapperClass();

    void setInformation(XMFloat3 position,String^ name,int model,double price);

    void setPosition(XMFloat3);
    void setPosition(float x,float y,float z);
    XMFloat3 getPosition() { return obj->getPosition(); }
    void setName(String^ name);
    String^ getName() { return gcnew String(obj->getName()); }

    void setModel(int);
    int getModel() { return obj->getModel(); }

    void setPrice(double);
    double getPrice() { return obj->getPrice(); }

    float getX() { return obj->getX(); }

    ~WrapperClass();
private:
    Car *obj;  
};

.....................................................................................

But when i use this Wrapper class function getPosition() in C#, in C# getPosition() Declaration is like:

XMFloat3* getPosition(XMFloat *);

That's not perfect for my use please tell me if i doing any wrong up there or suggest how to use User Defined Datatype in such scenario.

And 2nd question is How User Defined data types are access or use in C#. e.g if i want to pass XMFloat3 data from C# how is it possible?? (I create XMFloat3 struct in C# .cs file and try to use it but its give type matching error.

In C# setPosition declaration for Wrapper Class is like: public void setPosition(XMFloat3 t); Error is Type Mismatch CSharp.XMFloat3 to WrapperDll.XMFloat3.

Please tell me how to use above Class in C#.

Thanks

TorbenJ
  • 4,462
  • 11
  • 47
  • 84
  • One issue is that you cannot use a * (pointer) when you're pointing to a managed GC object. You need to use the GC variant of a pointer. [See this answer](http://stackoverflow.com/questions/202463/what-does-the-caret-mean-in-c-cli). – Measurity May 09 '16 at 04:07
  • If i change XMFloat3 getPosition() { return obj->getPosition(); } to XMFloat3* getPosition() { return obj->getPosition(); } now c# getPosition declaration is like: public XMFloat3* getPosition(); But still i cant get the inner(x,y,z) values in C# due to there protection level. – Zeeshan Maqsood May 09 '16 at 04:18
  • Error: Error 1 'CreateClassLibraryForDllUSE.WrapperClass.getPosition()' is inaccessible due to its protection level D:\CALL_CPP_TO_C#\CSHARP_DLL\CSHARP_DLL\Form1.cs 57 – Zeeshan Maqsood May 09 '16 at 04:20
  • XMFloat3 is a native struct and not usable from C#. You must write a public `value struct` wrapper for it. – Hans Passant May 09 '16 at 07:29
  • I create value type struct XMFloat3_ with x,y,z data member now how i can use this for Set and Get C++ XMFLoat3 x,y,z data members. ----------------------------------------------------------------------------------------- I did not create such Struct because when we include Cpp_Dll.h in our wrapper file(CreateClassLibraryForDllUSE), XMFloat3 is accessable in that file and i think its directly work for me.Please also discuss on this condition how its work in backend.Thanks – Zeeshan Maqsood May 10 '16 at 03:51

0 Answers0