1

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.

Florian
  • 51
  • 5
  • The .NET side seems ok, but you want to use `...\regasm yourdll /codebase` to make sure the dll path is written in the registry. Is your qt .exe compiled as x64? Can you call the object using plain C/C++? – Simon Mourier Apr 11 '22 at 07:37
  • Oh i made some progress, i noticed that in visual studio i used "any cpu". After setting it to x64, rebuilding and re-registering it i dont get the error anymore. But, sum(1, 2) is returning 0 instead of 3. – Florian Apr 11 '22 at 08:47
  • I tested moving the dll somewhere else and launch again, same result. So i guess that i'm getting some kind of default value and the dll is not really loaded. I also tried /codebase, and did a research in the registry, the right path is defined in multiples places Also yes, Qt is using "Desktop Qt 6.2.4 MingW 64 Bit" – Florian Apr 11 '22 at 09:02
  • Sum "working" but returning 0 is very unexpected. Not sure what you're testing in the end. You can use the ProcMon tool from sysinternals to see what registry keys and what files are accessed. And you should try w/o Qt, with plain C++. – Simon Mourier Apr 11 '22 at 09:19
  • Hmmm it's very strange Before trying the plain c++ way, i tried something else: I added a new function that returns a string. I also changed the InterfaceType to InterfaceIsDual and added `[ClassInterface( ClassInterfaceType.None )]` to the SuperTest class. And it works, it prints the string perfectly, but only the function that returns a string works (even with parameters). And just to be sure i renamed Sum to another name and changed the return type to int. But still 0, even when i return a constant number. This is so strange – Florian Apr 11 '22 at 13:10
  • Now it works, just changing back and forth the functions name made it work. I really dont understand what is happening, this is so weird. Anyway, thanks a lot for your help ! – Florian Apr 11 '22 at 13:23
  • Quick update: I think dumpcpp is broken, i analysed the generated cpp, and in some reproductible cases the indexes given to`qt_metacall` are wrong. If i change them to the right index and compile the cpp it works. I will make a message on the Qt forums. – Florian Apr 11 '22 at 14:11

0 Answers0