PDA

View Full Version : The problem with QLibrary, help me!



dungsivn
16th January 2008, 23:41
I created a small application to use the library:



// Test is a simple widget for testing
Test::Test( QWidget* parent): QWidget(parent)
{
setupUi(this);
QByteArray buffer;
QLibrary mylib("kernel32);
typedef int (*GetLocalInfo)( int Locale, int LcType, char* l, int chData);

GetLocaleInfor myFunc = (GetLocaleInfo)mylib.resolve("GetLocaleInfoA");

if(myFunc(0x400, 0x1003, Buffer.data(), 99) !=0)
{
int v1= 1;
float b2= 2.0;
QString str = "Hi everyone";
QMessageBox::information(0, "Hello","Hello");

//...
}


After the line "QString str = "Hi everyone";", the pointer "this" would be changed the address, I don't know why? If I replace QString by another Object( QTextEdit, QLineEdit...),
it also changed "this" pointer. After the application changed the address, it display a warnining means that .exe was modified... and stop.

Could you tell me that way not to change the address?
Thanks!

The Storm
17th January 2008, 00:37
I can't understand very good what do you mean but I see a little mistake in your code:
QLibrary mylib("kernel32);
perhaps it must be
QLibrary mylib("kernel32"); ? :)

dungsivn
17th January 2008, 01:42
Sorry for my mistake( QLibrary mylib("kernel32)) ;), I mean that when using "myFunc(0x400, 0x1003, Buffer.data(), 99)" function, the address of Test object will be changed( "this" pointer). This is the structure of that small project: main.cpp, test.h and test.cpp. This is very simple project to load a .dll file in Windows. In main.cpp, it only creates an instance of Test.
In test.h, there are two function: constructor and destructor. If I comment the function:
myFunc(0x400, 0x1003, Buffer.data(), 99), everything is OK.

I don't know why?

dungsivn
17th January 2008, 13:20
a simple question: how to create a small GUI application with QLibary and load .dll file, may be "kernel32"?

Do I need any flag, header, library in that application? I use Qt 4.3.3 and Visual Studio 2005.
In my current program, I don't use and flag, lib in the setting.

Thanks!

bender86
17th January 2008, 14:03
Why not this way?
#include <windows.h>

...

// Test is a simple widget for testing
Test::Test( QWidget* parent): QWidget(parent)
{
setupUi(this);
QByteArray buffer;

if(GetLocaleInfoA(0x400, 0x1003, Buffer.data(), 99) !=0)
{
int v1= 1;
float b2= 2.0;
QString str = "Hi everyone";
QMessageBox::information(0, "Hello","Hello");

//...
}

}
And add in pro file LIBS += kernel32.lib?


By the way, are you sure that buffer.data() is writable? From docs, It seems that with default ctor, QByteArray is empty, so QByteArray::data() is an invalid pointer.