none
Calling functions in a managed C# DLL from a unmanaged C++ MFC appication running on WEC7 RRS feed

  • Question

  • I am trying to use a C# library written for MQTT which is written with managed C# code. What I have is legacy unmanaged C++ code that I need to make calls into the C# DLL. I believe I need to create a wrapper that exposes the functions in the DLL. I am not having luck finding examples on this process. I am hoping somebody out there has done this. Looking forward for some guidance

    Thanks!

    Tuesday, August 20, 2019 3:55 PM

All replies

  • I don't know what you mean with "WEC7", can only speak for Windows versions 7 and higher.
    you could either:
    1. compile the MFC project with /CLR, add the C# assembly as reference and directly call the methods:
    in my opinion the easiest way
    2. create a mixed mode dll (again comiled with /CLR) that wraps the C# assembly and is called from the MFC application like a native dll:
    this requires one more project, but might be needed when you can't recompile the MFC app
    I have only used this in the other direction (so calling native code from C#)
    3. export COM interfaces from the C# assembly and call from the MFC application via COM interop:
    preferred when you are already using COM in the MFC app and don't want to change too much

    I have used the first approach successfully for some years.
    This can get tricky when you mix WPF, Windows Forms and MFC controls.


    Tuesday, August 20, 2019 5:40 PM
  • WEC7 is windows embedded version 7. Not a  PC version.

    I think I need to use your option 2 as I cannot change the project for the legacy code to a /CLR one. I am OK with an additional project just need guidance as to how to do it. For example, I would nee to make this call from my C++ code:

    // create client instance

    MqttClient client = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS));

    In the C# DLL, that code looks like this:

    public MqttClient(IPAddress brokerIpAddress) : this(brokerIpAddress, MqttSettings.MQTT_BROKER_DEFAULT_PORT, false, null, null, MqttSslProtocols.None)
    {

    ...       

    }

    So I need to understand how to 'wrap' that constructor as an example. Thoughts? I appreciate the response.

    Tuesday, August 20, 2019 8:09 PM
  • perhaps something like:
    using namespace System::Net;
    using namespace System;
    using namespace uPLibrary::Networking::M2Mqtt;
    void CMFCApplication1Dlg::Open(const CString& address) { auto client = gcnew MqttClient(IPAddress::Parse(gcnew String(address))); }

    but I wonder if it wouldn't be easier to use a native implementation?
    f.e. Eclipse Paho MQTT C client
    • Edited by EckiS Tuesday, August 20, 2019 10:03 PM
    Tuesday, August 20, 2019 10:02 PM
  • Hi Jakester2261,

    Thank you for posting here.

    You could look at the following link to call functions in a c# dll from a unmanaged c++ MFC app.

    Calling C# .NET methods from unmanaged C/C++ code

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 21, 2019 1:16 AM
    Moderator
  • 2. create a mixed mode dll (again comiled with /CLR) that wraps the C# assembly and is called from the MFC application like a native dll

    And that DLL can also be a MFC DLL, right?



    Sam Hobbs
    SimpleSamples.Info

    Wednesday, August 21, 2019 7:21 PM
  • yes, MFC dll's can also be mixed mode
    Wednesday, August 21, 2019 8:10 PM