Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
11 Jan 2008 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here