Converting from C to C#
-
hi, I'm developing a web application for my graduation project and it's my first time to use asp.net and C# , the problem is I have an essential part of a code written in C and I need it to be converted or embedded in my project. I found a couple of converters but they all say that they are not reliable, another problem is that the C code has LOTS of pointers and I'm afraid that I'll mess things up.. is there a reliable converter or a reliable way ? or anyone who can compare both and make sure that they are right ? Thanks
-
AseelHadlaq wrote:
embedded in my project.
You can use 'interop' to call external code in C#.
How can I use it ? I'm new to C# and can I send and retrieve variables ?
-
hi, I'm developing a web application for my graduation project and it's my first time to use asp.net and C# , the problem is I have an essential part of a code written in C and I need it to be converted or embedded in my project. I found a couple of converters but they all say that they are not reliable, another problem is that the C code has LOTS of pointers and I'm afraid that I'll mess things up.. is there a reliable converter or a reliable way ? or anyone who can compare both and make sure that they are right ? Thanks
All converters will offer a free trial/demo version. Try it out and compare the results with some common sense. Even if the conversion is less than 100%, it may give you something you would have missed. The main challenge of converting C/C++ code to other languages, especially with pointers, is that a C/C++ declaration alone does not tell you how something is used. This is in contrast to C# where the declaration does tell you all you need to know for a translation to other languages.
David Anton Convert between VB, C#, C++, & Java www.tangiblesoftwaresolutions.com Instant C# - VB to C# Converter Instant VB - C# to VB Converter
-
hi, I'm developing a web application for my graduation project and it's my first time to use asp.net and C# , the problem is I have an essential part of a code written in C and I need it to be converted or embedded in my project. I found a couple of converters but they all say that they are not reliable, another problem is that the C code has LOTS of pointers and I'm afraid that I'll mess things up.. is there a reliable converter or a reliable way ? or anyone who can compare both and make sure that they are right ? Thanks
Conversion isn't usually that difficult once you get underway. Pointers are fairly straight forward. In C# all classes are 'reference' types, meaning that a pointer (actually a copy of the pointer) is passed, not the value - so these are often directly interchangeable with C code when translating. structs are 'value' types so the value is copied rather than the reference. This can be changed by using
out
orref
to retrieve or pass a pointer. Only if there is pointer arithmetic involved or pointers to pointers do matters get more complicated. Much of the time, by carefull examination of the code and refactoring it into smaller sections, you will find you can get rid of the pointers altogether :) Good luck, and if you get stuck with any individual parts feel free to post.Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
How can I use it ? I'm new to C# and can I send and retrieve variables ?
-
hi, I'm developing a web application for my graduation project and it's my first time to use asp.net and C# , the problem is I have an essential part of a code written in C and I need it to be converted or embedded in my project. I found a couple of converters but they all say that they are not reliable, another problem is that the C code has LOTS of pointers and I'm afraid that I'll mess things up.. is there a reliable converter or a reliable way ? or anyone who can compare both and make sure that they are right ? Thanks
Use
DllImport
to get your C functions into your C# project. If you actually wrote C++ objects, you can create a wrapper for them in managed C++, and the managed C++ dll can be called from you C# code. -
AseelHadlaq wrote:
How can I use it ?
By learning how to use it of course. You can google for information on it.
Yea I noticed it was a stupid question, I read about it and I found it a bit confusing and I didnt get the concept of it, can you give me a link where it is explained in a clear way ? and can I send and retrieve variables through interop ? thank you
-
All converters will offer a free trial/demo version. Try it out and compare the results with some common sense. Even if the conversion is less than 100%, it may give you something you would have missed. The main challenge of converting C/C++ code to other languages, especially with pointers, is that a C/C++ declaration alone does not tell you how something is used. This is in contrast to C# where the declaration does tell you all you need to know for a translation to other languages.
David Anton Convert between VB, C#, C++, & Java www.tangiblesoftwaresolutions.com Instant C# - VB to C# Converter Instant VB - C# to VB Converter
thnx I found it earlier but it's not free and I dont know about its reliability , I guess I have to try it
-
Conversion isn't usually that difficult once you get underway. Pointers are fairly straight forward. In C# all classes are 'reference' types, meaning that a pointer (actually a copy of the pointer) is passed, not the value - so these are often directly interchangeable with C code when translating. structs are 'value' types so the value is copied rather than the reference. This can be changed by using
out
orref
to retrieve or pass a pointer. Only if there is pointer arithmetic involved or pointers to pointers do matters get more complicated. Much of the time, by carefull examination of the code and refactoring it into smaller sections, you will find you can get rid of the pointers altogether :) Good luck, and if you get stuck with any individual parts feel free to post.Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)That is scary a bit but I have to try ,, thank you
-
Use
DllImport
to get your C functions into your C# project. If you actually wrote C++ objects, you can create a wrapper for them in managed C++, and the managed C++ dll can be called from you C# code.My questions will sound stupid I know :^) But can I send and retrieve variables ? without changing my C code ? can you explain more about it plzz
-
My questions will sound stupid I know :^) But can I send and retrieve variables ? without changing my C code ? can you explain more about it plzz
You might want to start here[^].
AseelHadlaq wrote:
But can I send and retrieve variables ? without changing my C code ?
That really depends on how your C is written. In 10 years of working with .NET, I've never found a case where I couldn't P/Invoke because of variables, but that's not to say that some weird and wacky pointer manipulation couldn't cause an issue (although I do recall dealing with a particularly tricky void** parameter that caused some real headaches).
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
Yea I noticed it was a stupid question, I read about it and I found it a bit confusing and I didnt get the concept of it, can you give me a link where it is explained in a clear way ? and can I send and retrieve variables through interop ? thank you
I can't figure out for you if it is clear. You can use the following in google to search for links that would most likely be of help to you.
"C#" interop example -com
This is dependent on you knowing a non-trivial amount of C and having some non-trivial experience in writing code. If you have neither then this is going to be a difficult task. Especially since if you mess it up the most likely outcome will be a system exception and the C# VM will exit will little information.