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();
}
@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();
}
@
[/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();
}
@This might work when your class does not nasty things in its definition.
-
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/