Click here to Skip to main content
Licence CPOL
First Posted 11 Jan 2008
Views 15,680
Downloads 116
Bookmarked 16 times

Porting from C++ to C#: Huo Chess Example

By | 11 Jan 2008 | Article
An example of how to move from C++ to C# with minimal effort
Example

Why Read This

This article is a very small and elementary example of how to port (transfer) a C++ application to C#. It is based on the porting of Huo Chess from C++ to C# and may have no application to other more complicated projects. For example, it does not cover any changes you may need to make in projects that involve databases. The article does not cover the differences between the two languages in depth, nor any advanced features of programming - at least not yet…

What I Did

I recently tried to transfer a micro chess program I developed in C++/CLI to C#. The main reason behind my decision was the increasing importance that C# is gaining in the software development world. As I found out, porting the program to C# was very easy, since even though C# is a new language with many interesting features, the syntax differences with C++ are minimal. I recorded all my actions during the porting process and I present them here.

General Algorithm for Porting from C++ to C#

A general list of actions to perform in order to successfully port a C++ application to C# is the following:

  1. Create a new C# project in Visual Studio.

  2. Copy the C++ code into the new project.

  3. Build the solution. As a result, many errors will appear.

  4. Add any namespaces required for your program to run [see below for details].

  5. Fix functions declarations [see below for details].

  6. Replace C++ keywords with the respective C# keywords [see below for details].

  7. Fix the variables' declarations in your program [see below for details].

Please note: as I already mentioned in the beginning of the article, the above list is not exhaustive. It is merely an attempt to identify the main actions one should take so as to move to the C# environment with minimal effort. The list is based on the porting of Huo Chess from C++ to C# and may have no application to other more complicated projects.

An Example: Porting of Huo Chess

In the following lines, I list the actions I performed to port Huo Chess to C#. The C++ source code, along with the C# source code, is included in the distributable files above. If you want to read details about the chess program and how it works, go to its C++/CLI page at The Code Project. The actions are as follows:

  1. I replaced the C++ operators with the ‘.’ which is used in C#

    • REPLACE :: with .

    • REPLACE -> with .

  2. I added the following namespaces: System.IO, which the program uses in order to read from the opening book

    • Errors I ended up with: 136

  3. I changed the variables' declarations

    • Arrays: static array<int, 2>^ Number_of_defenders = gcnew array<int,2>(8,8); has been changed to int[,] Number_of_defenders = new int[8,8];

    • String variables: replaced String^ with String

    • Errors I ended up with: 56

  4. I fixed the functions declarations

    • Changed arrays used in functions as parameters: bool CheckForBlackCheck(array<String^, 2>^ BCSkakiera) has been changed to bool CheckForBlackCheck(string[,] BCSkakiera)

    • void Enter_move(void) has been changed to void Enter_move() [i.e. REPLACE (void) => () ]

    • Errors I ended up with: 10

  5. I fixed some remaining errors

    • String^ has been changed to String inside various lines of code in the program

    • Specific problem with Move variable declared in line 370

    • Fixed move randomizer (line 1904): removed the ^ character (delete ^) and replaced gcnew with new

    • Fixed Streamwriter/reader stuff (line 2036 etc): removed the ^ character and replaced gcnew with new.

    • Fixed catch exceptions' lines: replaced catch(Exception^) with catch(Exception ex)

    • Errors I ended up with: 0

  6. I renamed the Game() function to static void Main(string[] args)

    • Errors: 31

Next steps:

  • Fix the declarations of the variables to public static so that the variables can be accessed by the Main() function's code lines

  • Remove this keyword: this.Starting_position(); becomes Starting_position();

  • Add to the functions' declarations the public static keyword in the beginning so that they can be accessed by other functions

Warnings: 11

Final steps:

  • Assign initial values to variables

Warnings: 0

Errors: 0

Successful build! Will be back with more examples soon…

Epilogue

As all Greeks, I like philosophy and would like to make a last argument for the world of programming in general: programming is mainly designing the application, thinking of the program’s algorithm and creating a good user interface so that the software is easy to use. Writing the code can be tiring and difficult (even hard), but this should be the last thing one should be concerned with. If an application is correctly planned, structured and its code has good comments in it, then moving to any language should not be an impossible thing to do. When a new language emerges, just read the... manual and start writing code! Just as simple as that.

History

  • 11 January, 2008 -- Original version posted

License

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

About the Author

Palavos

Software Developer
Kakos Bros Solutions
Greece Greece

Member

Spiros [Spyridon or Spyros are also used] Kakos (huo) lives in Athens, Greece. He is currently working as an IT consultant in a large firm. Begun programming during the Commodore era in MS Basic and is still trying to learn (mostly in C++ and C#)...
He likes chess and has recently bought a new (old) modem for one of his Commodores 128 (yes, he has two of them!) to set up a server based on 8-bit technology. He thinks that when the World Wide Web crashes completely by an alien cyber attack, he will be the only one capable of surfing with his Commodore computer and will eventually save the day...
He likes reading and writting philosophy and is a fond admirer of Aristotle and Alfred Russel Wallace. His main heritage is Harmonia Philosophica.
At his free time he is researching the application of polypyrrole (PPy) in the PCB manufacturing process (through-hole plating) at the National Technical University of Athens - Advanced Materials section.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralPerformance PinmemberAdam Berent7:06 14 Apr '09  
GeneralRe: Performance PinmemberPalavos23:21 14 Apr '09  
GeneralRe: Performance PinmemberDmitriy Maksimov13:14 28 May '09  
Generalthat's not a big deal Pinmemberdmihailescu7:14 14 Jan '08  
GeneralRe: that's not a big deal PinmemberPalavos21:24 14 Jan '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 11 Jan 2008
Article Copyright 2008 by Palavos
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid