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

V

vijay.gandhi

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

Bruno van Dooren [MVP VC++]

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.

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
(e-mail address removed)
Remove only "_nos_pam"
 
T

Tamas Demjen

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

The C++/CLI language is built upon C++. Your C++/CLI code can directly
use C++ classes, and call C++ code.
2. If moving to C# is possible, then what would be a better choice -
C# or C++/CLI?

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.

Tom
 
T

Tamas Demjen

Tamas said:
it generates a
DLL that can be consumed by C# directly.

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.

Tom
 
B

Ben Voigt

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#?

No, many C++ language features have no C# counterpart.
2. If moving to C# is possible, then what would be a better choice -
C# or C++/CLI?

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

vijay.gandhi

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




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

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

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