|
-
February 20th, 2008, 02:19 PM
#1
Importing C++ Class in C#
Hi,
I was wondering if there was an easy way to import a C++ (Dll Exported)
Class in a C#/.Net program so I could call :
MyClass oMyClass = new MyClass(); //MyClass is coming from the C++ DllExport
I know how to import a static function with
[DllImport("myDll.dll", CharSet = CharSet.Auto]
public static extern myFunc();
But I would really like to know how to import a whole class and all of its public methods.
Thank you
Visual Studio 2005 Professional
-
February 20th, 2008, 04:28 PM
#2
Re: Importing C++ Class in C#
You can't import a C++ class into C#, you can only import C-styled dll functions.
The closest you can get is to create a COM dll with an interface (similar to the C++ class) and import the COM dll into C#.
-
February 20th, 2008, 06:27 PM
#3
Re: Importing C++ Class in C#
 Originally Posted by lmoreault
I was wondering if there was an easy way to import a C++ (Dll Exported)
Class in a C#/.Net program so I could call :
MyClass oMyClass = new MyClass(); //MyClass is coming from the C++ DllExport
I know how to import a static function with
[DllImport("myDll.dll", CharSet = CharSet.Auto]
public static extern myFunc();
But I would really like to know how to import a whole class and all of its public methods.
Thank you
Why do you want to import the class in a C# application? If you really need to use the methods in the C++ class you will have to write a wrapper C++ based DLL that exposes the methods so that you can P/Invoke or you can wrapp it up in a COM DLL. The COM dll approach will make it easier to you within a C# application.
-
February 21st, 2008, 03:44 PM
#4
Re: Importing C++ Class in C#
I already have the C++ source,
how do I wrap my whole class then so I could use it in a Managed environment ?
A short example would be... in C++
I have that class..
Code:
class myClass {
public:
int getInt();
void setInt(int nInt);
private:
int nMyint;
char cMychar;
}
Then once in C#, I could use my C++ code so that I could do:
Code:
myClass oMyInstance = new myClass();
oMyInstance.setInt(23);
oMyInstance.getInt();
//bla bla bla
I have found a way to do it with static functions built in C++ (by exporting it to a DLL) but can't manage to export/import a whole class.
Thanks a lot
Visual Studio 2005 Professional
-
February 21st, 2008, 06:38 PM
#5
Re: Importing C++ Class in C#
 Originally Posted by lmoreault
I have found a way to do it with static functions built in C++ (by exporting it to a DLL) but can't manage to export/import a whole class.
This is because you cannot import a C++ class into C#. You have already been told of the options in previous replies.
-
February 21st, 2008, 07:15 PM
#6
Re: Importing C++ Class in C#
look up C++/CLI and wrap the native C++ code with C++/CLI code and compile it to a .net assembly and use it in your C# class (where you can use the class like you normally would).
-
February 22nd, 2008, 02:51 PM
#7
Re: Importing C++ Class in C#
I have wrapped my native C++ class in a C++/CLI class (thank you for your patience by the way).
But now, can't seem to find a way to use my new WrapperClass in my C# application, any clue on how to do it ?
Best regard again and sorry for my beginner question.
Visual Studio 2005 Professional
-
February 22nd, 2008, 03:13 PM
#8
Re: Importing C++ Class in C#
You have to add reference in your C# project to dll which contains your managed code.
-
February 22nd, 2008, 04:37 PM
#9
Re: Importing C++ Class in C#
Thanks STLDude,
I have done that, it now works perfectly
Visual Studio 2005 Professional
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|