Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to read registry binary data using QT?
Qt 6.11 is out! See what's new in the release blog

How to read registry binary data using QT?

Scheduled Pinned Locked Moved Unsolved General and Desktop
24 Posts 7 Posters 6.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Narasimman
    wrote on last edited by
    #3

    Checked already I am not getting info for reading the binary data, all other i am able to read

    RatzzR 1 Reply Last reply
    0
    • N Narasimman

      Checked already I am not getting info for reading the binary data, all other i am able to read

      RatzzR Offline
      RatzzR Offline
      Ratzz
      wrote on last edited by
      #4

      @Narasimman
      Something like this?

          QSettings reg("HKEY_CURRENT_USER\\Environment", QSettings::NativeFormat);
          QString stringg = reg.value("TEST").toString();
          QByteArray data = QByteArray((const char*)stringg.utf16());
          qDebug() << "value : " << data;

      --Alles ist gut.

      jsulmJ 1 Reply Last reply
      0
      • RatzzR Ratzz

        @Narasimman
        Something like this?

            QSettings reg("HKEY_CURRENT_USER\\Environment", QSettings::NativeFormat);
            QString stringg = reg.value("TEST").toString();
            QByteArray data = QByteArray((const char*)stringg.utf16());
            qDebug() << "value : " << data;
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #5

        @Narasimman @Ratzz I would say (since QString isn't binary):

        QSettings reg("HKEY_CURRENT_USER\\Environment", QSettings::NativeFormat);
        QByteArray data = reg.value("TEST").toByteArray();
        qDebug() << "value : " << data;
        

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • N Offline
          N Offline
          Narasimman
          wrote on last edited by
          #6

          @jsulm said in How to read registry binary data using QT?:

          qDebug() << "value : " << data;

          QSettings Drives("HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices",QSettings::NativeFormat);

          QStringList keys = Drives.allKeys();
          qDebug()<<"jeys list :"<<keys.count();
          
          for(int i=0;i<keys.count();i++)
          {
              QString strkey = keys.at(i);
              qDebug()<<"Key: "<<strkey;
              QVariant strvalue=Drives.value(strkey);
              QByteArray data = Drives.value(strkey).toByteArray();
              qDebug() << "valuebyte : " << data;
              qDebug()<<"Value Type : "<<strvalue.type()<<" name : "<<strvalue.typeName()<<" value :"<<strvalue.toByteArray().toStdString().c_str();
          
          }
          

          This is my code.

          Below are the output of the above code

          [Key: "#{f6d859dc-538d-11e7-81fe-f85971670abf}"]
          [valuebyte : "\xE4\xB5\x84\xE4\xBD\x89\xE4\xA4\xBA\xE3\xA9\x84\xE6\xBB\x99\xEE\xBC\xB9\xE1\xA3\x9F\xE4\xA4\x8A\xE8\xBA\xA9\xEB\xBF\x8B\xE5\xBC\xB0\xE3\x88\x88"]
          [Value Type : QVariant::QString name : QString value : ????????????]
          [Key: "/??/Volume{0040c5a3-3c8c-11e8-8216-54e1ad32fe3b}"]
          [valuebyte : ""]
          [Value Type : QVariant::Invalid name : value : ]
          [Key: "/??/Volume{1eb29a46-1878-11e8-8211-54e1ad32fe3b}"]
          [valuebyte : ""]

          But still not getting the value. Let me know what is the issue ?

          jsulmJ 1 Reply Last reply
          0
          • N Narasimman

            @jsulm said in How to read registry binary data using QT?:

            qDebug() << "value : " << data;

            QSettings Drives("HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices",QSettings::NativeFormat);

            QStringList keys = Drives.allKeys();
            qDebug()<<"jeys list :"<<keys.count();
            
            for(int i=0;i<keys.count();i++)
            {
                QString strkey = keys.at(i);
                qDebug()<<"Key: "<<strkey;
                QVariant strvalue=Drives.value(strkey);
                QByteArray data = Drives.value(strkey).toByteArray();
                qDebug() << "valuebyte : " << data;
                qDebug()<<"Value Type : "<<strvalue.type()<<" name : "<<strvalue.typeName()<<" value :"<<strvalue.toByteArray().toStdString().c_str();
            
            }
            

            This is my code.

            Below are the output of the above code

            [Key: "#{f6d859dc-538d-11e7-81fe-f85971670abf}"]
            [valuebyte : "\xE4\xB5\x84\xE4\xBD\x89\xE4\xA4\xBA\xE3\xA9\x84\xE6\xBB\x99\xEE\xBC\xB9\xE1\xA3\x9F\xE4\xA4\x8A\xE8\xBA\xA9\xEB\xBF\x8B\xE5\xBC\xB0\xE3\x88\x88"]
            [Value Type : QVariant::QString name : QString value : ????????????]
            [Key: "/??/Volume{0040c5a3-3c8c-11e8-8216-54e1ad32fe3b}"]
            [valuebyte : ""]
            [Value Type : QVariant::Invalid name : value : ]
            [Key: "/??/Volume{1eb29a46-1878-11e8-8211-54e1ad32fe3b}"]
            [valuebyte : ""]

            But still not getting the value. Let me know what is the issue ?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #7

            @Narasimman
            Here you got binary:
            [Key: "#{f6d859dc-538d-11e7-81fe-f85971670abf}"]
            [valuebyte : "\xE4\xB5\x84\xE4\xBD\x89\xE4\xA4\xBA\xE3\xA9\x84\xE6\xBB\x99\xEE\xBC\xB9\xE1\xA3\x9F\xE4\xA4\x8A\xE8\xBA\xA9\xEB\xBF\x8B\xE5\xBC\xB0\xE3\x88\x88"]

            Here the content is a string and you should treat it as string:
            [Value Type : QVariant::QString name : QString value : ????????????]
            [Key: "/??/Volume{0040c5a3-3c8c-11e8-8216-54e1ad32fe3b}"]
            [valuebyte : ""]

            Here you got something invalid:
            [Value Type : QVariant::Invalid name : value : ]
            [Key: "/??/Volume{1eb29a46-1878-11e8-8211-54e1ad32fe3b}"]
            [valuebyte : ""]

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            N 1 Reply Last reply
            1
            • jsulmJ jsulm

              @Narasimman
              Here you got binary:
              [Key: "#{f6d859dc-538d-11e7-81fe-f85971670abf}"]
              [valuebyte : "\xE4\xB5\x84\xE4\xBD\x89\xE4\xA4\xBA\xE3\xA9\x84\xE6\xBB\x99\xEE\xBC\xB9\xE1\xA3\x9F\xE4\xA4\x8A\xE8\xBA\xA9\xEB\xBF\x8B\xE5\xBC\xB0\xE3\x88\x88"]

              Here the content is a string and you should treat it as string:
              [Value Type : QVariant::QString name : QString value : ????????????]
              [Key: "/??/Volume{0040c5a3-3c8c-11e8-8216-54e1ad32fe3b}"]
              [valuebyte : ""]

              Here you got something invalid:
              [Value Type : QVariant::Invalid name : value : ]
              [Key: "/??/Volume{1eb29a46-1878-11e8-8211-54e1ad32fe3b}"]
              [valuebyte : ""]

              N Offline
              N Offline
              Narasimman
              wrote on last edited by
              #8

              @jsulm said in How to read registry binary data using QT?:

              @Narasimman
              Here you got binary:
              [Key: "#{f6d859dc-538d-11e7-81fe-f85971670abf}"]
              [valuebyte : "\xE4\xB5\x84\xE4\xBD\x89\xE4\xA4\xBA\xE3\xA9\x84\xE6\xBB\x99\xEE\xBC\xB9\xE1\xA3\x9F\xE4\xA4\x8A\xE8\xBA\xA9\xEB\xBF\x8B\xE5\xBC\xB0\xE3\x88\x88"]

              Below all the registry keys are of same type as binary , why i got 1 as string another a binary, no clue on this.

              Here the content is a string and you should treat it as string:
              [Value Type : QVariant::QString name : QString value : ????????????]
              [Key: "/??/Volume{0040c5a3-3c8c-11e8-8216-54e1ad32fe3b}"]
              [valuebyte : ""]

              Here you got something invalid:
              [Value Type : QVariant::Invalid name : value : ]
              [Key: "/??/Volume{1eb29a46-1878-11e8-8211-54e1ad32fe3b}"]
              [valuebyte : ""]

              jsulmJ 1 Reply Last reply
              0
              • N Narasimman

                @jsulm said in How to read registry binary data using QT?:

                @Narasimman
                Here you got binary:
                [Key: "#{f6d859dc-538d-11e7-81fe-f85971670abf}"]
                [valuebyte : "\xE4\xB5\x84\xE4\xBD\x89\xE4\xA4\xBA\xE3\xA9\x84\xE6\xBB\x99\xEE\xBC\xB9\xE1\xA3\x9F\xE4\xA4\x8A\xE8\xBA\xA9\xEB\xBF\x8B\xE5\xBC\xB0\xE3\x88\x88"]

                Below all the registry keys are of same type as binary , why i got 1 as string another a binary, no clue on this.

                Here the content is a string and you should treat it as string:
                [Value Type : QVariant::QString name : QString value : ????????????]
                [Key: "/??/Volume{0040c5a3-3c8c-11e8-8216-54e1ad32fe3b}"]
                [valuebyte : ""]

                Here you got something invalid:
                [Value Type : QVariant::Invalid name : value : ]
                [Key: "/??/Volume{1eb29a46-1878-11e8-8211-54e1ad32fe3b}"]
                [valuebyte : ""]

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #9

                @Narasimman How do these binary keys look in regedit.exe?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  Narasimman
                  wrote on last edited by
                  #10

                  0_1530790912844_bindata.png

                  Above is the screeshot of the registry key.

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    Narasimman
                    wrote on last edited by
                    #11

                    Any update on this ?

                    1 Reply Last reply
                    0
                    • RatzzR Offline
                      RatzzR Offline
                      Ratzz
                      wrote on last edited by Ratzz
                      #12

                      @Narasimman @jsulm
                      I just noticed

                         QSettings reg("HKEY_LOCAL_MACHINE\\SYSTEM\\DriverDatabase", QSettings::NativeFormat);
                      
                          QString stringg = reg.value("OemInfMap").toString();
                          QByteArray data = QByteArray((const char*)stringg.utf16());
                          qDebug() << "value using QString : " << data;
                      
                          QByteArray byteArray = reg.value("OemInfMap").toByteArray();
                          qDebug() << "value using QByteArray : " << byteArray;
                      

                      Gives me

                      value using QString :  "\xFF\xFF\xFF\xF0"
                      value using QByteArray :  "\xEF\xBF\xBF\xEF\x83\xBF"
                      
                      

                      0_1531203315815_88bd20f7-15ab-4805-a2bf-da27377756ec-image.png

                      --Alles ist gut.

                      jsulmJ 1 Reply Last reply
                      1
                      • RatzzR Ratzz

                        @Narasimman @jsulm
                        I just noticed

                           QSettings reg("HKEY_LOCAL_MACHINE\\SYSTEM\\DriverDatabase", QSettings::NativeFormat);
                        
                            QString stringg = reg.value("OemInfMap").toString();
                            QByteArray data = QByteArray((const char*)stringg.utf16());
                            qDebug() << "value using QString : " << data;
                        
                            QByteArray byteArray = reg.value("OemInfMap").toByteArray();
                            qDebug() << "value using QByteArray : " << byteArray;
                        

                        Gives me

                        value using QString :  "\xFF\xFF\xFF\xF0"
                        value using QByteArray :  "\xEF\xBF\xBF\xEF\x83\xBF"
                        
                        

                        0_1531203315815_88bd20f7-15ab-4805-a2bf-da27377756ec-image.png

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #13

                        @Ratzz said in How to read registry binary data using QT?:

                        QByteArray data = QByteArray((const char*)stringg.utf16());

                        This is simply wrong. If it is binary already then do

                        QByteArray data = reg.value("OemInfMap").toByteArray();
                        

                        Do NOT convert binary data to string, then to Unicode string and then to binary again!

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        1
                        • RatzzR Offline
                          RatzzR Offline
                          Ratzz
                          wrote on last edited by
                          #14

                          @jsulm

                          QByteArray data = reg.value("OemInfMap").toByteArray();
                          

                          With this why don't I get exact binary data??

                          --Alles ist gut.

                          D jsulmJ 2 Replies Last reply
                          1
                          • RatzzR Ratzz

                            @jsulm

                            QByteArray data = reg.value("OemInfMap").toByteArray();
                            

                            With this why don't I get exact binary data??

                            D Offline
                            D Offline
                            Devopia53
                            wrote on last edited by Devopia53
                            #15

                            @Ratzz

                            You ar right. The REG_BINARY type is converted to QString. But there is a bug. If the length of the binary data is odd, the last data is truncated.

                            in qsettings_win.cpp, line 547, Qt 5.10.0
                            s = QString::fromWCharArray((const wchar_t *)data.constData(), data.size() / 2);

                            JonBJ 1 Reply Last reply
                            1
                            • RatzzR Ratzz

                              @jsulm

                              QByteArray data = reg.value("OemInfMap").toByteArray();
                              

                              With this why don't I get exact binary data??

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #16

                              @Ratzz Yes, just tested - QVariant contains a string, I expected it to contain QByteArray for binary data.

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              1
                              • D Devopia53

                                @Ratzz

                                You ar right. The REG_BINARY type is converted to QString. But there is a bug. If the length of the binary data is odd, the last data is truncated.

                                in qsettings_win.cpp, line 547, Qt 5.10.0
                                s = QString::fromWCharArray((const wchar_t *)data.constData(), data.size() / 2);

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #17

                                @Devopia53 said in How to read registry binary data using QT?:

                                @Ratzz

                                You ar right. The REG_BINARY type is converted to QString. But there is a bug. If the length of the binary data is odd, the last data is truncated.

                                in qsettings_win.cpp, line 547, Qt 5.10.0
                                s = QString::fromWCharArray((const wchar_t *)data.constData(), data.size() / 2);

                                This in itself is not a bug. A "wide char array" is an array of bytes in which each pair represents one character. Therefore the length must be even, and the code is as correct as it can be.

                                If the data is arbitrary bytes, as it seems to be, it should never be treated as a wide char array/QString.

                                I don't know, but does (unfixed) https://bugreports.qt.io/browse/QTBUG-98 imply this is simply still not doable in Qt5?

                                It could be an enhancement to QSettings to support more registry types which are correctly mapped in the QVariant. Qt should map REG_BINARY to QByteArray

                                We can't fix this until Qt 5, because REG_BINARY is currently read by QSettings as a utf-16 encoded QString. Changing this would break existing applications.

                                Naughty, naughty!

                                D 1 Reply Last reply
                                1
                                • JonBJ JonB

                                  @Devopia53 said in How to read registry binary data using QT?:

                                  @Ratzz

                                  You ar right. The REG_BINARY type is converted to QString. But there is a bug. If the length of the binary data is odd, the last data is truncated.

                                  in qsettings_win.cpp, line 547, Qt 5.10.0
                                  s = QString::fromWCharArray((const wchar_t *)data.constData(), data.size() / 2);

                                  This in itself is not a bug. A "wide char array" is an array of bytes in which each pair represents one character. Therefore the length must be even, and the code is as correct as it can be.

                                  If the data is arbitrary bytes, as it seems to be, it should never be treated as a wide char array/QString.

                                  I don't know, but does (unfixed) https://bugreports.qt.io/browse/QTBUG-98 imply this is simply still not doable in Qt5?

                                  It could be an enhancement to QSettings to support more registry types which are correctly mapped in the QVariant. Qt should map REG_BINARY to QByteArray

                                  We can't fix this until Qt 5, because REG_BINARY is currently read by QSettings as a utf-16 encoded QString. Changing this would break existing applications.

                                  Naughty, naughty!

                                  D Offline
                                  D Offline
                                  Devopia53
                                  wrote on last edited by
                                  #18

                                  @JonB

                                  I also know about Wide-char. If it's a problem with Wide-char, that's right. However, it is a problem that REG_BINARY type is converted to QString without used QByteArray.

                                  JonBJ 1 Reply Last reply
                                  0
                                  • D Devopia53

                                    @JonB

                                    I also know about Wide-char. If it's a problem with Wide-char, that's right. However, it is a problem that REG_BINARY type is converted to QString without used QByteArray.

                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on last edited by
                                    #19

                                    @Devopia53
                                    See my extra typings in my post.

                                    D 1 Reply Last reply
                                    0
                                    • JonBJ JonB

                                      @Devopia53
                                      See my extra typings in my post.

                                      D Offline
                                      D Offline
                                      Devopia53
                                      wrote on last edited by
                                      #20

                                      @JonB

                                      Yes I have known the problem for a long time. So, in this case, I use Windows API to handle it directly.

                                      JonBJ aha_1980A 2 Replies Last reply
                                      1
                                      • D Devopia53

                                        @JonB

                                        Yes I have known the problem for a long time. So, in this case, I use Windows API to handle it directly.

                                        JonBJ Offline
                                        JonBJ Offline
                                        JonB
                                        wrote on last edited by
                                        #21

                                        @Devopia53
                                        So far as I can see, your approach is indeed required at present. The OP needs to use a native Windows registry call to deal correctly with the data. Perhaps you'd be kind enough to give him a skeleton nudge in this direction for what you wrote to do so?

                                        kshegunovK 1 Reply Last reply
                                        0
                                        • D Devopia53

                                          @JonB

                                          Yes I have known the problem for a long time. So, in this case, I use Windows API to handle it directly.

                                          aha_1980A Offline
                                          aha_1980A Offline
                                          aha_1980
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #22

                                          Hi @Devopia53

                                          there is even a report for this: https://bugreports.qt.io/browse/QTBUG-52960

                                          Qt has to stay free or it will die.

                                          1 Reply Last reply
                                          1

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved