Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Class in DLL doesnt work properly



  • 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



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



  • Hi Koahnig:

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



  • 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/



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


Log in to reply