Best way to re-use C++ classes in .net

P

Priyesh

I am looking at writing a C# application which needs to re-use a
considerable amount of C++ code (data structures and business rules). What
would be the best way to go about this?

I am thinking about having the C++ classes behind an automation server (like
the office object model). Is that the easiest way to go?

Any comments are welcome.

TIA
 
L

Lionel LASKE

You can mix managed and unmanaged C++ code in your new application or you
can build a managed to unmanaged layer to encapsulate your legacy code.
Call of unmanaged code from .NET can have performance drawback but It could
be unexpensive if you call non-atomic business rules.
Web Service could be a more open alternative to Automation Server.

Lionel.
 
P

Priyesh

I might be missing something on mixing managed and unmanaged. Could you
elaborate on that a little?
Here's what i tried to do in that direction. - Create a C++ .net assembly
out of the C++ classes and try to reference them in the C# project. That was
successful with simple classes, but failed with C3633 when member variables
with copy constructors were included.

Maybe i should try the method this article describes?
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/ht
ml/csharp12192002.asp). Wouldnt that create a one-to-one wrapper scenario
for all the concerned C++ classes?

Thanks
Priyesh
 
L

Lionel LASKE

My idea is not to call directly your C++ legacy code from the new
application.
My idea is to build a C++ managed layer to encapsulate your legacy C++
legacy code.
Like this, your C++ managed layer is the only interface to call your legacy
code and you can call it from all .NET language (not only C++).
I experiment this sort of architecture to make an ADO.NET provider for a
legacy database with a C/C++ API only. See
http://www.15seconds.com/issue/030729.htm for an example of this
architecture.
I hope it helps.

Lionel.
 
P

Priyesh

Thanks for the link, I think that will help me get a better understanding
with the examples provided.
 
R

Richard Grimes [MVP]

Priyesh said:
I am looking at writing a C# application which needs to re-use a
considerable amount of C++ code (data structures and business rules).
What would be the best way to go about this?

I am thinking about having the C++ classes behind an automation
server (like the office object model). Is that the easiest way to go?

It is a bad idea unless you intend to allow unmanaged clients (eg VB classic
or VBScript) to access the C++ code. The problem is that you'll have to
write lots of code to create the automation server (and learn all about COM
and OLE) when you really don't need to do that to access the code from .NET.

If your intention is only to access the code from .NET then you have two
options, depending on how your code is designed.

1) If the C++ code is mainly function based then just export them as C
functions and use platform invoke to import them into .NET

2) If the C++ code is mainly class based then use managed C++ to write
wrapper classes.

Richard
 
P

Peter Aitken

Richard Grimes said:
It is a bad idea unless you intend to allow unmanaged clients (eg VB
classic or VBScript) to access the C++ code. The problem is that you'll
have to write lots of code to create the automation server (and learn all
about COM and OLE) when you really don't need to do that to access the
code from .NET.

If your intention is only to access the code from .NET then you have two
options, depending on how your code is designed.

1) If the C++ code is mainly function based then just export them as C
functions and use platform invoke to import them into .NET

2) If the C++ code is mainly class based then use managed C++ to write
wrapper classes.

Do you have any references for technique #2? I am facing exactly this
problem.

Thanks,

Peter Aitken
 

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

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top