How to use C# objects returned in QT.

qiangang 40 Reputation points
2023-03-02T06:00:21.27+00:00

Hello everyone, Coming from .NET environment [VB & C#] with a beginner level in C++ and Qt and i was wondering is it possible to call methods from C# dll files in my Qt project.dll example:

namespace Calc;
public class Program{
      public int sum(int x,int y){
         return x+y;
      }
}

I want to use the sum sum method in my Qt C++ project. And in my Qt project would be like this

Program c;
c.sum(2+3);
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,249 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,489 questions
{count} votes

Accepted answer
  1. RLWA32 40,286 Reputation points
    2023-03-05T13:43:51.4766667+00:00

    In the event that Qt does not work well building with /clr you can refer to the following example.

    C# class library -

    using System;
    
    namespace CSLibrary
    {
        public class Class1
        {
            public int Add(int a, int b)
            {
                return a + b;
            }
    
            public int Subtract(int a, int b)
            {
                return a - b;
            }
        }
    }
    

    C++/CLI class library that wraps CSLibrary.dll -

    #pragma once
    
    #include <msclr\gcroot.h>
    
    using namespace System;
    #using <CSLibrary.dll>
    
    namespace CppWrapLibrary {
        class CppWrap
        {
        public:
            virtual int Add(int a, int b) = 0;
    
            virtual int Subtract(int a, int b) = 0;
    
            virtual ~CppWrap() = default;
        };
    
        class CppWrapImpl : public CppWrap
        {
        public:
            virtual int Add(int a, int b)
            {
                return m_CSref->Add(a, b);
            }
    
            virtual int Subtract(int a, int b)
            {
                return m_CSref->Subtract(a, b);
            }
    
            CppWrapImpl() : m_CSref(gcnew CSLibrary::Class1())
            {
            }
    
            // Included for debugging breakpoint instead of using compiler generated default destructor
            virtual ~CppWrapImpl()
            {
            };
    
        private:
            // m_CSref holds a reference to the C# class library.
            msclr::gcroot<CSLibrary::Class1^> m_CSref;
        };
    };
    
    
    // return a pointer to base class CppWrap.  CppWrapImpl cannot be declared in unmanaged code
    __declspec(dllexport) CppWrapLibrary::CppWrap* CreateWrapper()
    {
        return static_cast<CppWrapLibrary::CppWrap*>(new CppWrapLibrary::CppWrapImpl);
    }
    
    

    C++ console application that uses the C++/CLI DLL -

    // CppConsole.cpp : This file contains the 'main' function. Program execution begins and ends there.
    //
    
    #include <iostream>
    
    namespace CppWrapLibrary {
        class CppWrap
        {
        public:
            virtual int Add(int a, int b) = 0;
    
            virtual int Subtract(int a, int b) = 0;
    
            virtual ~CppWrap() = default;
        };
    };
    
    __declspec(dllimport) CppWrapLibrary::CppWrap* CreateWrapper();
    
    using namespace CppWrapLibrary;
    
    int main()
    {
        CppWrap* pWrapper = CreateWrapper();
        if (pWrapper)
        {
            int x = pWrapper->Add(10, 10);
            int y = pWrapper->Subtract(100, 58);
    
            std::cout << "10 + 10 = " << x << "\n100 - 58 = " << y << std::endl;
    
            delete pWrapper;
        }
    }
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. RLWA32 40,286 Reputation points
    2023-03-02T08:50:55.61+00:00

    There are two general approaches to consuming managed code (C# class library) from unmanaged C++ code. The first approach is to create a class library using C++/CLI. This library serves as the intermediary between C++ and C#. An old but instructive example of this approach can be seen at Calling C# .NET methods from unmanaged C/C++. The second approach is for your C# class library to expose its objects and methods through COM.