Featured post
c# - C++/CLI convert existing application do managed code -
i've got existing application written in borland c++ builder. client wants rewritten in c#/wpf. requires lot of work , complex task because need rewrite whole (huge) algorithm. thinking of way reuse code , able create gui in c#/wpf. possible? easy? how can make c++ classes visible .net ?
if give me brief answers links/examples grateful.
you can wrap old c++ code in c++/cli wrapper , build dll file. should visible in .net project.
depending on algorithm/function structure, may change little, basic form, , way did this, following. keep in mind basic example, tried include key points:
1. define cli wrapper
using namespace system; #include "myalgorithmclass" public ref class mycliwrapperclass //the 'ref' keyword specifies managed class { public: mycliwrapperclass(); //constructor //function definitions same, though types, //like string, change little. here's example: string ^ getastring(); //the "string" .net "system.string" , ^ sort of pointer managed classes //int, double, long, char, etc not need specified managed. //.net code know how handle these types. //here want define functions in wrapper call //the functions un-managed class. here examples: //say functions in algorithm class int func1, double func2, , std::string func3: int func1(); double func2(); string ^ func3(); private: //all private functions , members here myalgorithmclass algor; };
2. implement wrapper class
using namespace system; #include <string> //for string function ex. below #include "myalgorithmclass" mycliwrapperclass::mycliwrapperclass() { algor = myalgorithmclass(); //create instance of un-managed class } int mycliwrapperclass::func1() { return algor.func1(); //call un-managed class function. } double mycliwrapperclass::func2() { return algor.func2(); //call un-managed class function. } string ^ mycliwrapperclass::func3() { //converting std::string system.string requires conversion //but i'm sure can find somewhere on google or here on string ^ mystring = ""; std::string myunmanagedstring = algor.func3(); //call un-managed class function. //convert myunmanagedstring std::string system.string here return mystring; }
after write wrapper class , compile class library can create reference .dll file in c# project , c# should see managed wrapper class , public functions.
another thing note if creating new managed objects in c++ use keyword "gcnew" instead of "new". new string string ^ somestring = gcnew string();
finally, here links things cli may help:
- Get link
- X
- Other Apps
Comments
Post a Comment