Question

I have an existing C# application. What I want is to write native code for some part of the c# application for speed improvement while another part using C# app and the other part using C++/CLI to provide interfaces to C# application. So for latter part, I need to convert C# code into C++/CLI.

I am wondering if I can convert my C# code to C++/CLI code. Maybe converting .net assembly to C++/CLI would be a better idea to get specific code. I looked into reflector and its addin CppCli. But the addin is not available any more. So I am looking for any way to automate this. I would appreciate any advice.

Was it helpful?

Solution

Generally speaking C++/CLI will have about the same run-time as C# (both are managed and probably would be converted to the same IL). If there is a certain part of your application you need to run faster you can write a C++/CLI layer so your C# app can interop with some native C++. This will be faster if you write your native C++ appropriately.

EDIT: As Ben Voigt pointed out, the C++/CLI might be more optimized than the C#, but still not nearly as fast as well-written native C++ since it's still managed.

If you're looking to wrap some native code in C++/CLI then a conversion tool probably wouldn't structure it in a useful fashion anyway. You're better off just hand-coding the interop portion.

There are commercial tools out there that do this, but I think you're still better off doing it by hand.

OTHER TIPS

You may not have to convert the code.

link.exe, part of the C++ compiler tools, is able to include both C++ and C# code in the same assembly. The C++ bit can include a mixture of managed and unmanaged code. You first have to compile the C# code to a .netmodule with command line switches of the C# compiler (csc.exe), and then you can use link.exe to compile it into an assembly with C++ code. It's been a while since I've created a mixed language assembly so apologies if the details are not 100%, but search for the above terms and you will find a way to do it.

I seem to remember that the key part is remembering that the C++ compiler is more advanced than the others and so can consume C#/VB netmodules, but not the other way round. I found that the advantage of compiling into a single assembly rather than one referencing another is that types inside each part of the single assembly can cross-reference each other. By having references between two separate assemblies the type awareness relationship has to be hierarchical.

There is also the unsafe keyword in C#, which allows C++-ish style pointers to be used within sections of a C# code file, and turns off array bounds checking. There are disadvantages of this that may or may not matter depending on your usage scenario.

Personally I found that running some calculation-intensive code was 20x quicker in the C++ compiler compared to C#, after I had optimised it using the pointers available in C++, which was worth the effort in my case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top