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. problem reading string from registry

problem reading string from registry

Scheduled Pinned Locked Moved C / C++ / MFC
helpwindows-admintutorialquestionannouncement
5 Posts 4 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.
  • L Offline
    L Offline
    Lucky the code machine
    wrote on last edited by
    #1

    Problem with reading a string from the registry (example windows version). I tryed this and it worked fine: char* text; //unsigned long* buffer; DWORD* buffer; unsigned long PerServer; unsigned long Per1_0Server; CRegKey reg; reg.Open(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"); reg.QueryValue(PerServer,"MaxConnectionsPerServer"); but when i try to read a string (the windows version name) with: reg.Open(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion"); reg.QueryValue(text,"ProductName",buffer); i get an assertation error on the line "pdwCount!=null;" (in CRegKey) if i change buffer to anything using "buffer=(unsigned long*)999;" i dont get the error but i get no data. Am i being stupid? I cant see where im going wrong. An Expert is somone who has previously made ALL the Mistakes, I dream of this day. - Lucky

    C R L 3 Replies Last reply
    0
    • L Lucky the code machine

      Problem with reading a string from the registry (example windows version). I tryed this and it worked fine: char* text; //unsigned long* buffer; DWORD* buffer; unsigned long PerServer; unsigned long Per1_0Server; CRegKey reg; reg.Open(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"); reg.QueryValue(PerServer,"MaxConnectionsPerServer"); but when i try to read a string (the windows version name) with: reg.Open(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion"); reg.QueryValue(text,"ProductName",buffer); i get an assertation error on the line "pdwCount!=null;" (in CRegKey) if i change buffer to anything using "buffer=(unsigned long*)999;" i dont get the error but i get no data. Am i being stupid? I cant see where im going wrong. An Expert is somone who has previously made ALL the Mistakes, I dream of this day. - Lucky

      C Offline
      C Offline
      Claudius Mokler
      wrote on last edited by
      #2

      To use the CRegKey::QueryValue(LPTSTR szValue, LPCTSTR lpszValueName, DWORD* pdwCount) overload, you'll have to have an allocated (or static) buffer for szValue and the size of the buffer in a variable pointed to by pdwCount before calling QueryValue() // here's the maximum length of our text buffer #define TEXTLENGTH 120 // that's the text buffer itself char text[TEXTLENGTH]; // here's a variable used both to tell QueryValue about the // buffer's length and to get QueryValue's length answer DWORD textlength; // the registry key object CRegKey reg; // stuff omitted (... snip ...) // initialize with maximum length of text buffer textlength = TEXTLENGTH; // query the registry key reg.QueryValue(text, "ProductName", &textlength); // now textlength is changed, it contains the actual length // of the text retrieved from the registry.

      L 1 Reply Last reply
      0
      • L Lucky the code machine

        Problem with reading a string from the registry (example windows version). I tryed this and it worked fine: char* text; //unsigned long* buffer; DWORD* buffer; unsigned long PerServer; unsigned long Per1_0Server; CRegKey reg; reg.Open(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"); reg.QueryValue(PerServer,"MaxConnectionsPerServer"); but when i try to read a string (the windows version name) with: reg.Open(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion"); reg.QueryValue(text,"ProductName",buffer); i get an assertation error on the line "pdwCount!=null;" (in CRegKey) if i change buffer to anything using "buffer=(unsigned long*)999;" i dont get the error but i get no data. Am i being stupid? I cant see where im going wrong. An Expert is somone who has previously made ALL the Mistakes, I dream of this day. - Lucky

        R Offline
        R Offline
        Renjith Ramachandran
        wrote on last edited by
        #3

        ALWAYS USE API s.............THEY ARE COOOOOOOOOOOOOL EXAMPLE IS: HKEY hkey; DWORD dwSize; char szInstalledapth[MAX_PATH*2] if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Renjith\\",0,KEY_READ,&hkey)!=ERROR_SUCCESS) { MessageBox(HWND_DESKTOP,"Unable To oen registry","Registry Error",MB_ICONERROR); return FALSE; } //Reading a String dwSize=MAX_PATH; if(RegQueryValueEx(hkey,"InstalledPath",0,NULL,(LPBYTE) szInstalledapth,&dwSize)!=ERROR_SUCCESS) { RegCloseKey(hkey); return FALSE; } //////////READING A DWORD DWORD dwLogLevel; dwSize=sizeof(DWORD); if(RegQueryValueEx(hkey,"LogLevel",0,NULL,(unsigned char*)&dwLogLevel,&dwSize)!=ERROR_SUCCESS) { RegCloseKey(hkey); return FALSE; } //CLOSING THE REGISTRY if(hkey) RegCloseKey(hkey); R_Renjith The True CP ian

        1 Reply Last reply
        0
        • C Claudius Mokler

          To use the CRegKey::QueryValue(LPTSTR szValue, LPCTSTR lpszValueName, DWORD* pdwCount) overload, you'll have to have an allocated (or static) buffer for szValue and the size of the buffer in a variable pointed to by pdwCount before calling QueryValue() // here's the maximum length of our text buffer #define TEXTLENGTH 120 // that's the text buffer itself char text[TEXTLENGTH]; // here's a variable used both to tell QueryValue about the // buffer's length and to get QueryValue's length answer DWORD textlength; // the registry key object CRegKey reg; // stuff omitted (... snip ...) // initialize with maximum length of text buffer textlength = TEXTLENGTH; // query the registry key reg.QueryValue(text, "ProductName", &textlength); // now textlength is changed, it contains the actual length // of the text retrieved from the registry.

          L Offline
          L Offline
          Lucky the code machine
          wrote on last edited by
          #4

          Thanks, that got it going first time. thank you again - Lucky :) An Expert is somone who has previously made ALL the Mistakes, I dream of this day. - Lucky

          1 Reply Last reply
          0
          • L Lucky the code machine

            Problem with reading a string from the registry (example windows version). I tryed this and it worked fine: char* text; //unsigned long* buffer; DWORD* buffer; unsigned long PerServer; unsigned long Per1_0Server; CRegKey reg; reg.Open(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"); reg.QueryValue(PerServer,"MaxConnectionsPerServer"); but when i try to read a string (the windows version name) with: reg.Open(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion"); reg.QueryValue(text,"ProductName",buffer); i get an assertation error on the line "pdwCount!=null;" (in CRegKey) if i change buffer to anything using "buffer=(unsigned long*)999;" i dont get the error but i get no data. Am i being stupid? I cant see where im going wrong. An Expert is somone who has previously made ALL the Mistakes, I dream of this day. - Lucky

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

            Lucky the code machine wrote: char* text; //unsigned long* buffer; DWORD* buffer; unsigned long PerServer; unsigned long Per1_0Server; CRegKey reg; You don't initialize your variables. do this instead:

            TCHAR text[1024]; //May be any length suitable for the value
            //unsigned long* buffer;
            DWORD buffer = 1024;
            unsigned long PerServer;
            unsigned long Per1_0Server;
            CRegKey reg;

            reg.Open(HKEY_LOCAL_MACHINE,_T("Software\\Microsoft\\Windows\\CurrentVersion"));
            reg.QueryValue(text,_T("ProductName"),&buffer);

            Often, when a method asks for a pointer to value, it asks for a placeholder to return something to you. but the this pointer must point to valid memory area. Also, note use of TCHAR and _T(""). 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
            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