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?

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
    #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 Online
                JonBJ Online
                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 Online
                    JonBJ Online
                    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 Online
                        JonBJ Online
                        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
                          • JonBJ JonB

                            @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 Offline
                            kshegunovK Offline
                            kshegunov
                            Moderators
                            wrote on last edited by kshegunov
                            #23

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

                            Perhaps you'd be kind enough to give him a skeleton nudge in this direction for what you wrote

                            https://bitbucket.org/kshegunov/qtdaemon/src/f8c744b54a6a878ca612ccff429364450a5cdb87/src/daemon/private/controllerbackend_win.cpp?at=master&fileviewer=file-view-default#controllerbackend_win.cpp-363

                            This should help with that.

                            Read and abide by the Qt Code of Conduct

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

                              memset(keyValue,0,(MAX_PATH * sizeof(wchar_t)));
                              long lResult1= RegQueryValueEx(hKey,KeyValue1.data(),NULL,NULL,reinterpret_cast<unsigned char*>(keyValue),&bufferSize);
                              if (!lResult1)
                              command = QString::fromStdWString(keyValue);

                              finally i used direct win32 api and able to read the binary string. above is the working code.

                              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