PDA

View Full Version : convert BYTE* to QString



tpf80
8th April 2007, 00:37
Currently I am using MODULEENTRY32 windows function to get information about a process. the modBaseAddr is returned as "BYTE*". I got many errors trying to convert BYTE* to a QString.

I did some research and found that "BYTE*" is really another name for "unsigned char*". I also read that the unsigned char should work in functions requiring char, however my compiler gave me errors when trying to use the unsigned char:


qModuleBase = QString(ModuleBase);

gave the error:


calculatorform.cpp:75: error: call of overloaded `QString(unsigned char*&)' is ambiguous
C:/Qt/4.2.3/include/QtCore/../../src/corelib/tools/qstring.h:344: note: candidates are: QString::QString(const QByteArray&) <near match>
C:/Qt/4.2.3/include/QtCore/../../src/corelib/tools/qstring.h:341: note: QString::QString(const char*) <near match>
C:/Qt/4.2.3/include/QtCore/../../src/corelib/tools/qstring.h:607: note: QString::QString(const QString&) <near match>
C:/Qt/4.2.3/include/QtCore/../../src/corelib/tools/qstring.h:74: note: QString::QString(QChar) <near match>
mingw32-make[1]: *** [tmp\obj\release_shared\calculatorform.o] Error 1


I tried to convert it in a couple of ways, and these changes allowed it to be compiled:


qModuleBase = QString((char*)ModuleBase);
or

qModuleBase = QString::fromLocal8Bit((char*)ModuleBase);

However when I try and run the program with either of those fixes, it crashes. when I comment out either of those lines of code, the program runs with no errors, but of course will not display the ModuleBase.

In any case I need to convert this BYTE* or unsigned char* to a Qstring mainly so I can display the value that it returns in a QLineEdit. I actually don't care what format it is in as long as I can display it in the line edit properly.

jacek
8th April 2007, 01:00
Does ModuleBase point to a null-terminated array of characters or it just points to some binary data?

Maybe QByteArray will be more appropriate?

hickscorp
8th April 2007, 01:04
Hello,

I believe you have to use a zero terminated char*, and then create your QString using the STATIC method:

QString yourQStringVar = QString::fromUtf8(your_zts_char_var);

But maybe there is easier?
Hope this helps ^^
Pierre.

tpf80
8th April 2007, 02:30
qModuleBase = QString::fromUtf8(ModuleBase);
returns a compiler error:

calculatorform.cpp:75: error: invalid conversion from `unsigned char*' to `const char*'
calculatorform.cpp:75: error: initializing argument 1 of `static QString QString::fromUtf8(const char*, int)'
mingw32-make[1]: *** [tmp\obj\release_shared\calculatorform.o] Error 1
mingw32-make[1]: Leaving directory `C:/Qt/4.2.3/Kdevelop/navui'
mingw32-make: *** [release] Error 2
so I tried:

qModuleBase = QString::fromUtf8((char*)ModuleBase);
which compiles fine, but the program crashes.

MSDN shows MODULEENTRY32 as this:

typedef struct tagMODULEENTRY32 {
DWORD dwSize;
DWORD th32ModuleID;
DWORD th32ProcessID;
DWORD GlblcntUsage;
DWORD ProccntUsage;
BYTE* modBaseAddr;
DWORD modBaseSize;
HMODULE hModule;
TCHAR szModule[MAX_MODULE_NAME32 + 1];
TCHAR szExePath[MAX_PATH];
} MODULEENTRY32,
*PMODULEENTRY32;

heres something interesting as well:

cout << hex << (PVOID)ModuleBase << endl;
This works to echo the value to the command line.

marcel
8th April 2007, 04:54
Hello,

Since the modBaseAddr holds an address, you probably want this displayed in hex.



QString hexStr;
hextStr.sprintf( "%0x%08X", ( unsigned int *)modBaseAddr );
The code above should work on a 32 bit processor but I don't really know if it will be correct on a 64 bit processor.

hickscorp
8th April 2007, 10:40
I'm not sure... What do you want to store in your QString??
Let's say, you want the process name.
- Your "ModuleBase" variable is an instance of MODULEENTRY32 do this:
QString yourQString = QString::fromUtf8(ModuleBase.szModule)
- Your "ModuleBase" variable is an pointer to a MODULEENTRY32 instance do this:
QString yourQString = QString::fromUtf8(ModuleBase->szModule)

But again... try to be more specific about what you do want to retrieve in the ModuleBase variable.

Hope it helps :)
Pierre.

tpf80
8th April 2007, 11:29
actually I said above that it was "modBaseAddr", the BYTE*.

Marcel hit it on the head with:

QString hexStr;
hextStr.sprintf( "%0x%08X", ( unsigned int *)modBaseAddr );

Modified a little bit, this perfectly suited my needs of displaying the base in the line edit.

Thanks to everyone that helped out!