Enumerating registry values from a key
Multi hours were wasted here because this is about as error prone of an API as you could have. You set
max_name_length and
max_val_length to tell Windows how big a result you can handle. Fine. But the API then writes back the size it actually needed in those variables. Then, the next time around those values are likely too small, leading to an
ERROR_MORE_DATA return value. Sigh.
In this example I've got a dedicated key with values with meaninglesss value names...I just want the value values, each stuffed into an element in a 2D array.
Note that I believe you need to have the 2 index vars as shown, because of the way Windows stores these things.
my_roots[0][0] = 0;
LONG result = ERROR_SUCCESS;
while (result != ERROR_NO_MORE_ITEMS)
{
max_name_length = 10;
max_val_length = MAX_PATH;
result =
RegEnumValue(dirKeyHandle, reg_index, this_name, &max_name_length, NULL, &type, (unsigned char*)&my_roots[array_index][0], &max_val_length);
reg_index++;
if (result == ERROR_SUCCESS)
{ array_index++;
}
}
A generic example
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
char keyName[] = "SOFTWARE\\SomeKey";
DWORD whatHappened; // designates whether key created & opened or existing key opened
/* open key, create new one if non-existent
*/
result = RegCreateKeyEx(HKEY_LOCAL_MACHINE, keyName, 0, "dontcare", REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS, 0, &keyHandle, &whatHappened);
if (result != ERROR_SUCCESS)
{
MessageBox(NULL,"Can't make registry changes", "Hosed", MB_OK);
return 1;
}
/* Set a DWORD value
*/
DWORD theData = 0x01;
result = RegSetValueEx(keyHandle, "ValueName", 0, REG_DWORD, (LPBYTE)&theData, 4);
if (result != ERROR_SUCCESS)
{
MessageBox(NULL,"Can't make registry changes", "Hosed", MB_OK);
return 1;
}
RegCloseKey(keyHandle);
return 0;
}
--
MattWalsh - 08 Nov 2002