CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2007
    Posts
    8

    Reading from registry?? (a question)

    hi all
    I searched google on how reading from registry and i have found this code

    Code:
    int main()
    {
      char buffer[500];
      unsigned long size = sizeof(buffer);
    
      DWORD type;
      HKEY hKey;
    
      RegCreateKey(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Visual Basic 5.0",&hKey);
      RegQueryValueEx(hKey,"Version",NULL,&type,(LPBYTE)buffer,&size);
      RegCloseKey(hKey);
    
      cout << buffer << endl;
      system("pause");
      return 0;
    }
    but i have a question, what if the size of the value in the registry key is bigger than 500 ?!!!
    is there's a way to fix this problem without making the size of buffer 9999999999 lol

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Reading from registry?? (a question)

    [ moved thread ]
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Reading from registry?? (a question)

    You shouldn't store such big data in the registry.
    If your data exceeds a certain size, it's much better to write it to some file.

    Anyway, you can create your buffer dynamically.
    First call RegQueryValueEx with lpData set to NULL. Then the required size will be returned in the size variable. Then allocate your dynamic buffer with new and call RegQueryValueEx with your buffer. Don't forget to call delete on your buffer once your done with it.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Reading from registry?? (a question)

    Beside that Marc G already stated.
    • It has no sense to "open" a key using RegCreateKey when you need it for reading a value. If the user is not an administrator, most possible that function will fail.
    • You have to check the returned values from RegXX functions to prevent your program going in the binary heaven (or hell)
    • After calling first time RegQueryValueEx with lpData set to NULL, verify if the type value is indeed REG_SZ and test size against a "paranoid" but of common sense limit (which can be enough for string values like "Version").
      Never know... a wise guy can put some (huge) garbage in the registry and again, your program can go in the binary...
    Last edited by ovidiucucu; June 2nd, 2007 at 03:44 PM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  5. #5
    Join Date
    May 2007
    Posts
    8

    Re: Reading from registry?? (a question)

    Quote Originally Posted by ovidiucucu
    Beside that Marc G already stated.
    • It has no sense to "open" a key using RegCreateKey when you need it for reading a value. If the user is not an administrator, most possible that function will fail.
    thx for the help..So what should i use?
    RegOpenKey?!

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Reading from registry?? (a question)

    Yup! (you should only use Create when you want to...well...ummm...Create)
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  7. #7
    Join Date
    May 2007
    Posts
    8

    Re: Reading from registry?? (a question)

    Quote Originally Posted by TheCPUWizard
    Yup! (you should only use Create when you want to...well...ummm...Create)
    lol..thx alot

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured