Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Import Qt dll in C#
Forum Updated to NodeBB v4.3 + New Features

Import Qt dll in C#

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 5 Posters 5.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andrea
    wrote on last edited by
    #1

    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

    JonBJ 1 Reply Last reply
    0
    • A Andrea

      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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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....

      1 Reply Last reply
      0
      • mrdebugM Offline
        mrdebugM Offline
        mrdebug
        wrote on last edited by
        #3

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

        Need programmers to hire?
        www.labcsp.com
        www.denisgottardello.it
        GMT+1
        Skype: mrdebug

        PangolinP 1 Reply Last reply
        2
        • A Offline
          A Offline
          Andrea
          wrote on last edited by
          #4

          @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?

          mrjjM 1 Reply Last reply
          0
          • A Andrea

            @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?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            @Andrea
            Hi
            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
            https://stackoverflow.com/questions/315051/using-a-class-defined-in-a-c-dll-in-c-sharp-code
            the PInvoke version.

            1 Reply Last reply
            0
            • mrdebugM mrdebug

              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);
              
              PangolinP Offline
              PangolinP Offline
              Pangolin
              wrote on last edited by
              #6

              @mrdebug said in Import Qt dll in C#:

              DLL
              This is the right answer.

              Speak less and do more . You are your own worst enemy.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved