Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. 3rd Party Software
  4. Class in DLL doesnt work properly
Qt 6.11 is out! See what's new in the release blog

Class in DLL doesnt work properly

Scheduled Pinned Locked Moved 3rd Party Software
5 Posts 3 Posters 3.4k Views 1 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello everyone,
    I have written a DLL (a class) in codeblocks with just one function:

    This is my .cpp file. Test function only prints the input argument.

    @#include "main.h"
    #include <stdio.h>

    int MyFirstClass::test(int x)
    {
    printf("D:%d\n",x);
    return x;
    }

    BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
    {
    switch (fdwReason)
    {
    case DLL_PROCESS_ATTACH:
    break;

        case DLL_PROCESS_DETACH:
            break;
    
        case DLL_THREAD_ATTACH:
            break;
    
        case DLL_THREAD_DETACH:
            break;
    }
    return TRUE;
    

    }
    @

    This is my .h file. I have copied this to the QT console application project I am working on.

    @#ifndef MAIN_H
    #define MAIN_H
    #include <windows.h>
    #ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
    #else
    #define DLL_EXPORT __declspec(dllimport)
    #endif
    #ifdef __cplusplus
    extern "C"
    {
    #endif
    class DLL_EXPORT MyFirstClass{
    public:
    int test(int x);
    };
    #ifdef __cplusplus
    }
    #endif
    #endif // MAIN_H@

    This is my test code in QT:

    @#include <QCoreApplication>
    #include <stdio.h>
    #include <stdlib.h>
    #include "main.h"

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

    int val;
    val = 0;
    MyFirstClass *MyFC = 0;
    scanf("%d",&val);
    val = MyFC->test(val);
    
    return a.exec&#40;&#41;;
    

    }
    @

    I have tested my DLL in codeblocks and there is no problem, but when I try this code in QT it prints a random value. I have realized that the argument val obtain its value for a buffer. For example if in the last code line 14 I write a printf("%d",whatever), the function TEST prints the value of WHATEVER.

    Thanks

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      welcome to devnet

      [quote author="gcucho" date="1387808563"]
      @#include <QCoreApplication>
      #include <stdio.h>
      #include <stdlib.h>
      #include "main.h"

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

      int val;
      val = 0;
      MyFirstClass *MyFC = 0;
      scanf("%d",&val);
      val = MyFC->test(val);
      
      return a.exec&#40;&#41;;
      

      }
      @
      [/quote]

      I am wondering why you do not have a seg fault when executing this piece . You initialize the pointer with zero which is good in general, when not using the pointer directly. However, in your case you do not allocate the memory for the class.
      @
      int main(int argc, char *argv[])
      {
      QCoreApplication a(argc, argv);

      int val;
      val = 0;
      MyFirstClass *MyFC = new MyFirstClass();
      scanf("%d",&val);
      val = MyFC->test(val);
      
      return a.exec(&#41;;
      

      }
      @

      This might work when your class does not nasty things in its definition.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Hi Koahnig:

        I test with the correction and the result was the same :( , It prints a random value.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          rwxc
          wrote on last edited by
          #4

          You can try converting the returned integer to a standard string and then appending a \0 at the end of the string but for this you will need to do a bit more processing.

          So try converting the returned value to a (char *) array that will have enough elements to hold the converted integer + an additional element for '\0' . Then you can try printing out the string.

          The idea is to append the null terminating character to the string.

          That might resolve your issue. I have had similar issues in the past when dealing with strings. I hope this helps. A quicker to do it would be to convert the value to an std::string and then use the c_str() method member of std::string() and that should return a char * array with a null in it - see: http://www.cplusplus.com/reference/string/string/c_str/

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            Thanks for the replies.

            I figure out the solution. My MINGW g++.exe in codeblocks was 4.7, and the MINGW g++.exe in QT was 4.8, so I changed all the toolchains in codeblocks with those from QT and everything works fine now.

            G.C.

            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