Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.


UPDATE:

The project being modified was originally developed with C++Builder 12.1 Athens. I updated that project to 12.2 by creating a new project and adding all of the original forms to it. I still get the same result. I kept working on it and found the following:

TStringList *subkeys = new TStringList(); 
pRegistry->GetKeyNames(subkeys); 
UnicodeString RALPH = subkeys->Strings[0]; <--- this fails 
UnicodeString ALICE = subkeys->Strings[1][1]; <--- this also fails 
wchar_t NORTON = subkeys->Strings[0][1]; <--- this works 
wchar_t TRIXIE = RALPH[1]; <--- this also worked!!! 

If I try to use any of the UnicodeStrings, I get NULL values.

Since I'm working with single character keys, I can work around it, but it's certainly not right.

like image 765
Kevin Avatar asked Oct 23 '25 16:10

Kevin


1 Answers

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.

like image 196
Remy Lebeau Avatar answered Oct 26 '25 06:10

Remy Lebeau