old C++ called from C#

  • Thread starter Thread starter Daniel P.
  • Start date Start date
D

Daniel P.

How can I do that?

Would it work if I added the C++ code to the current C# project/colution? Do
I have to create another assembly with a managed C++ wrapper to the old C++
code?

Thanks!
 
Daniel,

One way to use ur old c++ code is to compile as a dll. In c#, you can access
the functions using "DllImport".

Unsafe c# is another way to use c++ code. Refer MSDN regarding that.
 
I used a managed C++ wrapper for my old C++ code.

In your solution add a managed C++ library. Add your existing C++
source to the project and compile. Once everything compiles, add
managed C++ classes to create the API that you want your C# code to see.

After doing this you will likely get a LNK 4243 warning. Search for
this warning in your local MSDN docs or the MSDN website to find out how
to initialize the C run time library.

If you don't already know managed C++ you might want to use a Beta
version of VS 2005, or wait until it is released. From what I here the
managed C++ syntax will change in VS 2005.

Another posibility is to use the Unix way. Set up your C++ code as a
separate process. Use sockets to communicate between the program
written in C++ and the one written in C#. I was just reading "The Art
of Unix Programming" on the weekend, so havn't tried this technique yet.

Bill
 
Beeeeeeeeeeeeves said:
What NOT to do: "MANAGED C++" - it is dire!

What *to* do: create an unmanaged C++ dll and call it using PInvoke (i.e.
the DllImport statement, in namespace System.Runtime.InteropServices)

Overall that is a rather broad statement and can lead to extreme problems.
DllImport is not type safe, so using it poses far more risk than "Managed
C++". Using C++ to create a type safe wrapper that can be exposed to other
..NET languages is by far a more superior approach.

There are instances where using DllImport is preferable (perhaps when
dealing with multiple AppDomains in the VC7 or VC7.1 products), but the
statement here doesn't discuss that at all.
 
Back
Top