0

I work on a project that aims to create a GUI for a certain device, with the tlb file being provided by its manufacturer. The default / recommended way to go is WPF, but I have reasons to consider C++ (portability to non-Windows systems and personal experience).

I was able to get the tlb file to work well with VS2019 and C++ (with CLR support). The problem which I have now is how to interface this further with the Qt framework. Is there a way to configure Qt Creator to accept managed CLR classes? Or add Qt to a Visual Studio code that was not set as a "Qt Project" from scratch, and includes CLR support?

Consider the following C++ code (minimum example):

#include "pch.h"
#include <stdio.h>
#include <iostream>
#include <chrono>
#include <thread>

using namespace System;

public ref class Instrument {
public:
    TEMEXTLib::TEM3Class ^theScope;
    TEMEXTLib::IDef3^ theDef;

    Instrument() {
        theScope = gcnew TEMEXTLib::TEM3Class();
        theDef = theScope->CreateDef3();
        std::chrono::seconds timespan(2);
        std::this_thread::sleep_for(timespan);
    }

    void check() {
        int x;
        int y;
        theDef->GetCLA1(x, y);   
        std::cout << "X value is: " << x << " and Y value is : " << y;
    }
};

int main(array<System::String ^> ^args)
{
    Instrument ^machine = gcnew Instrument();
    machine->check();
    return 0;
}

Is there a way to have a Qt Application that would take these X and Y values in display them in QLabels?

I am a total noob when it comes to Qt-CLR/C++ problems, any suggestion that points in the right direction would be very helpful.

Edit: just to be super clear - this is not about using managed classes from a tlb file in CLR/C++, it's about using them in the C++ (original) version of the Qt framework. As far as I am aware, Qt Creator cannot run in the CLR mode, and valid Qt code in VS is no longer valid if CLR support is turned on. Is there a way to use both in the same time: Qt/C++ and tlb/CLR? Ideally a simple way - that does not involve running a dedicated .NET core host within the native code...

Bart M
  • 697
  • 1
  • 6
  • 18
  • Does this answer your question? [Calling C# code from C++](https://stackoverflow.com/questions/778590/calling-c-sharp-code-from-c) –  Jun 03 '21 at 12:52
  • The accepted answer will only work on windows, but there are cross-platform alternatives in the other answers. –  Jun 03 '21 at 12:52
  • Well, not really... (or maybe I don't read those answers correctly). But... the moment I turn on the CLR support in a Qt project in VS that was started as Qt for Desktop, all Qt functions, classes and imports instantly turn red. The issue is not using the tlb file with CLR/C++, it's about using tlb with Qt C++. – Bart M Jun 03 '21 at 14:03
  • that's kind of why I mentioned that the accepted answer is not appropriate. You can only do `/clr` on windows anyways. The whole point of the execersie is to be cross-platform, so that's not going to fly regardless. Froma glance, I'd say [this answer](https://stackoverflow.com/a/57534949/4442671) is on the right track –  Jun 03 '21 at 14:06

0 Answers0