C++ v C#

G

Guest

I have a class library in C++ and it has hundreds of methods each having
scores of static variables. Evidently, I cannot reference this library in a
C# project so I must convert (right?).

In converting the class library to c# I'll need to eliminate the static
variables (right?). To do so, I could declare them at class level or I could
break the library into several classes. Is there any other way? This is a
VERY heavy task with this library.

Do Microsoft developers code in C# or are they secretly using C++?
 
N

Nicholas Paldino [.NET/C# MVP]

MS has a number of products implemented in a number of technologies, C++
and C# included. There is no one language that they code all of their
product offerings in.

As for converting class libraries, there is really no need to do that.
You can always create a COM wrapper around the classes you expose, and then
reference that in your .NET projects, or create a managed wrapper and then
reference that.
 
G

Guest

Where can I learn how to do this COM wrapper?

Nicholas Paldino said:
MS has a number of products implemented in a number of technologies, C++
and C# included. There is no one language that they code all of their
product offerings in.

As for converting class libraries, there is really no need to do that.
You can always create a COM wrapper around the classes you expose, and then
reference that in your .NET projects, or create a managed wrapper and then
reference that.


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

mr peanut said:
I have a class library in C++ and it has hundreds of methods each having
scores of static variables. Evidently, I cannot reference this library in
a
C# project so I must convert (right?).

In converting the class library to c# I'll need to eliminate the static
variables (right?). To do so, I could declare them at class level or I
could
break the library into several classes. Is there any other way? This is a
VERY heavy task with this library.

Do Microsoft developers code in C# or are they secretly using C++?
 
P

pedrito

mr peanut said:
Where can I learn how to do this COM wrapper?

Nicholas Paldino said:
MS has a number of products implemented in a number of technologies,
C++
and C# included. There is no one language that they code all of their
product offerings in.

As for converting class libraries, there is really no need to do
that.
You can always create a COM wrapper around the classes you expose, and
then
reference that in your .NET projects, or create a managed wrapper and
then
reference that.


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

mr peanut said:
I have a class library in C++ and it has hundreds of methods each having
scores of static variables. Evidently, I cannot reference this library
in
a
C# project so I must convert (right?).

In converting the class library to c# I'll need to eliminate the static
variables (right?). To do so, I could declare them at class level or I
could
break the library into several classes. Is there any other way? This is
a
VERY heavy task with this library.

Do Microsoft developers code in C# or are they secretly using C++?

Not to state the obvious, but did you try doing a Google search on COM
wrapper. Surely your answer lies somewhere in the 2.75 million results,
probably 7 or 8 times in the first 10 results.
 
G

Guest

Hummm, Let's think about that. Now Google has some related resources (some of
which are for sale), and some outright misinformation (some of which is for
sale), some unrelated resources with the terms COM and wrapper.

How to identify a resource which is reliable and immediately applicable to
the subject at hand? I like asking an expert. And what better place to find
one than in this forum?

I'm still interested in some expert direction on this (besides go to H...).


pedrito said:
mr peanut said:
Where can I learn how to do this COM wrapper?

Nicholas Paldino said:
MS has a number of products implemented in a number of technologies,
C++
and C# included. There is no one language that they code all of their
product offerings in.

As for converting class libraries, there is really no need to do
that.
You can always create a COM wrapper around the classes you expose, and
then
reference that in your .NET projects, or create a managed wrapper and
then
reference that.


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

I have a class library in C++ and it has hundreds of methods each having
scores of static variables. Evidently, I cannot reference this library
in
a
C# project so I must convert (right?).

In converting the class library to c# I'll need to eliminate the static
variables (right?). To do so, I could declare them at class level or I
could
break the library into several classes. Is there any other way? This is
a
VERY heavy task with this library.

Do Microsoft developers code in C# or are they secretly using C++?

Not to state the obvious, but did you try doing a Google search on COM
wrapper. Surely your answer lies somewhere in the 2.75 million results,
probably 7 or 8 times in the first 10 results.
 
B

Ben Voigt [C++ MVP]

