title: How to read a REG_BINARY value from registry where installations of 32bit applications are found, but on a 64bit OS using C#
body: <p>I want to get the value of this particular node in the registry using C# on a Win7 64bit machine:</p>
  
  <p><img src="https://i.stack.imgur.com/YCcJr.png" alt="enter image description here"></p>
  
  <p>This value resides in the registry where installations of 32bit applications store their information. The problem I am facing is not reaching the node but returning a meaningful value as array (string or int). </p>
  
  <p>The code I am using is shown below and is adapted from </p>
  
  <p><a href="http://social.msdn.microsoft.com/Forums/vstudio/en-US/378491d6-23a3-4ae7-a702-c52c5abb0e8d/access-to-both-32-and-64-bit-registry-using-c-and-regmultisz?forum=csharpgeneral" rel="nofollow noreferrer">http://social.msdn.microsoft.com/Forums/vstudio/en-US/378491d6-23a3-4ae7-a702-c52c5abb0e8d/access-to-both-32-and-64-bit-registry-using-c-and-regmultisz?forum=csharpgeneral</a></p>
  
  <p>and</p>
  
  <p><a href="http://www.rhyous.com/2011/01/24/how-read-the-64-bit-registry-from-a-32-bit-application-or-vice-versa/" rel="nofollow noreferrer">http://www.rhyous.com/2011/01/24/how-read-the-64-bit-registry-from-a-32-bit-application-or-vice-versa/</a></p>
  
  <p>So my listing is this:</p>
  
  <pre><code>public static UIntPtr HKEY_LOCAL_MACHINE = new UIntPtr(0x80000002u);
  
  [DllImport("Advapi32.dll")]
  public static extern int RegQueryValueEx(int hKey, string lpValueName, int lpReserved, ref uint lpType, byte[] lpData, ref uint lpcbData);
  
  public enum RegSAM
  {
      QueryValue = 0x0001,
      SetValue = 0x0002,
      CreateSubKey = 0x0004,
      EnumerateSubKeys = 0x0008,
      Notify = 0x0010,
      CreateLink = 0x0020,
      WOW64_32Key = 0x0200,
      WOW64_64Key = 0x0100,
      WOW64_Res = 0x0300,
      Read = 0x00020019,
      Write = 0x00020006,
      Execute = 0x00020019,
      AllAccess = 0x000f003f
  }
  
  string path = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion";
  
  RegistryKey rkTest = Registry.LocalMachine.OpenSubKey(path);
  try
  {   
      int hkey = 0;
      uint lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, (int)RegSAM.QueryValue | (int)RegSAM.WOW64_32Key, out hkey);
      if (0 != lResult) hkey = 0;
      else
      {
          uint lpType = 0;
          uint lpcbData = 1024;
          byte[] Buffer = new Byte[1024];
          RegQueryValueEx(hkey, "DigitalProductID", 0, ref lpType, Buffer, ref lpcbData);
          ... at this point the buffer returns empty.
      }
  }
  catch (Exception exc) { };
  </code></pre>
  
  <p>but my when I execute it my buffer is empty.</p>
  
  <p><img src="https://i.stack.imgur.com/rVlKa.png" alt="enter image description here"></p>
  
  <p>The code that I have provided here can reach that particular location the registry shown in the top image and read normal string values if I replace byte[] with the StringBuilder class, provided the value in the registry is of type REG_SZ. But what can we do if it is of type REG_BINARY?</p>
  
  <p>Could somebody point out to me how can I achieve this?</p>
  
  <p>PS:</p>
  
  <p>Just to show that I have been working on a similar issue for a few days now I have successfully converted a REG_BINARY value into a readable array of integers from the registry using the code snippet below, but only if that value is under the WOW6432 node. </p>
  
  <pre><code>byte[] bytes = (byte[])rkTest.GetValue("DigitalProductID");
  string s = Convert.ToBase64String(bytes);
  List&lt;int&gt; int_array = new List&lt;int&gt;();
  int temp_int = 0, i = 0;
  for (i = 0; i &lt; bytes.Length; i++) { temp_int = bytes[i]; int_array.Add(temp_int); }
  </code></pre>
  
  <p>The problem I want to solve here is for that area in the registry where 32bit applications stored their information in REG_BINARY type.</p>
  
  <p>Thank you.</p>