Thread: Working with the registry

  1. #1
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    Working with the registry

    I'm trying to add a value to the registry if it doesn't already exist, but I'm a little confused. Will this work?

    Code:
    see message below
    Last edited by neandrake; 12-04-2003 at 07:30 PM.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    ok, so I worked on the code a little. No errors/crashes, but it doesn't work.

    Code:
    	HKEY tmpKey = NULL;
    	if (RegOpenKey(HKEY_CLASSES_ROOT, "\\*\\shellex\\ContextMenuHandler\\SHA1-Hash", &tmpKey) != ERROR_SUCCESS)
    	{
    		char *key = "\"C:\\Program Files\\SHA1Hash\\hash.exe" \"%1\"";
    		RegCreateKey(HKEY_CLASSES_ROOT, "\\*\\shellex\\ContextMenuHandler\\SHA1-Hash", &tmpKey);
    		RegCreateKey(HKEY_CLASSES_ROOT, "\\*\\shellex\\ContextMenuHandler\\SHA1-Hash\\command", &tmpKey);
    		RegSetValue(HKEY_CLASSES_ROOT, "\\*\\shellex\\SHA1-Hash\\command", REG_SZ, key, (DWORD)strlen(key));
    	}
    It doesn't even create the keys
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    You might consider checking the return code of RegCreateKey, and RegSetValue, and if they fail call GetLastError(), etc.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    First, I think this belongs on the Windows board.

    >>The RegCreateKeyEx function creates the specified registry key. If the key already exists, the function opens it.<<

    So you don't need the RegOpenKey stuff.

    When you open regedit the nodes on the left are keys. The items on the right are values.

    Therefore, 'command' is a value and can not be created using RegCreateKey. It is created with the call to RegSetValue.

    RegSetValue takes, as its first argument, the key where the value will reside. In this case, that is the key returned by RegCreateKey.

    So if you want to create the key/value "HKCR\\*\\shellex\\ContextMenuHandler\\SHA1-Hash\\command" = key then this is what you come up with:

    Code:
    HKEY tmpKey = NULL;
    const char * REG_KEY_NAME = "\\*\\shellex\\ContextMenuHandler\\SHA1-Hash";
    const char * REG_VALUE_NAME = "command";
    char *key = "\"C:\\Program Files\\SHA1Hash\\hash.exe" \"%1\"";
    
    RegCreateKeyEx(HKEY_CLASSES_ROOT, REG_KEY_NAME, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, &tmpKey, NULL);
    RegSetValueEx(tmpKey, REG_VALUE_NAME, 0, REG_SZ, key, strlen(key));
    We have done this topic a number of times on the windows board so do a search.

    Also note that you will only be able to write to this part of the registry when logged on as an administrator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with the registry
    By neandrake in forum Windows Programming
    Replies: 1
    Last Post: 03-03-2004, 06:41 PM
  2. Registry
    By gvector1 in forum C# Programming
    Replies: 0
    Last Post: 07-30-2003, 04:02 PM
  3. how to edit the registry
    By jverkoey in forum Windows Programming
    Replies: 3
    Last Post: 03-28-2003, 04:20 AM
  4. Registry
    By sean345 in forum Windows Programming
    Replies: 4
    Last Post: 07-15-2002, 01:24 PM
  5. LOGFONT struct & Registry :: MFC
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 05-27-2002, 03:38 PM