Nicholas Paldino said:
MS has a number of products implemented in a number of technologies,
C++ and C# included. There is no one language that they code all of their
product offerings in.

As for converting class libraries, there is really no need to do that.
You can always create a COM wrapper around the classes you expose, and
then reference that in your .NET projects, or create a managed wrapper and
then reference that.

Recompiling with the C++/CLI compiler and some judicious use of "ref class"
would be much easier than either conversion to C# or a wrapper.
 
P

Philippe

mr said:
Hummm, Let's think about that. Now Google has some related resources (some of
which are for sale), and some outright misinformation (some of which is for
sale), some unrelated resources with the terms COM and wrapper.

How to identify a resource which is reliable and immediately applicable to
the subject at hand? I like asking an expert. And what better place to find
one than in this forum?

I'm still interested in some expert direction on this (besides go to H...).

Well, if you want expert *direction*, I'd suggest the nearest technical
school.. Here, you'll get input from peers, some with more or less
experience than you do.. The MVPs are a great source, then again doing a
bit of the legwork yourself (say, doing an advanced google group search,
targetting this NG or others like it and searching specifically for COM
wrappers or some such, since I really doubt you're the first to ever
bring it up) is definitely worth the effort..

HTH
P.
 
N

Nicholas Paldino [.NET/C# MVP]

From my experience, in most cases, would be a complete and utter
disaster, as usually there are patterns with memory allocation as well as
lifetime issues (due to the introduction of GC) which change completely with
a transition to .NET.
 
C

Carl Daniel [VC++ MVP]

Nicholas said:
From my experience, in most cases, would be a complete and utter
disaster, as usually there are patterns with memory allocation as
well as lifetime issues (due to the introduction of GC) which change
completely with a transition to .NET.

But isn't that missing the whole point of using C++/CLI for interop? You
don't transition the existing code to .NET. Rather, you build a bridge
between the two worlds. Not that it can't be fraught with pitfalls (which
it certainly can), but in many cases, particularly if the C++ code was well
designed, it's quite possible to build a very usable interop layer without
disturbing the C++ code much (if at all).

-cd
 
N

Nicholas Paldino [.NET/C# MVP]

I don't believe it is missing the point. The suggestion was "judicious"
use of "ref class", which will lead exactly to the pitfalls that I mentioned
in my post.

I am in agreement that a well designed class library will help, and that
CLI is a good thing.
 
P

pedrito

mr peanut said:
Hummm, Let's think about that. Now Google has some related resources (some
of
which are for sale), and some outright misinformation (some of which is
for
sale), some unrelated resources with the terms COM and wrapper.

How to identify a resource which is reliable and immediately applicable to
the subject at hand? I like asking an expert. And what better place to
find
one than in this forum?

I'm still interested in some expert direction on this (besides go to
H...).

Sorry, I didn't mean it to be a "go to h" kind of comment so much as a hint:
Do a google search on COM Wrapper

The first result:

http://www.codeproject.com/csharp/SafeCOMWrapper.asp

CodeProject isn't always great, but it's a pretty detailed description fo a
Disposable wrapper which will give you a lot of ideas of how to do a general
wrapper, written by someone who clearly knows what he's doing.

The 5th or 6th item is here:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=281150&SiteID=1

is a post in which someone asks: "I have a unmanaged code which I want to
use in C# application...How can I create a COM wrapper to use in that
code...If someone can give some example...."

The response is this link:
http://msdn2.microsoft.com/en-us/library/ms173184.aspx

