Reputation: 7254
I've got an existing application written in Borland C++ Builder. My client wants it rewritten in C#/WPF. This requires a lot of work and is a complex task because I need to rewrite the whole (huge) algorithm. I was thinking of a way to reuse the code and to be able to create only GUI in C#/WPF. Is this possible? Would that be easy? How can I make C++ classes visible to .NET ?
If you could give me brief answers with some links/examples I would be grateful.
Upvotes: 4
Views: 2298
Reputation: 9013
You can wrap your old C++ code in a C++/CLI wrapper and build it into a DLL file. It should then be visible in any .NET project.
Depending on your algorithm/function structure, this may change a little, but the basic form, and the way I did this, is the following. Keep in mind this is a VERY basic example, but I tried to include the key points:
1. Define the CLI wrapper
using namespace System;
#include "myAlgorithmClass"
public ref class MyCLIWrapperClass //the 'ref' keyword specifies that this is a managed class
{
public:
MyCLIWrapperClass(); //Constructor
//function definitions are the same, though some types,
//like string, change a little. Here's an example:
String ^ GetAString(); //the "String" is the .NET "System.String" and the ^ is sort of a pointer for managed classes
//int, double, long, char, etc do not need to be specified as managed.
//.NET code will know how to handle these types.
//Here you want to define some functions in the wrapper that will call
//the functions from your un-managed class. Here are some examples:
//Say that your functions in the Algorithm class are int Func1, double Func2, and std::string Func3:
int Func1();
double Func2();
String ^ Func3();
private:
//All private functions and members here
myAlgorithmClass algor;
};
2. Implement the wrapper class
using namespace System;
#include <string> //For the string function ex. below
#include "myAlgorithmClass"
MyCLIWrapperClass::MyCLIWrapperClass()
{
algor = myAlgorithmClass(); //create instance of your un-managed class
}
int MyCLIWrapperClass::Func1()
{
return algor.Func1(); //Call the un-managed class function.
}
double MyCLIWrapperClass::Func2()
{
return algor.Func2(); //Call the un-managed class function.
}
String ^ MyCLIWrapperClass::Func3()
{
//Converting a std::string to a System.String requires some conversion
//but I'm sure you can find that somewhere on Google or here on SO
String ^ myString = "";
std::string myUnmanagedString = algor.Func3(); //Call the un-managed class function.
//Convert myUnmanagedString from std::string to System.String here
return myString;
}
After you write the wrapper class and compile it into a class library you can create a reference to the .DLL file in a C# project and C# should see the managed wrapper class and all its public functions.
Another thing to note is that if you are creating new managed objects in C++ you use the keyword "gcnew" instead of "new". So a new string would be String ^ someString = gcnew String();
Finally, here are some links to some things about CLI that may help:
CLI - Wikipedia
CLI - Code Project
CLI - FunctionX
Upvotes: 4
Reputation: 3257
Well, As far as i can remember by head, you can use interops for that matter. It is in fact better to continue using a code that was for a long time tested, and is stable. With net Interop Services you can create a very good bridge between the new app and the old one.
examples:
http://www.daniweb.com/forums/thread136041.html
http://msdn.microsoft.com/en-us/library/aa645736%28v=vs.71%29.aspx
http://www.codeguru.com/cpp/cpp/cpp_managed/interop/article.php/c6867
Upvotes: 0