Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: TRegistry open a subkey to the open key



Permlink Replies: 2 - Last Post: Mar 10, 2017 3:31 AM Last Post By: Asger Joergensen Threads: [ Previous | Next ]
Asger Joergensen

Posts: 370
Registered: 11/18/08
TRegistry open a subkey to the open key
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 9, 2017 3:02 PM
Hi

What is the right way to open a subkey relative to an already open key

   AppRootKey = L"Software\\CompanyName\\AppName";
   SubKey =  L"Software\\CompanyName\\AppName\\Subkey";
 
   std::auto_ptr<TRegistry> Reges( new TRegistry(KEY_READ ) );
   TRegistry* Reg = Reges.get();
 
   Reg->RootKey = HKEY_CURRENT_USER;
   bool Res = Reg->OpenKey( AppRootKey , false );
   if( ! Res )return;
 
   //  read some values
 
   // option 1.
   Reg->CloseKey();
   Res = Reg->OpenKey( SubKey, false );
   if( ! Res )return;
 
   //  read some values
 
   // option 2.  
   Res = Reg->OpenKey( L"\\Subkey", false );
   if( ! Res )return;
 
   //  read some values 


Thanks in advance
Best regards
Asger
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: TRegistry open a subkey to the open key
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 9, 2017 3:22 PM   in response to: Asger Joergensen in response to: Asger Joergensen
Asger wrote:

What is the right way to open a subkey relative to an already open key

Simply call TRegistry::OpenKey() again with the desired subkey. DO NOT prefix
it with "
" (that makes it an absolute key path):

AppRootKey = L"\\Software\\CompanyName\\AppName";
SubKey = L"Subkey";
 
bool Res = Reg->OpenKey( AppRootKey, false );
if( ! Res )return;
//  read some values
 
Res = Reg->OpenKey( SubKey, false );
if( ! Res )return;
//  read some values 


Or, you CAN use an abolute path instead, if you want:
 

AppRootKey = L"
Software
CompanyName
AppName";
SubKey = AppRootKey + L"
Subkey";

bool Res = Reg->OpenKey( AppRootKey, false );
if( ! Res )return;
// read some values

Res = Reg->OpenKey( SubKey, false );
if( ! Res )return;
// read some values


Either way works fine. OpenKey() opens the new key first, so you can use
a relative or absolute key path, and only if the open is successful will
it then close the old key.

--
Remy Lebeau (TeamB)
Asger Joergensen

Posts: 370
Registered: 11/18/08
Re: TRegistry open a subkey to the open key
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 10, 2017 3:31 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Hi Remy

Thanks for the help.
Best regards
Asger
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02