Which takes you to the MSDN Interoperability (C# Programming Guide)

If you don't consider that expert information, you're certainyl not going to
find any here.

I found all that in a quick google search when you asked the question, which
is why I asked if you had done a google search on COM Wrapper.

This isn't even my question, it's yours... I think the point Philipe made in
the follow up was precisely my point: "The MVPs are a great source, then
again doing a
bit of the legwork yourself (say, doing an advanced google group search,
targetting this NG or others like it and searching specifically for COM
wrappers or some such, since I really doubt you're the first to ever
bring it up) is definitely worth the effort.."
 
A

Andre Kaufmann

Nicholas said:
I don't believe it is missing the point. The suggestion was
"judicious" use of "ref class", which will lead exactly to the pitfalls
that I mentioned in my post.

What do you mean exactly. That Dispose must be explicitly called if
unmanaged resources are wrapped with a "ref class" ?

IMHO the situation is more worse with COM wrappers, where additionally
reference counting is involved.

Andre
 
A

Andre Kaufmann

mr said:
I have a class library in C++ and it has hundreds of methods each having
scores of static variables. Evidently, I cannot reference this library in a
C# project so I must convert (right?).

Don't think so. IMHO the best solution would be to use C++/CLI and wrap
the C++ classes with managed ones.
The MS VS C++ is capable of compiling native and managed code to a
single assembly. You could even link C#, C++/CLI and C++ into a single
assembly (executable), though this can be only accomplished by using the
linker from the command line.

Andre
 
N

Nicholas Paldino [.NET/C# MVP]

Andre,

Just slapping ref class on all of your classes isn't going to solve the
problem. Some classes depend on deterministic finalization, and some
classes have methods that dictate patterns of memory allocation/release (by
returning pointers) which require a little more thinking.

I don't think that the solution is worse from a conceptual view with COM
wrappers, as the model of reference counting and deterministic finalization
go well together, IMO. I do think that the implementation of COM wrappers
though can be a PITA.


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

Andre Kaufmann said:
Nicholas said:
I don't believe it is missing the point. The suggestion was
"judicious" use of "ref class", which will lead exactly to the pitfalls
that I mentioned in my post.

What do you mean exactly. That Dispose must be explicitly called if
unmanaged resources are wrapped with a "ref class" ?

IMHO the situation is more worse with COM wrappers, where additionally
reference counting is involved.

Andre
 
B

Ben Voigt [C++ MVP]

Nicholas Paldino said:
Andre,

Just slapping ref class on all of your classes isn't going to solve the

"slapping ref class on all of your classes" is exactly what "judicious" does
not mean. Judicious use would be converted the outermost, publicly visible
class to a ref class, and leaving all of the internal helper stuff native.
problem. Some classes depend on deterministic finalization, and some
classes have methods that dictate patterns of memory allocation/release
(by returning pointers) which require a little more thinking.

So you leave the delete calls in place, which causes -- surprise surprise,
deterministic finalization. So do stack semantics. C++/CLI has this
problem solved in a way that is very compatible with native code and far
superior to C#.
I don't think that the solution is worse from a conceptual view with
COM wrappers, as the model of reference counting and deterministic
finalization go well together, IMO. I do think that the implementation of
COM wrappers though can be a PITA.

A "ref class" wrapper is far superior to COM (in terms of error reporting,
assembly signing, partial trust sandboxing, and performance just to name a
few ways).

Anyway, COM reference counting isn't deterministic when used from .NET.
IUnknown.Release will be called only when the referent object is collected,
which in .NET is not determistic without adding explicit Dispose calls.
 
A

Andre Kaufmann

Nicholas Paldino [.NET/C# MVP] wrote:

Nicholas,
Andre,

Just slapping ref class on all of your classes isn't going to solve the
problem. Some classes depend on deterministic finalization, and some
classes have methods that dictate patterns of memory allocation/release (by
returning pointers) which require a little more thinking.

I rather thought of wrapping the internal classes with a single or few
ref classes, exposing an interface to access internal functionality.
A C++ class returning a pointer, must be wrapped by a ref class wrapping
the returned pointer.
I know it's not that simple, but IMHO the simplest and straight
forwardest solution of porting to / using native C++ code from a managed
application.
I don't think that the solution is worse from a conceptual view with COM
wrappers, as the model of reference counting and deterministic finalization
go well together, IMO. I do think that the implementation of COM wrappers
though can be a PITA.

Well, at least I had problems with a COM callback, passing a huge number
of COM objects per second to a C# application. Which held all references
, even if I explicitly released and Disposed the COM objects. Perhaps it
was a problem of .NET 1.1 or a general one, because I read about that
this might indeed raise problems and should be prevented.


Andre
 

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