Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
hello programmers
I want (if exist) a program that convert c code to c# automatically. if there is not program such that pleas can you advise me how to make simple one, from where can i start what document i have to read

thanks
best regards

What I have tried:

I was make search in web and get this program (CxxCavaConverter) but it is not application it is just library for code.
Posted
Updated 22-Aug-18 16:18pm
v2
Comments
F-ES Sitecore 18-Mar-16 10:34am    
Given they run in environments that are very dissimilar I doubt you'll find much that can do anything beyond the very basics. Your best bet is to start learning c# so you can convert the code manually, or amend the C code to expose COM interfaces that you can then use in c# or just use the Imports feature to call directly into the c dll.
alcitect 18-Mar-16 11:49am    
thanks for reply
my job is to find ab automatic program that convert. i have to get my way to do that.
Arthur V. Ratz 21-Mar-16 3:00am    
5+
Philippe Mori 18-Mar-16 22:58pm    
The only code that can relatively easily be converted is safe/pure code C++/cli code that essentially only use .NET stuff.

Often, since we want to keep comments and we want to have code that look as much as possible as the original code (for ex. for loop stay for loops), it is often faster to copy C++ code and modify it by hand.

You would typically so some find and replace of -> or :: with . and unsigned char to byte etc... but you have to be careful to ensure that all change are valid.

It might make sense to convert some files by hand but usually you might prefer to insert a mixed-mode C++/cli assembly between native C++ and managed C#. Then you have full control on what you do.
Philippe Mori 18-Mar-16 23:00pm    
It might be useful if you could tell us more about the C++ code. Is it MFC? ATL? standard C++...

Also, showing us typical errors you get might allows us to give some specific tips...

Don't.
Although C and C# look very similar, they are very, very different at a fundamental level: C# is OOPs based, and C is most definitely not! And although the syntax looks very similar, C# is heavily reference based, and tries hard to prevent you using pointers (which aren't the same thing at all) where C is heavily stack based, and uses pointers extensively.
Converting any C code (other than trivial snippets) is going to produce bad C# code at best - and trivial snippets alone don't need any real help to convert!
Use the C code as a basis, and design the C# code to do the same function but within a C# OOPs environment. You'll get a much better result, and probably a lot quicker!
 
Share this answer
 
Comments
alcitect 18-Mar-16 12:34pm    
thanks for reply, i agree with you but my question is c++, just change it
Arthur V. Ratz 21-Mar-16 3:01am    
Good solution. +5
Don't bother converting the code.

The only reason to move to C# is access to the .NET framework. Compile the code as C++/CLI which is C++ for .NET. You can combine with other .NET projects (including C# or VB.NET).
 
Share this answer
 
Comments
Matt T Heffron 18-Mar-16 18:29pm    
Easier said than done!
Maybe it'll work if it was coded against a comparatively recent C++ standard.
We have a LARGE VC6 C++ application and trying several years ago to compile it as C++/CLI gave tens of thousands of errors!

Edit: I wasn't trying to imply that this was a bad suggestion!
This is an excellent suggestion.
I was just trying to "set expectations" that it wouldn't be automagic!
+5
Sergey Alexandrovich Kryukov 18-Mar-16 19:56pm    
Yes, of course, and yet, this is the most reasonable advice in the situation. Who told you that it's going to be easy? And of course, no "automatic conversion", by one simple reason: it would be an ill-posed problem; these words cannot serve as a valid formulation of a problem.
—SA
Matt T Heffron 18-Mar-16 20:01pm    
Agreed.
Philippe Mori 18-Mar-16 22:49pm    
First of all, you have to ensure that you don't select pure or safe mode but only /clr.
Normally, it should not be hard to compile VC6 C++ code in more recent version of Visual Studio if you use the proper options and fix some "broken" code (for example loops that use old scoping rules).
If you code does not compile in mixed-mode then the best option would probably be to create an additional mixed-mode assembly that sit between the legacy C++ code and the C# code.
When you have a lot of errors, you should compile one file at a time.
By the way, if you use precompiled header, you might need more than one precompiled header if you try to have all your C++ code in the same assembly. And you cannot use some "Windows" header when compiling managed code. So you have to be very careful for incompatible definitions... For any large code base, a solution consisting of C++ DLL, mixed-mode C++/cli assembly and C# assembly is certainly much easier to implement particularly if you have a lot of errors otherwise.

But we cannot tell you what is the best solution as it depends a lot on the actual code (and the kind of errors you have...).
Arthur V. Ratz 21-Mar-16 3:01am    
5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900