none
C++/C# DLL - Call functions - procedure RRS feed

  • Question

  • C++/C# DLL - Call functions - procedure

    Hello,

    I have a C++ MFC application with CString and so one.

    Now I have to call from C++ functions of a C# and a C++/CLI DLL.

    How can I achieve this? Are there any examples of what the procedure is?

    int GetArticleNo(string name);
    string GetArticleName(int no);
    string[] GetArticleList(string groupName);
    double GetArticlePrice(string name);


    Thanks in advance.

    Best Regards Markus

    Saturday, May 4, 2019 10:23 AM

Answers

  • Hello RLWA32,

    works really well. Thanks a lot.

    One  question more. When I debug, the stops are not displayed. Do I need special debug settings?

    Thanks for your feedback.

    Best regards, Markustest looks good


    Set VS to use Mixed mode debugging -

    But note that when a native C++ application is built with /clr (i.e., CPPNoWrap) it is not necessary to use a C++/CLI wrapper dll to call managed code in a C# class library.  That is what EckiS was referring to.

    Monday, May 13, 2019 11:05 AM

All replies

  • switch the project settings of the MFC app to compile to mixed mode, and you can directly call code in managed assemblies: How to: Compile MFC and ATL Code By Using /clr
    Saturday, May 4, 2019 10:31 AM
  • ok, but I must call from managed C++ Code a function from C# or C++/CLI DLL.

    I need a wrapper. Do you know a step by step instruction or YouTube tutorial?

    Thanks in advance.

    Greetings Markus

    Saturday, May 4, 2019 10:57 AM
  • you don't need a wrapper when you are calling managed code from managed C++.
    You add the assembly reference to the project, then gcnew the managed objects and call the methods.
    Saturday, May 4, 2019 11:21 AM
  • Hello EckiS,

    My main application is C ++ and from this I have to call functions from a C # DLL.

    Now I am looking for a way how this works?

    For a good example, I would be very grateful.

    MainApp.EXE(MFC, C++) ------ WrapperC++/CLI ------- C#DLL

                                                                                         C++/CLI DLL

    Thanks in advance.

    Greetings Markus


    Saturday, May 4, 2019 3:56 PM
  • Hi Markus Freitag,

    Thank you for posting here.

    For you question, you could refer to the examples I done before.

    https://social.msdn.microsoft.com/Forums/office/en-US/dbafec35-0759-4dde-9048-d1c0c80a2ffb/use-managed-c-class-library-with-c?forum=netfxbcl

    Best Regards,

    Wendy


    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.

    Monday, May 6, 2019 5:36 AM
    Moderator
  • Just as a bit of background, the problem here is that managed code expects to be run under the control of the Common Language Runtime.  When you start a managed app, you don't actually run your code.  Instead, it starts into the CLR, which expects to be in full control of the environment.  It gets everything set up, and then starts interpreting the code in your app.  It's easy for it to call outside functions, but when a native app wants to call a managed DLL, the CLR is not present, and the DLL doesn't know to start it.

    The easy way to do it is to make your C# code into a COM object.  Once you do that, you can use regasm to build a type library (TLB), which can by #imported into your C++ code and called like a COM object.  Here's an example.

    https://www.codeproject.com/Questions/476583/callplusc-23plusdllplusorplusclasspluswithinplusc

    If you can't make it into a COM object, then it's not really practical.  It is POSSIBLE for your C++ code to start the CLR, and then use CLR calls to poke into your DLL to find the functions to call, but the code is quite complicated.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Monday, May 6, 2019 8:19 PM
  • @tim roberts: what is complicated about compiling the MFC app with /cli?
    We host Windows Forms controls in an old MFC app the way it is described in Using a Windows Form User Control in MFC
    Monday, May 6, 2019 8:53 PM

  • For you question, you could refer to the examples I done before.

    https://social.msdn.microsoft.com/Forums/office/en-US/dbafec35-0759-4dde-9048-d1c0c80a2ffb/use-managed-c-class-library-with-c?forum=netfxbcl


    Hello Wendy,

    Thanks for your response. Yes it is difficult.

    I will check the Posting from Tim and EckiS.

    You implemented in C# a C++ dll.

    I need the other direction.

    @EckiS,

    Do you have a sample project?

    Regards Markus



    Wednesday, May 8, 2019 4:55 PM
  • I linked to an MSDN article which should be enough to create a project yourself.
    Wednesday, May 8, 2019 5:14 PM
  • Hello,

    // C# DLL
    // New Project -> Visual C# -> Class Library" selected
    // Create me a "Calculator.dll"
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace CSharpCalculate
    {
    public class Calc 
    { 
    public int Add(int a, int b){return a + b;} 
    public int Sub(int a, int b){return a - b;} 
    public int Mult(int a, int b){return a * b;} 
    public int Div(int a, int b){return a / b;} 
    public string Div(double a, int b) {return a / b;}; 
    public  string Div(double a, double b) {return a / b;}; 
    public  decimal Sum(double a, double b) {return (decimal)(a/b);}; 
    } 
    }
    
    
    //"Add Project -> Visual C++ -> CLR -> Class Library"
    // Use the  "Calculator.dll"
    // Interface (Wrapper) to use my C# functions.
    // Create the "CalculatorWrapper.dll"
    
    // Header File
    namespace CSharpCalculateWrapper
    {
    class CSharpCalculate_API CalcWrapper
    {
    public: 
    int Add(int a, int b); 
    int Sub(int a, int b); 
    int Mult(int a, int b); 
    int Div(int a, int b); 
    //  string Div(double a, int b); 
    //  string Div(double a, double b); 
    //  decimal Sum(double a, double b); 
    };
    
    // CPP File
    
    #include "CSharpCalculateWrapper.h"
    
    using namespace System; //weiß nicht genau wofür man System braucht
    using namespace CSharpCalculateWrapper;
    using namespace CSharpCalculate;
    
    int CalcWrapper::Add(int a, int b)
    {
    Calc^ c = gcnew Calc(); 
    return c->Add(a, b); 
    }
    
    int CalcWrapper::Sub(int a, int b)
    {
    Calc^ c = gcnew Calc(); 
    return c->Sub(a, b); 
    }
    
    int CalcWrapper::Mult(int a, int b)
    {
    Calc^ c = gcnew Calc(); 
    return c->Mult(a, b); 
    }
    
    int CalcWrapper::Div(int a, int b)
    {
    Calc^ c = gcnew Calc(); 
    return c->Div(a, b); 
    }
    
    
    // ---
    #include <iostream>
    #include "CSharpCalculateWrapper.h"
    
    using namespace CSharpCalculateWrapper;
    
    int main()
    {
    CalcWrapper* cW = new CalcWrapper(); 
    int result = cW->Add(1, 2); 
    
    std::cout << "Result: " << result << std::endl; 
    
    system("pause"); 
    delete cW; 
    return 0; 
    }
    
    //Error:
    //3>CppNativeCalculateTest.obj : error LNK2019: unresolved external symbol
    //"__declspec(dllimport) public: int __thiscall
    //CSharpCalculateWrapper::CalcWrapper::Add(int,int)"
    //(__imp_?Add@CalcWrapper@CSharpCalculateWrapper@@QAEHHH@Z) referenced in
    //function _main
     
    Maybe someone knows a good short guide. Thanks in advance for tips.
     


    Saturday, May 11, 2019 10:38 AM
  • so your problems is how to use a C++ dll in a C++ exe project.
    And you ask this in the "Visual C#" forum...
    Saturday, May 11, 2019 11:41 AM
  • I put together a small demo for you.  The VS2015 solution contains 4 projects -

    1) A C# class library -- CSharpCalculate

    2) A native C++ console application built with /clr (Common Language Runtime Support) that can directly use the C# class library -- CPPNoWrap

    3) A C++/CLI dll that wraps a couple of functions from the C# class library -- CSharpCalcWrapper

    4) A native C++ console application without /clr that uses the C++/CLI wrapper -- CPPWithWrapper

    You can get it from onedrive at MFSample.zip


    • Edited by RLWA32 Saturday, May 11, 2019 2:58 PM
    Saturday, May 11, 2019 2:58 PM
  • Hello RLWA32,

    works really well. Thanks a lot.

    One  question more. When I debug, the stops are not displayed. Do I need special debug settings?

    Thanks for your feedback.

    Best regards, Markustest looks good


    Monday, May 13, 2019 10:24 AM
  • Hello RLWA32,

    works really well. Thanks a lot.

    One  question more. When I debug, the stops are not displayed. Do I need special debug settings?

    Thanks for your feedback.

    Best regards, Markustest looks good


    Set VS to use Mixed mode debugging -

    But note that when a native C++ application is built with /clr (i.e., CPPNoWrap) it is not necessary to use a C++/CLI wrapper dll to call managed code in a C# class library.  That is what EckiS was referring to.

    Monday, May 13, 2019 11:05 AM