17 November 2014

...get your hive on: LESSONS LEARNED accessing registry values for other users IN CODE

Problem
You want to read and/or update registry for a user other than yourself in using an application.

Solved
I cannot take credit or create the nth entry for this well documented solution.

The interwebs tells you how to do so using Windows API advapi32.dll, follow the yellow brick road HERE.

In this post I just want to share what I learned while implementing the API code.

Lessons Learned:
RegLoadKey returns STATUS 1314 [ERROR_PRIVILEGE_NOT_HELD]:
Application must run with Admin access/rights

RegLoadKey returns STATUS 32 [ERROR_SHARING_VIOLATION]:
Application cannot load the Hive file while the target user is logged on


RegUnLoadKey returns STATUS 5 [ERROR_ACCESS_DENIED]:
Registry Key must be closed before unloading Hive file. I combined the advapi32 API code with Microsoft.Win32.RegistryKey code, I had to execute RegistryKey.Close before unloading file.

Not there yet:
So my initial user registry update solution using hive load had the following issues in the production environment:
·         Alternating between [ERROR_PRIVILEGE_NOT_HELD] and [ERROR_SHARING_VIOLATION] depending on user logged on when the application executed
·         Hogging and sometimes corrupting the user hive

I downgraded my solution to a simpler design as per the next blog entry.



Note: Useful page with System Error Codes can be found HERE, know your errors and conquer them…