Results 1 to 7 of 7

Thread: convert BYTE* to QString

  1. #1
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question convert BYTE* to QString

    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:

    Qt Code:
    1. qModuleBase = QString(ModuleBase);
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. qModuleBase = QString((char*)ModuleBase);
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. qModuleBase = QString::fromLocal8Bit((char*)ModuleBase);
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by jacek; 7th April 2007 at 23:54. Reason: changed [code] to [quote]

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: convert BYTE* to QString

    Does ModuleBase point to a null-terminated array of characters or it just points to some binary data?

    Maybe QByteArray will be more appropriate?

  3. #3
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: convert BYTE* to QString

    Hello,

    I believe you have to use a zero terminated char*, and then create your QString using the STATIC method:
    Qt Code:
    1. QString yourQStringVar = QString::fromUtf8(your_zts_char_var);
    To copy to clipboard, switch view to plain text mode 

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

  4. #4
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: convert BYTE* to QString

    Qt Code:
    1. qModuleBase = QString::fromUtf8(ModuleBase);
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. qModuleBase = QString::fromUtf8((char*)ModuleBase);
    To copy to clipboard, switch view to plain text mode 
    which compiles fine, but the program crashes.

    MSDN shows MODULEENTRY32 as this:
    Qt Code:
    1. typedef struct tagMODULEENTRY32 {
    2. DWORD dwSize;
    3. DWORD th32ModuleID;
    4. DWORD th32ProcessID;
    5. DWORD GlblcntUsage;
    6. DWORD ProccntUsage;
    7. BYTE* modBaseAddr;
    8. DWORD modBaseSize;
    9. HMODULE hModule;
    10. TCHAR szModule[MAX_MODULE_NAME32 + 1];
    11. TCHAR szExePath[MAX_PATH];
    12. } MODULEENTRY32,
    13. *PMODULEENTRY32;
    To copy to clipboard, switch view to plain text mode 

    heres something interesting as well:
    Qt Code:
    1. cout << hex << (PVOID)ModuleBase << endl;
    To copy to clipboard, switch view to plain text mode 
    This works to echo the value to the command line.
    Last edited by tpf80; 8th April 2007 at 01:36. Reason: reformatted to look better

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: convert BYTE* to QString

    Hello,

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

    Qt Code:
    1. QString hexStr;
    2. hextStr.sprintf( "%0x%08X", ( unsigned int *)modBaseAddr );
    To copy to clipboard, switch view to plain text mode 
    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.
    Last edited by marcel; 8th April 2007 at 03:55. Reason: reformatted to look better

  6. The following user says thank you to marcel for this useful post:

    tpf80 (8th April 2007)

  7. #6
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: convert BYTE* to QString

    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.

  8. #7
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: convert BYTE* to QString

    actually I said above that it was "modBaseAddr", the BYTE*.

    Marcel hit it on the head with:
    Qt Code:
    1. QString hexStr;
    2. hextStr.sprintf( "%0x%08X", ( unsigned int *)modBaseAddr );
    To copy to clipboard, switch view to plain text mode 

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

    Thanks to everyone that helped out!

Similar Threads

  1. How to convert from QString to string ?
    By probine in forum Newbie
    Replies: 2
    Last Post: 1st December 2010, 01:50
  2. How to convert Int to QString in QT4?
    By drake1983 in forum Newbie
    Replies: 2
    Last Post: 11th March 2007, 06:58
  3. Convert wstring to QString.
    By bunjee in forum Qt Programming
    Replies: 2
    Last Post: 23rd January 2007, 14:18
  4. Replies: 2
    Last Post: 12th June 2006, 17:47
  5. How to convert from QString to quint16 ?
    By probine in forum Qt Programming
    Replies: 5
    Last Post: 31st March 2006, 09:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.