简体   繁体   中英

Use .NET in an C++/MFC applications?

The program we manage and develop is written in C++/MFC. We want to modernize it to .NET, but do not want to rewrite the entire application at once. Therefore, we are thinking about writing new parts of the program in .NET, while maintaining the rest of the program in C++/MFC.

When we read about this on Internet it says a lot about calling unmanaged code from managed code. But we want to do it the other way. We've found some information that it is possible, but it seem very tedious and cumbersome.

Is that possible? Is it a good solution? Are there better ways to incrementally modernize the program?

It's a common way to use C++/CLI to use the .NET parts from C++/MFC code. It isn't that hard when you get to know it. .NET UI controls should have a methods to set the HWND of parent window, and the size of the control (not all of them have though). Another thing that many .NET components struggle with, is the obfuscation of the .NET assemblies that prevent using the controls from C++/CLI.

We are a .net chart manufacturer, and customers have used our products to modernize their apps. We've got documentation and example apps of how to use .NET charts from C++/CLI.

#using "Arction.LightningChartUltimate.dll"
using namespace Arction::LightningChartUltimate;

//"Global managed variables"  
ref class GlobalObjects {
public:
   static Arction::LightningChartUltimate::LightningChartUltimate^ chart; 
}; 

void CMainFrame::CreateChart(HWND hwndParent)
{

    GlobalObjects^ go = gcnew GlobalObjects();

    //Embeddable trial key is used here. 
    go->chart = gcnew LightningChartUltimate("licensekey"); 

    LightningChartUltimate^ chart = go->chart;

    //Disable repaints for every property change
    chart->BeginUpdate(); 

    //Set parent window by window handle
    chart->SetParentWindow((System::IntPtr) hwndParent); 

    //Remove existing series 
    chart->ViewXY->PointLineSeries->Clear(); 

    //Create new series 
    PointLineSeries^ series = gcnew PointLineSeries(chart->ViewXY, chart->ViewXY->XAxes[0], chart->ViewXY->YAxes[0]);
    const int PointCount = 10; 

    //Create SeriesPoint array
    array<SeriesPoint> ^ data =
        gcnew array<SeriesPoint>(PointCount);

    //Fill the array
    double yValues[PointCount] = {0.0, 5.0, 4.0, 3.0, 8.0, 10.0, 9.0, 8.0, 3.0, 2.0};
    for(int i=0; i<PointCount; i++)
    {
        data[i].X = i+1; 
        data[i].Y = yValues[i]; 
    }

    //Add the points to series
    series->AddPoints(data,false);

    //Add the series itself into chart's PointLineSeries collection
    chart->ViewXY->PointLineSeries->Add(series); 

    //Fit the axis ranges to data assigned
    chart->ViewXY->FitView();

    //Allow repaints, update chart
    chart->EndUpdate();  

} 

void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
    __super::OnSize(nType, cx, cy);

    RECT rectStatusBar;
    if (m_wndStatusBar.m_hWnd)
        m_wndStatusBar.GetClientRect(&rectStatusBar);

    //Set the new size to LightningChart object, with SetBounds method call
    GlobalObjects^ go = gcnew GlobalObjects();
    if (go)
    {
        LightningChartUltimate^ chart = go->chart;
        if (chart)
            chart->SetBounds(2, 2, cx - 4, cy - rectStatusBar.bottom - 2);
    }
}

It isn't harder than that, pretty much the same amount of code is needed than from C#.

Another approach is to make new DLLs with in C# (WinForms or WPF), and export a simple API for the C++ app, which minimizes the C++/CLI usage in your unmanaged side.

(I'm the CTO of LightningChart components.)

The easiest way for your to archive this would be using COM+ to host your C# and then from C++ add those COM+ as references. It is a simple procedure which also adds to a few benefits like code encapsulation.

http://my.execpc.com/~gopalan/dotnet/complus/complus.net_accountmanager.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM