/// <summary> /// This method will insert a value into the VREG. /// </summary> /// <param name="hKey">A handle to an open registry key.</param> /// <param name="lpValueName"> /// The name of the value to be set. If a value with this name is not already present in the key, /// the function adds it to the key. /// If lpValueName is NULL or an empty string, "", the function sets the type and data for /// the key's unnamed or default value. /// </param> /// <param name="reserved">This parameter is reserved and must be zero.</param> /// <param name="dwType">The type of data pointed to by the <paramref name="lpData"/> parameter.</param> /// <param name="lpData">The data to be stored.</param> /// <param name="cbData">The size of the information pointed to by the lpData parameter, in bytes.</param> /// <returns>A WinError code.</returns> public NativeResultCode SetValueEx(UIntPtr hKey, [MarshalAs(UnmanagedType.LPWStr)] string lpValueName, uint reserved, ValueType dwType, IntPtr lpData, uint cbData) { uint handle; if (!TryParse(hKey, out handle)) return NativeResultCode.InvalidHandle; using (EngineCore.Engine.GetEngineProcessingSpace()) { var data = lpData.Read<byte[]>(cbData); var registryValue = new VirtualRegistryValue(lpValueName, data, dwType); var resultCode = _registry.SetValue(handle, registryValue); EngineCore.Log.Debug("SetValue(HKey={0} Name={1} Type={2}) => {3}", handle, lpValueName, dwType, resultCode); return resultCode; } }
/// <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; } }