Call .net in ActiveQt

226 Views Asked by At

For a project i need to call C# in Qt. It seems that the way to do it is by using COM and ActiveQt.

So i have this very basic C# (.Net Framework 4.8, "Register for COM interop enabled", visual studio launched in admin to register properly)

using System.Runtime.InteropServices;

[ComVisible( true )]
[Guid( "DB1797F5-7198-4411-8563-D05F4E904956" )]
[InterfaceType( ComInterfaceType.InterfaceIsIUnknown )]
public interface ISuperTest
{
    double Sum( int a, int b );
}

[ComVisible( true )]
[Guid( "BA9AC84B-C7FC-41CF-8B2F-1764EB773D4B" )]
public class SuperTest : ISuperTest
{
    double ISuperTest.Sum( int a , int b )
    {
        return a + b;
    }
}

I generate the .cpp/.h class by using B:\Qt\6.2.4\mingw_64\bin\dumpcpp.exe SuperTestCOM.tlb and include the resulting .cpp/.h it in my Qt project

#include <QAxObject>
#include <QApplication>
#include "supertestcom.h"


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    qInfo() << "Hello World !";


    auto *test = new SuperTestCOM::SuperTest();

    int ret = test->Sum(1, 2);
    qInfo() << ret;

    return a.exec();
}

It compiles perfectly, but when launching i still get this error.

Hello World !
QAxBase::setControl: requested control {ba9ac84b-c7fc-41cf-8b2f-1764eb773d4b} could not be instantiated
QAxBase::qt_metacall: Object is not initialized, or initialization failed
0

I tried to manualy register the SuperTestCOM.dll with C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe SuperTestCOM.dll it succeded.

And B:\Qt\6.2.4\mingw_64\bin\dumpcpp.exe -getfile {BA9AC84B-C7FC-41CF-8B2F-1764EB773D4B} doesn't return anything.

Edit: The C# was set as "Any CPU", after setting it to x64 i don't get the initialization errors anymore.

But test->Sum(1, 2) returns 0

After moving the dll somewhere else and launching again, i got the same result, so i guess i'm getting some default value and the .dll is not really loaded. What am i missing ?

Thanks in advance for your answers.

0

There are 0 best solutions below