Converting the C ++ / CLI of an existing application makes managed code

I have an existing application written in Borland C ++ Builder. My client wants it to be rewritten in C # / WPF. This requires a lot of work and is a difficult task because I need to rewrite the entire (huge) algorithm. I was thinking of a way to reuse code and the ability to only create a graphical interface in C # / WPF. Is it possible? Would it be easy? How can I make C ++ classes visible to .NET?

If you could give me short answers with some links / examples, I would be grateful.

+4
source share
2 answers

You can wrap your old C ++ code in a C ++ / CLI shell and create it in a DLL file. Then it should be visible in any .NET project.

This may change slightly depending on your algorithm / function structure, but the main form and the way I did it are as follows. Keep in mind that this is a VERY simple example, but I tried to include key points:

1. CLI shell definition

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 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. Introduce a shell 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 a shell class and compile it into a class library, you can create a link to the .DLL file in a C # project, and C # should see the managed shell class and all its public functions.

One more note: if you create new managed objects in C ++, instead of "new" you use the keyword "gcnew". So the new line will be String ^ someString = gcnew String();

Finally, here are some links to some things about the CLI that may help:

CLI - Wikipedia
CLI - Draft Code
CLI - FunctionX

+4
source

Well, as far as I remember, you can use interops in this regard. In fact, itโ€™s better to continue to use code that has been tested and stable for a long time. With Net Interop Services, you can create a very good bridge between the new application 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

0
source

Source: https://habr.com/ru/post/1332795/


All Articles