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. Registry

Registry

Scheduled Pinned Locked Moved C / C++ / MFC
windows-admintutorial
3 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.
  • U Offline
    U Offline
    udayGovekar
    wrote on last edited by
    #1

    Hi All, Can anyone please explain me on how to write a variable(integer type) into the registry and also how to read it back Thanks Uday

    T S 2 Replies Last reply
    0
    • U udayGovekar

      Hi All, Can anyone please explain me on how to write a variable(integer type) into the registry and also how to read it back Thanks Uday

      T Offline
      T Offline
      Tomasz Sowinski
      wrote on last edited by
      #2

      Check the API function with names starting with Reg prefix, like RegSetValueEx. If you're using MFC, you can also use CWinApp::WriteProfileXXX methods. Tomasz Sowinski -- http://www.shooltz.com

      "Yields falsehood when preceded by its quotation" yields falsehood when preceded by its quotation.

      1 Reply Last reply
      0
      • U udayGovekar

        Hi All, Can anyone please explain me on how to write a variable(integer type) into the registry and also how to read it back Thanks Uday

        S Offline
        S Offline
        Steven Gregg
        wrote on last edited by
        #3

        Following code isn't perfect but it works and should get you started. If you look at the documentation for RegSetValueEx() you can set the dwType param to REG_DWORD for numbers but I usually just deal with strings and use atoi() or similar to convert. bool bGetRegistryEntry(const HKEY hKey, const char* name, unsigned char* const dest) { bool bReturnVal = true; unsigned long lType = 0; unsigned long ulSize = MAX_PATH; if (ERROR_SUCCESS != RegQueryValueEx(hKey, name, NULL, &lType, dest, &ulSize)) { bReturnVal = false; } return (bReturnVal); } bool bSetRegistryEntry(const HKEY hKey, const char* name, unsigned char const* dest) { bool bReturnVal = true; unsigned long lType = 0; LONG lRESULT = 0; lRESULT = RegSetValueEx(hKey, name, 0, REG_SZ, dest, _mbslen(dest) + 1); //ERROR_ACCESS_DENIED indicates it already exists if ((lRESULT != ERROR_SUCCESS) && (lRESULT != ERROR_ACCESS_DENIED)) { bReturnVal = false; } return (bReturnVal); } // Open or create registry. void vRegOpenKey() { HKEY hKey=0; //char *my_App = "Registry_Example"; CString sAppId = "SOFTWARE\\RegKeyExample"; bool bReturnVal = false; DWORD dwDisposition = 0; if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, sAppId, 0, KEY_READ, &hKey)) { LONG lRESULT = 0; lRESULT = RegCreateKeyEx(HKEY_LOCAL_MACHINE, sAppId, 0, REG_NONE, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition); if (lRESULT != ERROR_SUCCESS) { MessageBox(NULL, "Registry not available!", NULL, NULL); return; } } const unsigned char name[5] = "this"; if (!bSetRegistryEntry(hKey, "value1", &name[0])) { MessageBox(NULL, "Unable to set registry value1", NULL, NULL); } else { unsigned char name2[5]; if (bGetRegistryEntry(hKey, "value1", name2)) { CString sText; sText.Format("Registry value1 is %s", name2); MessageBox(NULL, sText, NULL, NULL); } else { MessageBox(NULL, "Unable to get registry value1", NULL, NULL); } } RegCloseKey( hKey ); }

        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