views:

269

answers:

3
+2  Q: 

Mixing c# and vc++

I've got a code snippet of c# that I've been trying to translate into vc++, but I'm having some difficulties. So, I'm wondering if it is possible or advisable to mix c# and vc++; that is to say, can I call my c# function from vc++ and vice-versa. If so, are there tricks to it? If, not, why?

A: 

If you want to mix the two, I'd start with C# as the base project and then register an unmanaged (C++) DLL in your C# project. That way you can combine the reliability of the managed C# parent application with the speed benefits of C++ in any processor-intensive methods you might have.

Steve Wortham
the app already exists in c++, I'm just adding on to it.
thepocketwade
A: 

Is the VC++ app managed? If so, you could create a C# DLL, use it from your c++ app, and if you so desire, ILMerge them into a single EXE.

Snarfblam
it is not managed
thepocketwade
+1  A: 

Calling C++ code from C# is easy enough and you should be able to find plenty of articles on Pinvoke and other types of interop.

For calling your C# code from C++ - if your C++ app is managed then you should be able to directly reference the C# assembly from your C++ app and use it as normal.

If ypu C++ code is not managed then you will need to use some sort of interop to allow your C++ assembly to call C# code, the only way of doing this that springs to mind is COM.

Try some of these links for more information on COM interop with C#:

Kragen
@Kragen - in your 2nd paragraph did you mean "For calling your C# code from C++..."? But otherwise great answer! I've had decent luck using .NET DLLs as ActiveX COM servers in other languages, as described in the second link you provided.
mtrw
Whoops, fixed it now!
Kragen