Important: Please read the Qt Code of Conduct -

Import Qt dll in C#

  • Hi everybody,

    I'm getting stucked trying to import and use a Qt class, deployed as dll library, in a Visual Studio C# solution.
    I've googled a lot, and found many different possible advices on how to "solve" this issue: who tells to register dll as COM, who tells to make a C# wrapper, who to call dllimport, and many other.
    Also Qt "official" tutorial seems to be quite old (still using MC++).
    In all C# solution tested, also the simplest one with just one form and a button, still getting error:

    "A reference to [dll_path] could not be added. Please make sure that the file is accessible, and that is a valid assembly or COM component"

    Many thanks

  • @Andrea
    Unless an expert here knows different and offers you a solution example, that message is telling you that you do indeed need either a native .NET assembly or a COM component and you will need to try the Googles you've found for one that works....

  • Hi Andrea. This is a method exported by a Qt dll

    extern "C" DLLTESTSHARED_EXPORT int Somma(DllTest *&dlltest, int a, int b);

    and this is the way to have it in a c# source

    [DllImport(@"C:\Cpp\dllTest\dllTest-build-desktop\release\dllTest.dll", CallingConvention = CallingConvention.Cdecl)]
            public static extern int Somma(ref IntPtr ptr, int a, int b);

  • @mrdebug Thanks for your reply. I've seen how to "export" a method, but is possible doing something similar for unmanaged classes?

    I know that I should create a C++/CLI wrapper, but my question is: if I'm not able to import the DLL, and therefore the class, how can I wrap it?

  • Lifetime Qt Champion

    In older times it was not possible to export a true class from c++ to c#.
    Newer visual studio might be able to tricks
    i dont know about to make work, so take this with a grain of salt.

    However, back then you would wrap the class and use a function based
    proxy-like design to create and use
    classes in the dll and not directly in c#.
    Like here
    the PInvoke version.

  • @mrdebug said in Import Qt dll in C#:

    This is the right answer.

Log in to reply