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
Printable View
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
ok, so I worked on the code a little. No errors/crashes, but it doesn't work.
It doesn't even create the keysCode: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));
}
You might consider checking the return code of RegCreateKey, and RegSetValue, and if they fail call GetLastError(), etc.
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:
We have done this topic a number of times on the windows board so do a search.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));
Also note that you will only be able to write to this part of the registry when logged on as an administrator.