Click to See Complete Forum and Search --> : Importing C++ Class in C#
lmoreault
February 20th, 2008, 01:19 PM
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
Arjay
February 20th, 2008, 03:28 PM
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#.
nelo
February 20th, 2008, 05:27 PM
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.
lmoreault
February 21st, 2008, 02:44 PM
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..
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:
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
Arjay
February 21st, 2008, 05:38 PM
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.
MadHatter
February 21st, 2008, 06:15 PM
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).
lmoreault
February 22nd, 2008, 01:51 PM
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.
STLDude
February 22nd, 2008, 02:13 PM
You have to add reference in your C# project to dll which contains your managed code.
lmoreault
February 22nd, 2008, 03:37 PM
Thanks STLDude,
I have done that, it now works perfectly :)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.