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,

Community
  • 1
  • 1
Panch
  • 1,097
  • 3
  • 12
  • 43
  • Windows error code 2 is ERROR_FILE_NOT_FOUND. Are you sure that your registry key exists? – Mohamad Elghawi Oct 30 '15 at 09: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). – Mohamad Elghawi Oct 30 '15 at 09: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 '15 at 09:40
  • I had downloaded the procmon from https://technet.microsoft.com/en-us/sysinternals/bb896645. – Panch Oct 30 '15 at 09: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? – Mohamad Elghawi Oct 30 '15 at 12:24
  • When I checked the registry path with the assistance of procmon I observed it as HKEY_LOCAL_MACHINE\SOFTWARE\TEMP. – Panch Nov 02 '15 at 01:46

1 Answers1

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.

Panch
  • 1,097
  • 3
  • 12
  • 43