all groups > visual c > january 2007 >
You're in the

visual c

group:

VC++ 6.0 project to C# or C++/CLI


VC++ 6.0 project to C# or C++/CLI vijay.gandhi NO[at]SPAM gmail.com
1/30/2007 7:35:30 PM
visual c:
Hi,

I have a project that was created created in C++ using VC++ 6.0.

My current goal is to create .NET based components from the modules of
that project. These components may be consumed by older versions
(before .NET) of Visual Basic and C++ programs. I have a couple of
questions regarding that:

1. I believe it is possible to convert C++ to C++/CLI. Is it possible
to convert the C++ code to C#?

2. If moving to C# is possible, then what would be a better choice -
C# or C++/CLI?

3. Can you please point me to some references which will be useful
concerning these.

Thank you!
Vijay.
Re: VC++ 6.0 project to C# or C++/CLI Bruno van Dooren [MVP VC++]
1/31/2007 6:59:39 PM
[quoted text, click to view]

Hi,

Direct conversion is not possible to C#. they are far too different.
What I would is to upgrade first to VC2005. you'll probably have to refactor
the code to make it standards compliant. VC6 let you get away with a lot of
things that are not compatible with the C++ standard.

Then I would try to compile using /clr and convert different components to
managed classes.

Each application is different however, so a lot depends on what your
codebase looks like.
For other resource I would buy a book on C++/CLI and have a look on
www.codeproject.com

--

Kind regards,
Bruno van Dooren
bruno_nos_pam_van_dooren@hotmail.com
Remove only "_nos_pam"

Re: VC++ 6.0 project to C# or C++/CLI Tamas Demjen
2/1/2007 11:48:29 AM
[quoted text, click to view]

The C++/CLI language is built upon C++. Your C++/CLI code can directly
use C++ classes, and call C++ code.

[quoted text, click to view]

You could use C++/CLI to wrap native ISO C++ classes into managed
objects, which can then be used in any .NET langauge. Typically you do
the wrapping this way:

class OldNativeClass
{
public:
void Method();
};

public ref class ManagedWrapper
{
public:
ManagedWrapper() : p(new OldNativeClass) { }
~ManagedWrapper() { delete p; }
!ManagedWrapper() { delete p; }
void Method() { p->Method(); }
private:
OldNativeClass* p;
};

Once you compile this into a CLR Class Library project, it generates a
DLL that can be consumed by C# directly. ManagedWrapper will be
available, but OldNativeClass will not be.

If your requirement is to create a fully managed output from native C++
code, that has some limitations, depending on whether you need a fully
verifyable assembly or not. Generally speaking, the first step in
porting legacy could should be based on wrappers, so that you can use
your existing C++ code from C#. Once you have accomplished that, and you
are not allowed to use native code at all, then you can worry about
replacing your legacy solution with a fully managed one.

Re: VC++ 6.0 project to C# or C++/CLI Tamas Demjen
2/1/2007 12:02:23 PM
[quoted text, click to view]

I forgot to mention that such a ManagedWrapper automatically becomes an
IDisposable object, and thus you must follow the Dispose pattern,
preferably with the C# using keyword, and in a VB.NET project, using a
try ... catch pattern. Failure to call Dispose on ManagedWrapper may not
be such a big problem, as it provides a finalizer, but who knows when
that is going to be called (sometimes it never will be).

Also, in order to use ManagedWrapper from C# you have to reference the
assembly. I don't think you can mix C++ and C# within the same project,
without actually creating a DLL, but I may be wrong.

Re: VC++ 6.0 project to C# or C++/CLI Ben Voigt
2/1/2007 5:10:17 PM

[quoted text, click to view]

No, many C++ language features have no C# counterpart.

[quoted text, click to view]

Since you already know C++, C++/CLI will serve you far better than C#, even
for new code.

[quoted text, click to view]

Re: VC++ 6.0 project to C# or C++/CLI vijay.gandhi NO[at]SPAM gmail.com
2/4/2007 10:42:54 PM
[quoted text, click to view]

Bruno, Tamas, Ben - thank you all for your replies. That helps me to
start the project.
Vijay.
AddThis Social Bookmark Button