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. Windows 7 and Windows 10, QSettings read registory REG_DWORD keys, can not get the values
QtWS25 Last Chance

Windows 7 and Windows 10, QSettings read registory REG_DWORD keys, can not get the values

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 2.6k Views
  • 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.
  • F Offline
    F Offline
    Frankkon
    wrote on 7 Jul 2017, 07:49 last edited by Frankkon 7 Jul 2017, 07:53
    #1

    Windows 7 and Windows 10, QSettings read registory REG_DWORD keys, can not get the values,but other type keys can read well, eg. REG_SZ.
    code as below:

    void MainWindow::on_pushButton_clicked()
    {
    ui->listWidget->clear();
    QString str = ui->lineEdit->text();
    QSettings reg(str, QSettings::Registry64Format);
    QStringList keyList=reg.childKeys();
    foreach(QString key, keyList)
    {
    QString keyvalue=reg.value(key).toString();
    ui->listWidget->addItem(key + " = " + keyvalue);
    }
    }
    output as below:
    0_1499413859266_regedit.png
    0_1499413886062_myProgram.png

    J 1 Reply Last reply 7 Jul 2017, 08:30
    0
    • F Frankkon
      7 Jul 2017, 07:49

      Windows 7 and Windows 10, QSettings read registory REG_DWORD keys, can not get the values,but other type keys can read well, eg. REG_SZ.
      code as below:

      void MainWindow::on_pushButton_clicked()
      {
      ui->listWidget->clear();
      QString str = ui->lineEdit->text();
      QSettings reg(str, QSettings::Registry64Format);
      QStringList keyList=reg.childKeys();
      foreach(QString key, keyList)
      {
      QString keyvalue=reg.value(key).toString();
      ui->listWidget->addItem(key + " = " + keyvalue);
      }
      }
      output as below:
      0_1499413859266_regedit.png
      0_1499413886062_myProgram.png

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 7 Jul 2017, 08:30 last edited by
      #2

      @Frankkon said in Windows 7 and Windows 10, QSettings read registory REG_DWORD keys, can not get the values:

      reg.value(key).toString()

      Did you try to print out reg.value(key)

      qDebug() << reg.value(key);
      

      You're converting it to string, but actually it is an integer number. You should convert to string like this:

      QString::number(reg.value(key).toInt());
      

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

      F 1 Reply Last reply 8 Jul 2017, 08:12
      2
      • J jsulm
        7 Jul 2017, 08:30

        @Frankkon said in Windows 7 and Windows 10, QSettings read registory REG_DWORD keys, can not get the values:

        reg.value(key).toString()

        Did you try to print out reg.value(key)

        qDebug() << reg.value(key);
        

        You're converting it to string, but actually it is an integer number. You should convert to string like this:

        QString::number(reg.value(key).toInt());
        
        F Offline
        F Offline
        Frankkon
        wrote on 8 Jul 2017, 08:12 last edited by Frankkon 7 Aug 2017, 08:14
        #3

        @jsulm Thank you very much. I do as you said, but it still not work well.
        my debug code as below:
        qDebug() << key << " = " << reg.value(key);
        qDebug() << key << " = " << reg.value(key).toInt();
        qDebug() << key << " = " << QString::number(reg.value(key).toInt());
        the debug output as below:
        0_1499501461174_debug.PNG

        but regedit tool shows as below:
        0_1499501507610_regedit.PNG

        can you help me? thank you.

        1 Reply Last reply
        0
        • H Offline
          H Offline
          hskoglund
          wrote on 8 Jul 2017, 17:05 last edited by
          #4

          Hi, unfortunately when registry keys contain slashes or backslashes QSettings does not work :-(
          But you can still use old the old Win32 API, like this:

          Add
          LIBS += -ladvapi32
          to your .pro file.

          Add #include "windows.h" to your .cpp file.

          And try something like this:

          QSettings reg("HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\SharedDLLs\\",QSettings::NativeFormat);
          
          for (QString key : reg.childKeys())
          {
              key = key.replace("/","\\");    // replace back to backslashes to keep Windows happy
              DWORD dw = 0,sdw = sizeof(dw);  // dword value comes to dw
              ::RegGetValue(HKEY_LOCAL_MACHINE,L"Software\\Microsoft\\Windows\\CurrentVersion\\SharedDLLs",(LPCWSTR) key.constData(),RRF_RT_DWORD,NULL,&dw,&sdw);
          
              qDebug() << key << dw;
          }
          
          F 1 Reply Last reply 10 Jul 2017, 06:57
          1
          • H hskoglund
            8 Jul 2017, 17:05

            Hi, unfortunately when registry keys contain slashes or backslashes QSettings does not work :-(
            But you can still use old the old Win32 API, like this:

            Add
            LIBS += -ladvapi32
            to your .pro file.

            Add #include "windows.h" to your .cpp file.

            And try something like this:

            QSettings reg("HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\SharedDLLs\\",QSettings::NativeFormat);
            
            for (QString key : reg.childKeys())
            {
                key = key.replace("/","\\");    // replace back to backslashes to keep Windows happy
                DWORD dw = 0,sdw = sizeof(dw);  // dword value comes to dw
                ::RegGetValue(HKEY_LOCAL_MACHINE,L"Software\\Microsoft\\Windows\\CurrentVersion\\SharedDLLs",(LPCWSTR) key.constData(),RRF_RT_DWORD,NULL,&dw,&sdw);
            
                qDebug() << key << dw;
            }
            
            F Offline
            F Offline
            Frankkon
            wrote on 10 Jul 2017, 06:57 last edited by
            #5

            @hskoglund Thank you! Yes, the problom is “\” , I create a key like "aaa/bbb/ccc", QSettings can work well on it. Thanks a lot.

            1 Reply Last reply
            0

            3/5

            8 Jul 2017, 08:12

            • Login

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