Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. DLLs written in C++ QT6 cannot be linked by C programs
QtWS25 Last Chance

DLLs written in C++ QT6 cannot be linked by C programs

Scheduled Pinned Locked Moved Solved Qt 6
6 Posts 3 Posters 489 Views
  • 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.
  • P Offline
    P Offline
    pedisChen
    wrote on 26 Oct 2024, 15:36 last edited by
    #1

    Cause: I wrote a DLL file that uses QT6's QTCPSocket component. This file can be linked and used by QLibrary without any problems, but it cannot be linked and used by LoadLibrary in a C program, which will result in an error. I want to know why, thanks.

    example code like this:

    C program that links DLL by LoadLibrary

    #include <windows.h>
    #include <stdio.h>
    int main() {
        HMODULE hModule = LoadLibrary("myDLL.dll");
        if (hModule) {
            printf("ok!");
            FreeLibrary(hModule);
        } else {
            DWORD error = GetLastError();
            printf("Could not load the DLL. Error: %lu\n", error);
        }
        return 0;
    }
    

    and Then, the err message like this

    PS D:\Code\CProgram>  & 'c:\Users\12037\.vscode\extensions\ms-vscode.cpptools-1.20.5-win32-x64\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-z0eabn4e.l2k' '--stdout=Microsoft-MIEngine-Out-a2ifctuv.t5z' '--stderr=Microsoft-MIEngine-Error-5vdihyxk.yrk' '--pid=Microsoft-MIEngine-Pid-i1z3tzaa.qen' '--dbgExe=C:\TDM-GCC-64\bin\gdb.exe' '--interpreter=mi' 
    Could not load the DLL. Error: 126
    

    Supplement:I have packaged all relevant DLLs using the windeployqt6.exe.

    1 Reply Last reply
    0
    • C Christian Ehrlicher
      26 Oct 2024, 18:30

      The error message is 'dll not found' - not some dependencies not found. Make sure the dll you want to load is in the current work dir.
      Also I don't see how this dll should work at all - where is your running Q(Core)Application?

      P Offline
      P Offline
      Pl45m4
      wrote on 26 Oct 2024, 18:37 last edited by Pl45m4
      #5

      @Christian-Ehrlicher said in DLLs written in C++ QT6 cannot be linked by C programs:

      Also I don't see how this dll should work at all - where is your running Q(Core)Application?

      I think it's related to this topic and the dll contains the complete Qt GUI program including the QApplication instance.
      (No idea if this works like this... never linked a Qt GUI app to a C program)

      @pedisChen try an absolute path to your DLL, if the only issue is that it can't locate it...
      Check your working directory (where your C program is executed in) and how this is relative to the directory where your DLL is.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      P 1 Reply Last reply 27 Oct 2024, 04:46
      1
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 26 Oct 2024, 16:06 last edited by
        #2

        Your dll is not found according the error number as you can see here: https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        P 1 Reply Last reply 26 Oct 2024, 18:07
        2
        • C Christian Ehrlicher
          26 Oct 2024, 16:06

          Your dll is not found according the error number as you can see here: https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-

          P Offline
          P Offline
          pedisChen
          wrote on 26 Oct 2024, 18:07 last edited by pedisChen
          #3

          @Christian-Ehrlicher thank you for your reply, so how can I solve this problem, I have already used windeployqt6.exe to ship all DLL dependency files to my c program project, I don't know which dll files I need to add.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 26 Oct 2024, 18:30 last edited by
            #4

            The error message is 'dll not found' - not some dependencies not found. Make sure the dll you want to load is in the current work dir.
            Also I don't see how this dll should work at all - where is your running Q(Core)Application?

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            P 1 Reply Last reply 26 Oct 2024, 18:37
            1
            • C Christian Ehrlicher
              26 Oct 2024, 18:30

              The error message is 'dll not found' - not some dependencies not found. Make sure the dll you want to load is in the current work dir.
              Also I don't see how this dll should work at all - where is your running Q(Core)Application?

              P Offline
              P Offline
              Pl45m4
              wrote on 26 Oct 2024, 18:37 last edited by Pl45m4
              #5

              @Christian-Ehrlicher said in DLLs written in C++ QT6 cannot be linked by C programs:

              Also I don't see how this dll should work at all - where is your running Q(Core)Application?

              I think it's related to this topic and the dll contains the complete Qt GUI program including the QApplication instance.
              (No idea if this works like this... never linked a Qt GUI app to a C program)

              @pedisChen try an absolute path to your DLL, if the only issue is that it can't locate it...
              Check your working directory (where your C program is executed in) and how this is relative to the directory where your DLL is.


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              P 1 Reply Last reply 27 Oct 2024, 04:46
              1
              • P Pl45m4
                26 Oct 2024, 18:37

                @Christian-Ehrlicher said in DLLs written in C++ QT6 cannot be linked by C programs:

                Also I don't see how this dll should work at all - where is your running Q(Core)Application?

                I think it's related to this topic and the dll contains the complete Qt GUI program including the QApplication instance.
                (No idea if this works like this... never linked a Qt GUI app to a C program)

                @pedisChen try an absolute path to your DLL, if the only issue is that it can't locate it...
                Check your working directory (where your C program is executed in) and how this is relative to the directory where your DLL is.

                P Offline
                P Offline
                pedisChen
                wrote on 27 Oct 2024, 04:46 last edited by
                #6

                @Pl45m4 It is workable. thank you!

                1 Reply Last reply
                0
                • P pedisChen has marked this topic as solved on 27 Oct 2024, 04:46

                3/6

                26 Oct 2024, 18:07

                • Login

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