c++registryc++buildervcl

TRegistry GetKeyNames not working as expected


I am trying to use the following code to get a list of subkeys in the registry. GetKeyNames takes a TString object. Upon return, the TStringList object has a count of 3 which is the correct count. However, while the TStringList has the proper count, it doesn't seem to have the names. This is probably something simple, but I haven't been able to discover the problem.

TRegistry *pRegistry = new TRegistry(KEY_READ);
pRegistry->RootKey   = HKEY_LOCAL_MACHINE;
pRegistry->OpenKeyReadOnly(L"\\SOFTWARE\\Ogre\\Fasthole");

TStringList *subkeyNames = new TStringList();
pRegistry->GetKeyNames(subkeyNames);

UnicodeString ALICE  = subkeyNames->Names[0];

ALICE is always NULL.


Solution

  • You need to use the TStringList's Strings[] property instead of its Names[] property:

    //UnicodeString ALICE  = subkeyNames->Names[0];
    UnicodeString ALICE  = subkeyNames->Strings[0];
    

    TRegistry::GetKeyNames() returns the key names as-is, not in "name=value" format, which is why the Names[] property is returning a blank string - there is no name for it to return:

    When the list of strings for the TStrings object includes strings that are name-value pairs, read Names to access the name part of a string. Names is the name part of the string at Index, where 0 is the first string, 1 is the second string, and so on. If the string is not a name-value pair, Names contains an empty string.