Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problems reading from registry

Problems reading from registry

Scheduled Pinned Locked Moved C / C++ / MFC
windows-adminhelptutorialquestion
5 Posts 3 Posters 0 Views 1 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.
  • E Offline
    E Offline
    esapp420
    wrote on last edited by
    #1

    I am not able to get a string value from the registry. I have looked over all the posts regarding this and have not found a solution. Here is my code:

    HKEY hKey = NULL;
    CHAR szBuf[100];
    LPDWORD lpdwLength;
    CString strReturn;

    ::RegCreateKeyEx(HKEY_CLASSES_ROOT, "CLSID", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL, &hKey, NULL);
    ::RegQueryValueEx(hKey, "E1", 0, 0, (LPBYTE)szBuf, lpdwLength);
    ::RegCloseKey(hKey);

    The value of szBuf after this runs is garbage, but the value is definately in the registry. Does anyone have a code example?? MSDNs code example link for RegQueryValueEx() doesnt link to code (go figure...) but rather to an explanation that is of no help. Nish?? You out there??? HELP!!! :confused: Thanks guys and gals. Is it football season yet?!?!?!?

    L L 2 Replies Last reply
    0
    • E esapp420

      I am not able to get a string value from the registry. I have looked over all the posts regarding this and have not found a solution. Here is my code:

      HKEY hKey = NULL;
      CHAR szBuf[100];
      LPDWORD lpdwLength;
      CString strReturn;

      ::RegCreateKeyEx(HKEY_CLASSES_ROOT, "CLSID", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL, &hKey, NULL);
      ::RegQueryValueEx(hKey, "E1", 0, 0, (LPBYTE)szBuf, lpdwLength);
      ::RegCloseKey(hKey);

      The value of szBuf after this runs is garbage, but the value is definately in the registry. Does anyone have a code example?? MSDNs code example link for RegQueryValueEx() doesnt link to code (go figure...) but rather to an explanation that is of no help. Nish?? You out there??? HELP!!! :confused: Thanks guys and gals. Is it football season yet?!?!?!?

      L Offline
      L Offline
      Le centriste
      wrote on last edited by
      #2

      A comment: ALWAYS check the return value from a function that returns one. Ex:

      HKEY hKey = NULL;
      CHAR szBuf[100];
      LPDWORD lpdwLength;
      CString strReturn;
      LONG lResult = ::RegCreateKeyEx(HKEY_CLASSES_ROOT, "CLSID", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL, &hKey, NULL);
      lResult = ::RegQueryValueEx(hKey, "E1", 0, 0, (LPBYTE)szBuf, lpdwLength);
      ::RegCloseKey(hKey);

      by knowing the return value, you will know what is going wrong. Michel It is a lovely language, but it takes a very long time to say anything in it, because we do not say anything in it, unless it is worth taking a very long time to say, and to listen to.
      - TreeBeard

      1 Reply Last reply
      0
      • E esapp420

        I am not able to get a string value from the registry. I have looked over all the posts regarding this and have not found a solution. Here is my code:

        HKEY hKey = NULL;
        CHAR szBuf[100];
        LPDWORD lpdwLength;
        CString strReturn;

        ::RegCreateKeyEx(HKEY_CLASSES_ROOT, "CLSID", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL, &hKey, NULL);
        ::RegQueryValueEx(hKey, "E1", 0, 0, (LPBYTE)szBuf, lpdwLength);
        ::RegCloseKey(hKey);

        The value of szBuf after this runs is garbage, but the value is definately in the registry. Does anyone have a code example?? MSDNs code example link for RegQueryValueEx() doesnt link to code (go figure...) but rather to an explanation that is of no help. Nish?? You out there??? HELP!!! :confused: Thanks guys and gals. Is it football season yet?!?!?!?

        L Offline
        L Offline
        Len Holgate
        wrote on last edited by
        #3

        lpdwLength should be the address of a dword, not an uninitialised pointer pointing into randomness. The value in that dword is size of the buffer you're passing in, or 0 if you're not passing a buffer. After the call it's the size of the buffer required (or the size of the buffer that was used (same thing)). It kinda says all that in the MSDN docs. So, something like this might work (beware, it hasn't seen a compiler). Or you can try a wrapper class, like the one I wrote about here.

        HKEY hKey = NULL

        long ret = ::RegCreateKeyEx(
        HKEY_CLASSES_ROOT,
        "CLSID",
        0,
        NULL,
        REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE,
        NULL,
        &hKey,
        NULL);

        if (ret == ERROR_SUCCESS)
        {
        CHAR szBuf[100];
        DWORD dwLength = sizeof(szBuf);

        ret = ::RegQueryValueEx(hKey, "E1", 0, 0, (LPBYTE)szBuf, &dwLength);

        if (ret == ERROR_SUCCESS)
        {
        // got a value...
        }

        ::RegCloseKey(hKey);
        }
        else
        {
        // error, deal with it.
        }

        Len Holgate www.jetbyte.com The right code, right now.

        E 1 Reply Last reply
        0
        • L Len Holgate

          lpdwLength should be the address of a dword, not an uninitialised pointer pointing into randomness. The value in that dword is size of the buffer you're passing in, or 0 if you're not passing a buffer. After the call it's the size of the buffer required (or the size of the buffer that was used (same thing)). It kinda says all that in the MSDN docs. So, something like this might work (beware, it hasn't seen a compiler). Or you can try a wrapper class, like the one I wrote about here.

          HKEY hKey = NULL

          long ret = ::RegCreateKeyEx(
          HKEY_CLASSES_ROOT,
          "CLSID",
          0,
          NULL,
          REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE,
          NULL,
          &hKey,
          NULL);

          if (ret == ERROR_SUCCESS)
          {
          CHAR szBuf[100];
          DWORD dwLength = sizeof(szBuf);

          ret = ::RegQueryValueEx(hKey, "E1", 0, 0, (LPBYTE)szBuf, &dwLength);

          if (ret == ERROR_SUCCESS)
          {
          // got a value...
          }

          ::RegCloseKey(hKey);
          }
          else
          {
          // error, deal with it.
          }

          Len Holgate www.jetbyte.com The right code, right now.

          E Offline
          E Offline
          esapp420
          wrote on last edited by
          #4

          Len, Thanks for the advice. I am still learning how to decipher the cryptic documentation that is MSDN... Anyways, I tried this and now get "access violation reading location ..." during the query. The access specifier is set to

          KEY_READ | KEY_WRITE | KEY_QUERY_VALUE

          Any suggestions???

          L 1 Reply Last reply
          0
          • E esapp420

            Len, Thanks for the advice. I am still learning how to decipher the cryptic documentation that is MSDN... Anyways, I tried this and now get "access violation reading location ..." during the query. The access specifier is set to

            KEY_READ | KEY_WRITE | KEY_QUERY_VALUE

            Any suggestions???

            L Offline
            L Offline
            Len Holgate
            wrote on last edited by
            #5

            Try using KEY_ALL_ACCESS? Len Holgate www.jetbyte.com The right code, right now.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups