Archived

This topic is now archived and is closed to further replies.

Help setting Registry key

This topic is 5277 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In a game I am writing, the user is able to set various options. I want to be able to save those options in the registry so that the next time they play the game, the selected options will be loaded automatically. I'm able to read the registry no problem. But on game exit, when I try to save options, the registry key does not appear to be updated. Here is the code I'm using:
HKEY Open = 0;
  DWORD Created;
  DWORD Result;

  //Attempt to open the Law Enforcement registry key

  Result = RegOpenKeyEx(HKEY_CURRENT_USER, 
                        "LaserShot\\Law Enforce", 
                        0, 
                        KEY_READ,
                        &Open);
  if(Result == ERROR_SUCCESS)
  {
    //Set the registry subkey value to the current size adjustment

    RegSetValueEx(Open, 
                  "SizeAdjustment", 
                  0, 
                  REG_DWORD, 
                  (BYTE *)&SizeAdjustment, 
                  sizeof(DWORD));
  }
  else
  {
    //Registry key does not exist.  Create it.

    Result = RegCreateKeyEx(HKEY_CURRENT_USER,
                            "LaserShot\\Law Enforce",
                            0,
                            "SizeAdjustment",
                            REG_OPTION_NON_VOLATILE,
                            KEY_ALL_ACCESS,
                            NULL,
                            &Open,
                            &Created);
    if(Result == ERROR_SUCCESS)
    {
      RegSetValueEx(Open,
                    "SizeAdjustment",
                    0,
                    REG_DWORD,
                    (BYTE *)&SizeAdjustment,
                    sizeof(DWORD));
    }
  }

  //Close registry key

  if(Open != 0)
  {
    RegCloseKey(Open);
  }
I've debugged it and stepped through it, and it looks like it executes just fine. However, when I check the registry, the value has not been changed--I still see the original value. Any ideas what I might be doing wrong? Thanks in advance!

Share this post


Link to post
Share on other sites
Interesting--I went back through and checked the return value of RegSetValueEx, and it came back as 5L (Access Denied).

That seems odd, because I''m logged on as the administrator on this machine. I''m able to go into the registry and set the value manually, no problem. Any ideas?

Share this post


Link to post
Share on other sites
*SMACK SELF*

Oh, duh! Thanks for pointing that out.

EDIT: Yep, that did the trick. Thanks for pointing out my dumb mistake.

[edited by - Teric on July 8, 2003 10:30:03 AM]

Share this post


Link to post
Share on other sites