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
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
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)); }
Environment: OS X, GCC / G++
Codes: Java, C#, C/C++
AOL IM: neandrake, Email: neandrake (at) gmail (dot) com
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.
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.