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;
}
}