/// <summary>
 /// Queries a key from the VREG, or if not exists from the RREG.
 /// </summary>
 /// <remarks>
 /// Documentation from MSDN:
 /// If the function succeeds, the return value is ERROR_SUCCESS.
 /// If the lpData buffer is too small to receive the data, the function returns ERROR_MORE_DATA.
 /// If the lpValueName registry value does not exist, the function returns ERROR_FILE_NOT_FOUND.
 /// </remarks>
 /// <param name="hKey"></param>
 /// <param name="lpValueName">The name of the registry value.</param>
 /// <param name="lpReserved">This parameter is reserved and must be NULL.</param>
 /// <param name="lpType">
 /// A pointer to a variable that receives a code indicating the type of data
 /// stored in the specified value. For a list of the possible type codes, see Registry Value Types.
 /// The lpType parameter can be NULL if the type code is not required.
 /// </param>
 /// <param name="lpData">
 /// A pointer to a buffer that receives the value's data.
 /// This parameter can be NULL if the data is not required.
 /// </param>
 /// <param name="lpcbData">
 /// A pointer to a variable that specifies the size of the buffer pointed to by the lpData parameter,
 /// in bytes. When the function returns, this variable contains the size of the data copied to lpData.
 /// The lpcbData parameter can be NULL only if lpData is NULL.
 /// </param>
 /// <returns></returns>
 public NativeResultCode QueryValue(UIntPtr hKey, [MarshalAs(UnmanagedType.LPWStr)] string lpValueName,
                                              IntPtr lpReserved, IntPtr lpType, IntPtr lpData, IntPtr lpcbData)
 {
   // BUG: If lpValueName is NULL or an empty string, the function retrieves the type and data for the key's unnamed or default value, if any.
   if (string.IsNullOrEmpty(lpValueName))
     return NativeResultCode.FileNotFound;
   uint handle;
   if (!TryParse(hKey, out handle))
     return NativeResultCode.InvalidHandle;
   using (EngineCore.Engine.GetEngineProcessingSpace())
   {
     VirtualRegistryValue virtualRegistryValue;
     var resultCode = _registry.QueryValue(handle, lpValueName, out virtualRegistryValue);
     EngineCore.Log.Debug("QueryValue(HKey={0} ValueName={1}) => {2}",
                         handle, lpValueName, resultCode);
     if (resultCode != NativeResultCode.Success)
       return resultCode;
     // Marshal all data to the specified pointers.
     if (lpType != IntPtr.Zero)
       lpType.Write(virtualRegistryValue.Type);
     if (lpcbData != IntPtr.Zero)
     {
       if (virtualRegistryValue.Data.Length > lpcbData.Read<uint>())
         return NativeResultCode.MoreData;
       if (lpData != IntPtr.Zero) // Guest might only need length
         lpData.Write(virtualRegistryValue.Data);
       lpcbData.Write(virtualRegistryValue.Data.Length);
     }
     return NativeResultCode.Success;
   }
 }