C# SUCKS - BIG TIME !!! (Arrrgh !!!)

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#:
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
function,


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 ?
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
C++/CLI) that wrap your unmanaged C++ class(es).


<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 ....

chill 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
#define CCONV __declspec(dllimport) #endif
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
object instances afterwards, dont ya?
////////////////////////////////////
I just roughly put some sample above, it may not workin extactly as
ya wish but ya hav got the idea.
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.
 
O

Olaf Baeyens

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.

Then my advice is to move away from C# if you hate it that much.
 
G

Guest

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

that's not a blanket line. that's exactly how you solve your problem.
write a managed C++ wrapper, and call the wrapper from C#. you are looking
at p/invoke C functions, which is the wrong direction. so instead ranting,
go learn managed C++ like everyone told you to and have your problem solved.
 
O

Olaf Baeyens

that's not a blanket line. that's exactly how you solve your problem.
write a managed C++ wrapper, and call the wrapper from C#. you are looking
at p/invoke C functions, which is the wrong direction. so instead ranting,
go learn managed C++ like everyone told you to and have your problem solved.
Actualy he is posting in the wrong newsgroup. It is a VC problem, not a C#
problem he is facing.

ET. I am doing it, so it can be done, but if you want people to help you
then you must stop ranting first.
And if you use Google in the newsgroups then you will find some posts from
me how to do this.
 
N

Nicholas Paldino [.NET/C# MVP]

Wow. That's like, well, very angry.

I used the search phrase "managed wrapper C++" on the MSDN site, and it
gave me this first link:

Accessing C++ Code from .NET Framework Objects (Managed Extensions for C++
Specification)

Which is located at (watch for line wrap):

http://msdn.microsoft.com/library/d...ml/vcmg_accessingcodefromframeworkobjects.asp

Then, looking at the table of contents on the left, I noticed it was
part of a larger topic titled:

Part I: Introduction to Wrapping C++ Classes

Which is located at (watch for line wrap):

http://msdn.microsoft.com/library/d...us/vcmxspec/html/vcmg_overview.asp?frame=true

So all-in-all, it wasn't too hard to find.

You should note that this is syntax in C++ using Managed Extensions
(which was released with .NET 1.1 and before). If you are using .NET 2.0,
then you should use the CLI syntax for creating managed code in C++.

Fortunately, searching for the phrase "cli managed c++ wrapper" on MSDN
gave me this as the second link:

Translation Guide: Moving Your Programs from Managed Extensions for C++ to
C++/CLI

Which is located at (watch for line wrap):

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/TransGuide.asp

I don't know, but when a search engine turns up what you need as the
first or second result, it calls into question how hard one might have
actually looked. It is understandable that you used the wrong search
phrases, but I think that was pointed out to you already.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


E.T. Grey said:
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#:
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
function,


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 ?
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
C++/CLI) that wrap your unmanaged C++ class(es).


<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 ....

chill 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
#define CCONV __declspec(dllimport) #endif
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
object instances afterwards, dont ya?
////////////////////////////////////
I just roughly put some sample above, it may not workin extactly as
ya wish but ya hav got the idea.
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.
 
M

Markus Stoeger

E.T. Grey said:
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#?

This book helped me a lot when I had to use old C++ code in C# Apps:

http://www.microsoft.com/MSPress/books/5959.asp

hth,
Max
 
R

Richard Grimes

E.T. Grey said:
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

Have you read the document in the VC7 folder?
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

Well I show how to do it in my managed C++ book. But it ain't pretty and
you really should not import unmanaged C++ classes into .NET. The two
main problems are that 1) methods are exported as mangled names, so you
have to import then as such, and 2) you have to be aware that there is a
hidden, 'this' parameter, so you have to add that extra parameter to
your C# imported functions. Honestly, this is not the way to do things.
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 ....

I would tell you, but since you're being aggressive you'll have to pay
the $50 for my book. On the other hand, you could do what I did and read
the documentation for [DllImport] and work it out (it is very
straightforward). If you look on the web you'll find lots of examples,
and usually I would point you to one, but as I've said, you're being
aggressive about this, so you'll just have to use Google, like the
polite posters here do.
In order to use C++ classes in C#, you have to write a C API

Well Doh! .NET is a managed runtime, unmanaged C++ concepts mean nothing
there. (For example, in 1.1 destructors are not called when an object
goes out of scope.) So you simply cannot plonk a native C++ object in
your .NET code and expect it to work the way it works in the unmanaged
world.
(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

That's one way. The other way is to write a managed C++ wrapper around
the native C++ wrapper and call that from C#. Again, it is straight
forward.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top