Windows registry loading and saving tip: application hives and the RegLoadAppKey API function

Posted: (EET/GMT+2)

 

Adding a couple of new keys to the system registry isn't usually a problem, and can be easily made from C#/.NET and native code Win32 applications. However, sometimes you might need dozens of keys and values, so you might start to think about a more efficient approach than creating all those keys and values one by one.

The Windows Registry API supports something called application hives. Application hives work like a "private copy" of the system registry, and can be either loaded or saved at will. However, application hives are stored on disk, meaning that they don't contribute to the size or complexity of the system registry. Support for application hives has been available since Windows Vista and Windows Server 2008. To load an application hive, you'd use the RegLoadAppKey function in the Win32 API. This works well in situations where you need to maintain a complex set (hundreds, perhaps thousands of keys and values) quickly.

Note that the RegLoadAppKey function is not listed in the regular list of registry API functions. Thus, you might miss this function altogether if you don't know about it beforehand.

Hope this helps!