Using C++ class in C#
-
Hi. In my project have 3 module. 1 and 2 nd module writen in c++ and 3th module - Application module written in C#. Every module different parts of projects. My first module have like that class :
class __declspec(dllexport) MyClass1
{
public :
int n;
MySimple1()
{
n=0;
}
~MySimple1()
{
}int Plus(int a,int b) { return n=(a+b); }
};
In my second module have like that class. And this class use 1 th class.
class __declspec(dllexport) MyClass2
{
public :MySimple2(MyClass1 a) { } ~MySimple2() { } ...
};
Can i use MyClass1 fields (i know how to use methods) in my C# app? In my C# app , how i can send MyClass1 like parameter to 2nd class ? How i can use like that in my C# app ?
public MyClass1 my1=new MyClass1();
int n=my1.Plus(10,12);MySimple2(my1); // calling through PInvoke.
Thanks.
We are haven't bug,just temporarily undecided problems.
-
Hi. In my project have 3 module. 1 and 2 nd module writen in c++ and 3th module - Application module written in C#. Every module different parts of projects. My first module have like that class :
class __declspec(dllexport) MyClass1
{
public :
int n;
MySimple1()
{
n=0;
}
~MySimple1()
{
}int Plus(int a,int b) { return n=(a+b); }
};
In my second module have like that class. And this class use 1 th class.
class __declspec(dllexport) MyClass2
{
public :MySimple2(MyClass1 a) { } ~MySimple2() { } ...
};
Can i use MyClass1 fields (i know how to use methods) in my C# app? In my C# app , how i can send MyClass1 like parameter to 2nd class ? How i can use like that in my C# app ?
public MyClass1 my1=new MyClass1();
int n=my1.Plus(10,12);MySimple2(my1); // calling through PInvoke.
Thanks.
We are haven't bug,just temporarily undecided problems.
You would need to use pinvoke and create interop to access the unmanaged classes. This might help http://www.pinvoke.net/[^]
I know the language. I've read a book. - _Madmatt
-
You would need to use pinvoke and create interop to access the unmanaged classes. This might help http://www.pinvoke.net/[^]
I know the language. I've read a book. - _Madmatt
Thanks for reply. I know how to use class methods with pinvoke. But i need use that class (NO METHOD) ? Any ideas? thanks.
We are haven't bug,just temporarily undecided problems.
-
Thanks for reply. I know how to use class methods with pinvoke. But i need use that class (NO METHOD) ? Any ideas? thanks.
We are haven't bug,just temporarily undecided problems.
-
You would need to use pinvoke and create interop to access the unmanaged classes. This might help http://www.pinvoke.net/[^]
I know the language. I've read a book. - _Madmatt
Runtime Call Wrapepers are for COM. This is just a normal C++ class exposed in the export table. In C++ calling methods on a class use a calling convention called ThisCall. With ThisCall the "this" pointer gets passed into the method via the ECX register and everything else is the same as STD call. .Net supports this. You can specify this calling convention in the DllImport attribute. How the constructor and destructor get called is something else. When you specify ThisCall in the DllImport attribute the first parameter of the method must be a pointer to "This". The names are going to be mangled in the export table. Use a program like dependency walker to see what they are. Here is a good post I found on this site. How to Marshal a C++ Class[^]
modified on Friday, October 1, 2010 2:38 PM
-
Hi. In my project have 3 module. 1 and 2 nd module writen in c++ and 3th module - Application module written in C#. Every module different parts of projects. My first module have like that class :
class __declspec(dllexport) MyClass1
{
public :
int n;
MySimple1()
{
n=0;
}
~MySimple1()
{
}int Plus(int a,int b) { return n=(a+b); }
};
In my second module have like that class. And this class use 1 th class.
class __declspec(dllexport) MyClass2
{
public :MySimple2(MyClass1 a) { } ~MySimple2() { } ...
};
Can i use MyClass1 fields (i know how to use methods) in my C# app? In my C# app , how i can send MyClass1 like parameter to 2nd class ? How i can use like that in my C# app ?
public MyClass1 my1=new MyClass1();
int n=my1.Plus(10,12);MySimple2(my1); // calling through PInvoke.
Thanks.
We are haven't bug,just temporarily undecided problems.
Create a wrapper class in a CLI project (C++.NET) and you should be able to access the C++ class from the .NET class. The advantage of this is that you keep your C++ project as is and you can debug both heaps easily. In this mode the unmanaged and managed heaps run within the same process without marshaling. I hope this helps.