E
E.T. Grey
usng a lot of advanced C++ features like templates, partial/specialized
templates, functors and callbacks. I am also using STL containers like
std::string, std::vector and std::map quite extensively in my C++ DLL API.C#. Below, is a very simple "proof of concept" C++ DLL. I would be
extremely grateful if someone could show me how I can use this Dll from C#:
What is so difficult to understand by this statement?. C++ methods *can*
throw exceptions - maybe managed C++/C# (or should that be C dumb)
classes can't ?
<snip>
I already knew that. Everybody (including the MS site tells me I need to
write managed C++ wrapper classes around my C++ classes - but there is
no useful examples given anywhere - most of the examples are to do with
calling C functions from C#. So far its been all talk and no examples -
has anyone *actually* EVER called C++ classes from C#? If it can't be
done or it has never been tried out in the real world - just be man
enough to say it instad of issuing the blanket line : "write managed C++
classes around your C++ classes". No one as yet (and that included MS
itself) afaik, has actually provided an example of using C++ classes
exported from a C++ DLL - so come on : can it be done or NOT ?. If yes
(everyone tells me it can - but has no examples) - HOW the hell is it
done - does anyone actually even know?. I provided a simple no brainer
C++ DLL and expected at least some pseudocode to show how I can call the
2/3 functions from C# - apparently, no one really knows how to do this ....
Ok, now we are at least getting somewhere. However, your code (assuming
that this is the ONLY way forward) proves *precisely* what I had
suspected all along:
In order to use C++ classes in C#, you have to write a C API
(i.e.wrapper) around your C++ classes and *then* use the C API from
managed C++ - lol and lmao !!!, excuse me for my mirth - but one has to
admire MS for being such a dominat force in the industry despite the
fact that it has so many half-assed, half baked "technologies" which are
so - erm f*@ked up and proprietary. C# appears to be nothing more than a
blatant plagiarism of Java (not to mention a few bits and pieces
"borrowed" from C and C++) and just enough proprietary crap to ensure
that it is not compatable with any of the "true" standard languages out
there.
This sucks BIG TIME !
PS: Thanks anyway Ashura - at least you tried. But this half cooked
language (if you can call it that) does not seem to have any saving graces.
templates, functors and callbacks. I am also using STL containers like
std::string, std::vector and std::map quite extensively in my C++ DLL API.C#. Below, is a very simple "proof of concept" C++ DLL. I would be
extremely grateful if someone could show me how I can use this Dll from C#:
function,Here is the code:
/* C++ code (Header)
(trivial proof of concept DLL */
#ifdef TESTDLL_EXPORTS
#define CCONV __declspec(dllexport)
#else
#define CCONV __declspec(dllimport)
#endif
typedef enum {
ONE ,
TWO ,
THREE
} myEnum;
typedef struct mystruct_ {
int x ;
float y ;
char z[8] ;
} myStruct ;
typedef int (*INT_FPTR)(const char*, const MyStruct*) ;
class CCONV MyClass {
public:
MyClass();
MyClass(const MyClass&);
MyClass& operator = (const MyClass&);
~MyClass();
private:
//some private variables here ..
};
class CCONV DClass : public MyClass {
public :
DClass();
DClass(const DClass&);
DClass& operator = (const DClass&);
~DClass();
const char* foo(void);
int barney(int, myEnum, const myStruct*); //throws an exception
void register_callback( INT_FPTR ) ;
private:
INT_FPTR cb ;
};
Many Thanks
What do you mean by throws an exception?, "barney" is a C++ member
What is so difficult to understand by this statement?. C++ methods *can*
throw exceptions - maybe managed C++/C# (or should that be C dumb)
classes can't ?
C++/CLI) that wrap your unmanaged C++ class(es).you can't create instances of native C++ classes from C#, so you can't call member functions.
Your only options are:
1. create a managed C++ class in a managed assembly (using vs2005
<snip>
I already knew that. Everybody (including the MS site tells me I need to
write managed C++ wrapper classes around my C++ classes - but there is
no useful examples given anywhere - most of the examples are to do with
calling C functions from C#. So far its been all talk and no examples -
has anyone *actually* EVER called C++ classes from C#? If it can't be
done or it has never been tried out in the real world - just be man
enough to say it instad of issuing the blanket line : "write managed C++
classes around your C++ classes". No one as yet (and that included MS
itself) afaik, has actually provided an example of using C++ classes
exported from a C++ DLL - so come on : can it be done or NOT ?. If yes
(everyone tells me it can - but has no examples) - HOW the hell is it
done - does anyone actually even know?. I provided a simple no brainer
C++ DLL and expected at least some pseudocode to show how I can call the
2/3 functions from C# - apparently, no one really knows how to do this ....
#define CCONV __declspec(dllimport) #endifchill man, we r all here to help others!
while ya cudnt create instance of c++ class in c#, its a bloody truth.
HOWEVER, this NOT so hard to get around.
//remove those lines out cuz ya dont need to export the whole
//class out from a c++ class
#ifdef TESTDLL_EXPORTS #define CCONV __declspec(dllexport) #else
object instances afterwards, dont ya?NO CHANGES for ya current classes, they r workin just fine
//add the following
extern "C" __declspec(dllexport) DClass* CreateInstance();
extern "C" __declspec(dllexport) DClass* DestroyInstance(DClass* obj);
//create the methods to export
extern "C" __declspec(dllexport) DClass* CreateInstance()
{ return new DClass(); }
extern "C" __declspec(dllexport) DClass* DestroyInstance(DClass* obj)
{ delete obj; }
/////////////////////////////////////
After ya hav done ALL above, im pretty sure ya kno how to deal with
ya wish but ya hav got the idea.////////////////////////////////////
I just roughly put some sample above, it may not workin extactly as
hope this helps
Ok, now we are at least getting somewhere. However, your code (assuming
that this is the ONLY way forward) proves *precisely* what I had
suspected all along:
In order to use C++ classes in C#, you have to write a C API
(i.e.wrapper) around your C++ classes and *then* use the C API from
managed C++ - lol and lmao !!!, excuse me for my mirth - but one has to
admire MS for being such a dominat force in the industry despite the
fact that it has so many half-assed, half baked "technologies" which are
so - erm f*@ked up and proprietary. C# appears to be nothing more than a
blatant plagiarism of Java (not to mention a few bits and pieces
"borrowed" from C and C++) and just enough proprietary crap to ensure
that it is not compatable with any of the "true" standard languages out
there.
This sucks BIG TIME !
PS: Thanks anyway Ashura - at least you tried. But this half cooked
language (if you can call it that) does not seem to have any saving graces.