Click here to Skip to main content
15,032,967 members
Please Sign up or sign in to vote.
1.67/5 (5 votes)
See more:

i have a program in c language and i want to convert that to c# but my program is more than 3000 lines ...

so please give me some advices to do

is there any way to convert?
Updated 27-Jul-21 7:49am
Zoltán Zörgő 21-Aug-12 3:10am
My guess (might be wrong): this is a case of reusability. A homework made years ago in C "wants to be" reissued by an other student to an other teacher in c# :)
sahrarainmelody 22-Aug-12 0:49am
no guy you are wrong ... its a state flows output that i try to convert shoud become visual
thanks anyway

The way to convert is to learn the relevant algorithms from the original source code or otherwise and then implement the application in C#. Some fragments of code could be more or less literal translation with somewhat different syntax, but most should be deeply different. In return, chances are, a lot of things you will find in the C program could be thrown out, because the are already implemented in .NET libraries. It all strongly depends on the character of original program. For example, big part of pure numeric calculations could be very similar.

If you hoped for some "automatic" way of conversion — just forget it. The languages and especially platforms are very, very different, both conceptually and formally.

You can consider some alternatives not related to translation though.

First one: you can re-word original application into a native DLL and then use it in your C# application via P/Invoke. If you need to learn P/Invoke, please see:[^],[^].

This CodeProject article can be also helpful: Essential P/Invoke[^].


In response to the follow-up questions:

The above references are useful to use unmanaged DLLs if they are already available. To re-work C code in DLL and export function to be used by .NET assemblies, please start from here:[^],[^].

You can find a lot more help on DLL creation. Make sure you only use the types which .NET can understand. If you try to use only primitive types, strings and enumerations, this is easy. Structures and pointers other then strings is a bit more tricky. At first, try to keep interfaces between DLLs and .NET as simple as possible.


Second one: you can use mixed-mode (managed+unmanaged) C++/CLI project to embed existing C code. Such project can more or less freely mix C++, C++/CLI and C code. You can wrap your C code in some managed "ref" classes/structures and make them public. The resulting executable module can be used as a regular .NET assembly, referenced by your C# application. Please see:[^],[^],[^],[^].

Good luck,
sahrarainmelody 22-Aug-12 1:06am
thanks alot ...

i try to use of "DLL " way :i create a dll file of my code and call it in a c# file but my program consist of 3 c source code and i shoud join them and i know anything of c .i can create dll for one but 3 .

and another thing : i see

too but this pages dont tell us that how can we create a dll file from c source ... can you help me more?
Sergey Alexandrovich Kryukov 22-Aug-12 11:08am
With one DLL this is quite doable, but just needs some attention. When you work with source code, there is no big difference if you merge it in one DLL or not. Besides, you can have 3 separate DLLs. I would also advise to make it C++ project, which can contain both C and C++ source. With C++, making DLLs is easier.

Sure, my references are only about using native DLLs in .NET, not about creation of DLLs in Windows. I though you knew it all. You can easily find help on creating DLLs. More exactly, this is about exports of functions in DLL.

Sergey Alexandrovich Kryukov 22-Aug-12 11:14am
In response to this request, I added one more link to the answer, after [EDIT], to help learning how to create unmanaged DLLs and export functions.

BillW33 9-Sep-12 8:11am
Very fine answer, +5
Thank you,
Philippe Mori 23-Apr-16 10:55am
Very good answer. We should be able to vote +10 to compensate for the one that vote 1 without valid reason.
Sergey Alexandrovich Kryukov 23-Apr-16 13:29pm
Thank you, Philippe.
You will never get the answer you expect.
1) As I know C has no .net enabled edition, thus you can not use the intermediate language as helper
2) C has really lower level concepts than any managed language
3) C is not OO, c# is OO language - and a really good one
4) Thinking in C is completely other than thinking in C#
You will find no automatic means to do this translation. And in general it is also impossible to do it at all. Only in special cases there might be an equivalent of the C application in C#.
And by the way, 3000 lines of code are not that much...
Good explanation, a 5. But I suggested some alternatives (other then translation) which could potentially work, please see my solution.
There is no automatic way to convert that I am aware of - it is a surprisingly difficult job to do automatically, since C# has no concept of globals, does not want you to use pointers, and is strongly tied to the .NET framework so the functions your C code uses will not be applicable in C#.

I suspect your best approach will be a re-write, rather than a translation, as the two languages are a lot more different in practice that they appear.

To paraphrase Winston Churchill / George Bernard Shaw: Two languages separated by a similar syntax.
In addition to all answer above that are really good, I would add some tricks that might help you.

How well it work would depends a lot on the original code. Would it be a good idea to convert it? This also depends. That kind of conversion might works relatively well for mathematical stuff or maybe encryption. On the contrary, it would not works well if your code depends on many C libraries, complex macros...

It would be up to you to evaluate which option is best:
- Try to convert the code manually.
- Rewrote the whole code in C# (and only use the old code as a reference).
- Put your C code in a DLL and use it either with P/Invoke or mixed-mode assembly.

You could start by copying the existing C code in a C# file and put all function in a class. Make that class static and all functions public static.

Remove includes and other specific C stuff.

For each function in C code, make a private static function with the exact same name as the original one. For example:
public static class ConvertCodeInC {
  // Copy C code here...

  // Sample function (add required using as appropriate for your code)
  private static double fabs(double x) => Math.Abs(x);

  private static void more_complete_function_here(int value)
    // Some code here...

The you can also convert macros to constants or functions as appropriate. Update types.

Many of that can be done with find and replace. For example, replace unsigned int with uint. Also replace -> with ..

Once the program is converted, you can do Rename method to remove useless temporary functions like fabs in my example. If the function is used only a few times and is trivial, it would be simpler to immediatly update it to call the .NET equivalent function.

Be aware that things like find and replace might be somewhat dangerous in a case like that so save often and confirm each change if necessary.

Once the code is compiling, you can then make it more .NET by refactoring it to use .NET naming convention for example.
Patrice T 23-Apr-16 11:29am
The question is from 2012 !
Philippe Mori 23-Apr-16 11:56am
Usually I don't pay attention to the original date as I only check recently added/updated questions.
Patrice T 23-Apr-16 12:01pm
Since a few weeks, old questions are popping on top of list with no obvious reason.
Sergey Alexandrovich Kryukov 23-Apr-16 13:27pm
I would say, date does not matter, especially if the question wasn't answered well enough.
Sergey Alexandrovich Kryukov 23-Apr-16 13:26pm
Fresh answer? I wonder what raised the interest in this question of 2012 again...
Southmountain 23-Sep-16 15:26pm
Thanks for Philippe. Your comments are very useful for my project.

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