Sign in to follow this  

Problems writing a registry key and value

This topic is 4223 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

I wrote this code based off the MSDN but it doesn't make a key. It doesn't return an error, it doesn't really do anything.
cSecurityKey::cSecurityKey(){
    HKEY securityKey;
    key = 0;

    
    if(!key){
        key = rand();
        
        if(RegCreateKeyEx(HKEY_LOCAL_MACHINE, TEXT("SYSTEM\\SOFTWARE\\MyProgram\\"), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &securityKey, NULL) != ERROR_SUCCESS){
            cout<<"Server could not save security key in registry."<<endl;
            
            char buffer[40];
            
            itoa( (int)key, buffer, 10);
            
            DWORD len = sizeof(buffer);
            
            if(RegSetValueEx(securityKey, "SecurityKey", 0, REG_EXPAND_SZ, (BYTE*)buffer, len) != ERROR_SUCCESS){
                cout<<"Server could not save security key value in registry."<<endl;
            }

            
            RegCloseKey(securityKey);
        }
    }
}

I even tried making the MyProgram key in software to see if it would put a value in it, but it didn't. Also, while it doesn't really warrent a seperate thread, my random numbers are not very random. There always like 8XXX, where only the last three numbers are random. I am calling srand(time(NULL)) before I call rand().

Share this post


Link to post
Share on other sites

  • Probably because the RegSetValueEx section of the code only executes if RegCreateKeyEx returns something other than ERROR_SUCCESS (check your use of closing braces for "if(RegCreateKeyEx(..."):
    if(RegCreateKeyEx(HKEY_LOCAL_MACHINE, TEXT("SYSTEM\\SOFTWARE\\MyProgram\\"), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &securityKey, NULL) != ERROR_SUCCESS){
    cout<<"Server could not save security key in registry."<<endl;
    // perhaps a closing brace here?

  • rand() only returns numbers in the range [0, RAND_MAX] (RAND_MAX defined in <cstdlib>). If you require a more robust source of entropy, consider boost::random.

And, just because i'm bored :P :

  • Consider replacing itoa, a non-standard function in itself, with a more C++ solution, say, std::ostringstream.

  • Try using expceptions so that you can actually tell when a registry operation fails, and can handle the failure. This also stops a 'zombie' cSecurityKey object (one without a valid state [registered key value]) being created.


Here is a edited version :P

#include <sstream>
#include <cstdio>

cSecurityKey::cSecurityKey()
{
key = rand(); // or boost::random

HKEY securityKey;
if(RegCreateKeyEx(HKEY_LOCAL_MACHINE, TEXT("SYSTEM\\SOFTWARE\\MyProgram\\"), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &securityKey, NULL) != ERROR_SUCCESS)
throw std::string("Server could not save security key in registry.");

std::ostringstream os;
os << key;

if(RegSetValueEx(securityKey, "SecurityKey", 0, REG_EXPAND_SZ, static_cast<BYTE *>(os.str().c_str()), os.str().size()) != ERROR_SUCCESS)
throw std::string("Server could not save security key value in registry.");

RegCloseKey(securityKey);
}


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
in addition to what mattd said, from the MSDN: "An application cannot create a key under HKEY_USERS or HKEY_LOCAL_MACHINE."

also get rid of the extra final back slash and set the final parm to a pointer to a valid var rather than as NULL.

Share this post


Link to post
Share on other sites

This topic is 4223 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this