0

I need to read key values in HKEY_LOCAL_MACHINE\SOFTWARE from my application. I use the following snippet to do that but it always returns 2.

std::wstring strTmp;
strTmp = L"SOFTWARE\\TEMP";
RegOpenKeyExW(HKEY_LOCAL_MACHINE, strTmp.c_str(), 0, KEY_READ, &hKey))

But when I use HKEY_CURRENT_USER then I'm able to read information successfully from HKEY_CURRENT_USER\SOFTWARE. I understand that my application is unable to read from HKEY_LOCAL_MACHINE due to access privileges. I did run my application with administrator privileges (using Run As) but that did not help me out.

Can someone shed some light on how can I access the HKEY_LOCAL_MACHINE using RegOpenKeyExW.

I did go through the links 1, 2 but that did not help me out,

6
  • Windows error code 2 is ERROR_FILE_NOT_FOUND. Are you sure that your registry key exists? Oct 30, 2015 at 9:26
  • 1
    Your code also looks to be missing an open flag. Use KEY_WOW64_64KEY to access the 64 bit view or KEY_WOW64_32KEY to access the 32 bit view (i.e keys under Wow6432Node). Oct 30, 2015 at 9:31
  • @ Mohamad Elghawi Yes the Registry key exists. But still it returns 2. Also I monitored the registry path using Procmon, and it displays the right path when this call is made but windows says file not found. Also as you advised I had tried with flag KEY_WOW64_64KEY combined with KEY_READ but no luck.
    – Panch
    Oct 30, 2015 at 9:40
  • I had downloaded the procmon from technet.microsoft.com/en-us/sysinternals/bb896645.
    – Panch
    Oct 30, 2015 at 9:47
  • What is the full path to your key. Is it HKEY_LOCAL_MACHINE\SOFTWARE\TEMP or is it HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Temp? Oct 30, 2015 at 12:24

1 Answer 1

2

My apologies for my misunderstandings. Actually I had to use KEY_WOW64_32KEY flag instead of KEY_WOW64_64KEY in RegOpenKeyExW(HKEY_LOCAL_MACHINE, strTmp.c_str(), 0, KEY_READ | KEY_WOW64_32KEY, &hKey)) to get my application working. Thanks to Mohamad Elghawi.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.