What are my options? Calls from C++ C# DLL functions, how?

Markus Freitag 3,786 Reputation points
2021-07-10T06:49:34.41+00:00

Hello,

I have a C# DLL. I need to call these functions from C++ unmanaged application which is statically bound, no CLI is possible.

What are my options?
Maybe I don't need a CLI wrapper class. Maybe it is easier. Can you make sample for all three functions, please? Thanks in advance!

  using System;  
    using System.Collections.Generic;  
    using System.Text;  
    using System.Windows.Forms;  
    using System.IO;  
      
    namespace ManagedCSharp  
    {  
        public static class ManagedClass  
        {  
            static List<string> ListSerials;  
            static Dictionary<int, string> DicPosSerial;  
      
            public static void ShowValue(ref int value)  
            {  
                DialogResult result = MessageBox.Show("C# Message Box", "C# Message Box", MessageBoxButtons.OKCancel);  
                if (result == DialogResult.OK)  
                {  
                    value = 1;  
                    ListSerials = new List<string>();  
                    DicPosSerial = new Dictionary<int, string>();  
                }  
                else  
                    value = 2;  
                return;  
            }  
      
            public static string GetOrder(string input)  
            {  
                return "[ORDER_RES]|2424245|0793|1000|2421Ad";  
            }  
      
            public static bool SendResult(int pos, string serial)  
            {  
                ListSerials.Add(serial);  
      
                DicPosSerial.Add(pos, serial);  
      
                return true;  
            }  
        }  
    }  

113542-cpp-exe-mfc-common-dll.png

.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
321 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,194 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,516 questions
{count} votes

Accepted answer
  1. RLWA32 40,021 Reputation points
    2021-07-11T08:55:55.99+00:00

    @Markus Freitag I put together a small sample using VS2019 that illustrates using a C++/CLI DLL to interoperate with a C# class library from unmanaged C++. It consists of a solution with 3 projects (C# class library, C++/CLI DLL and C++ console application). You can get it from https://1drv.ms/u/s!AmnqrCFBv4nDggHoFw90fA7As4eK?e=pXnIqv

    2 people found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Sam of Simple Samples 5,516 Reputation points
    2021-07-10T15:27:55.613+00:00

    In your first version of this question at C# to C++/MFC duplex mode - Concept I did not understand that the reason you think you cannot modify the MFC application is the static linking. See Managed C++ Wrapper For Unmanaged Code. Believe it or not, you can use C++/CLI in a C++ program that is statically linked. I wrote the original article from code I wrote to call Microsoft Detours, which is (was) only available as a static library. You can add C++/CLI to an existing C++ application such as a MFC application.

    1 person found this answer helpful.

  2. rupesh shukla 16 Reputation points
    2021-07-14T21:53:42.16+00:00

    You can get a small sample from

    call c# dll or class within c++ project

    Thanks

    1 person found this answer helpful.