Implement RegQueryMultipleValuesA().
[reactos.git] / reactos / lib / advapi32 / reg / reg.c
index e8fd8e3..4e8e98c 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: reg.c,v 1.41 2003/12/28 23:22:30 arty Exp $
+/* $Id: reg.c,v 1.61 2004/10/10 10:43:23 ekohl Exp $
  *
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS system libraries
 
 /* INCLUDES *****************************************************************/
 
-#define NTOS_MODE_USER
-#include <ntos.h>
-#include <ddk/ntddk.h>
-#include <rosrtl/string.h>
-#include <ntdll/rtl.h>
-#include <windows.h>
-#include <wchar.h>
-
+#include "advapi32.h"
 #define NDEBUG
 #include <debug.h>
 
@@ -27,7 +20,7 @@
 
 #define MAX_DEFAULT_HANDLES   6
 #define REG_MAX_NAME_SIZE     256
-#define REG_MAX_DATA_SIZE     2048     
+#define REG_MAX_DATA_SIZE     2048
 
 /* GLOBALS ******************************************************************/
 
@@ -37,7 +30,7 @@ static HANDLE ProcessHeap;
 
 /* PROTOTYPES ***************************************************************/
 
-static NTSTATUS MapDefaultKey (PHKEY ParentKey, HKEY Key);
+static NTSTATUS MapDefaultKey (PHANDLE ParentKey, HKEY Key);
 static VOID CloseDefaultKeys(VOID);
 
 static NTSTATUS OpenClassesRootKey(PHANDLE KeyHandle);
@@ -81,7 +74,7 @@ RegCleanup (VOID)
 
 
 static NTSTATUS
-MapDefaultKey (PHKEY RealKey,
+MapDefaultKey (PHANDLE RealKey,
               HKEY Key)
 {
   PHANDLE Handle;
@@ -92,7 +85,7 @@ MapDefaultKey (PHKEY RealKey,
 
   if (((ULONG)Key & 0xF0000000) != 0x80000000)
     {
-      *RealKey = Key;
+      *RealKey = (HANDLE)Key;
       return STATUS_SUCCESS;
     }
 
@@ -148,7 +141,7 @@ MapDefaultKey (PHKEY RealKey,
 
   if (NT_SUCCESS(Status))
     {
-      *RealKey = (HKEY)*Handle;
+      *RealKey = *Handle;
     }
 
    return Status;
@@ -191,7 +184,6 @@ OpenClassesRootKey (PHANDLE KeyHandle)
                    &Attributes);
 }
 
-#undef NDEBUG
 
 static NTSTATUS
 OpenLocalMachineKey (PHANDLE KeyHandle)
@@ -215,7 +207,6 @@ OpenLocalMachineKey (PHANDLE KeyHandle)
   return Status;
 }
 
-#define NDEBUG
 
 static NTSTATUS
 OpenUsersKey (PHANDLE KeyHandle)
@@ -315,6 +306,115 @@ RegConnectRegistryW (LPCWSTR lpMachineName,
 }
 
 
+/************************************************************************
+ *  CreateNestedKey
+ *
+ *  Create key and all necessary intermediate keys
+ */
+static NTSTATUS
+CreateNestedKey(PHKEY KeyHandle,
+               POBJECT_ATTRIBUTES ObjectAttributes,
+                PUNICODE_STRING ClassString,
+                DWORD dwOptions,
+                REGSAM samDesired,
+                DWORD *lpdwDisposition)
+{
+  OBJECT_ATTRIBUTES LocalObjectAttributes;
+  UNICODE_STRING LocalKeyName;
+  ULONG Disposition;
+  NTSTATUS Status;
+  ULONG FullNameLength;
+  ULONG Length;
+  PWCHAR Ptr;
+  HANDLE LocalKeyHandle;
+
+  Status = NtCreateKey((PHANDLE) KeyHandle,
+                       samDesired,
+                       ObjectAttributes,
+                       0,
+                       ClassString,
+                       dwOptions,
+                       (PULONG)lpdwDisposition);
+  DPRINT("NtCreateKey(%wZ) called (Status %lx)\n", ObjectAttributes->ObjectName, Status);
+  if (Status != STATUS_OBJECT_NAME_NOT_FOUND)
+    return Status;
+
+  /* Copy object attributes */
+  RtlCopyMemory (&LocalObjectAttributes,
+                ObjectAttributes,
+                sizeof(OBJECT_ATTRIBUTES));
+  RtlCreateUnicodeString (&LocalKeyName,
+                         ObjectAttributes->ObjectName->Buffer);
+  LocalObjectAttributes.ObjectName = &LocalKeyName;
+  FullNameLength = LocalKeyName.Length / sizeof(WCHAR);
+
+  /* Remove the last part of the key name and try to create the key again. */
+  while (Status == STATUS_OBJECT_NAME_NOT_FOUND)
+    {
+      Ptr = wcsrchr (LocalKeyName.Buffer, '\\');
+      if (Ptr == NULL || Ptr == LocalKeyName.Buffer)
+       {
+         Status = STATUS_UNSUCCESSFUL;
+         break;
+       }
+      *Ptr = (WCHAR)0;
+      LocalKeyName.Length = wcslen (LocalKeyName.Buffer) * sizeof(WCHAR);
+
+      Status = NtCreateKey (&LocalKeyHandle,
+                           KEY_ALL_ACCESS,
+                           &LocalObjectAttributes,
+                           0,
+                           NULL,
+                           0,
+                           &Disposition);
+      DPRINT("NtCreateKey(%wZ) called (Status %lx)\n", &LocalKeyName, Status);
+    }
+
+  if (!NT_SUCCESS(Status))
+    {
+      RtlFreeUnicodeString (&LocalKeyName);
+      return Status;
+    }
+
+  /* Add removed parts of the key name and create them too. */
+  Length = wcslen (LocalKeyName.Buffer);
+  while (TRUE)
+    {
+      NtClose (LocalKeyHandle);
+
+      LocalKeyName.Buffer[Length] = L'\\';
+      Length = wcslen (LocalKeyName.Buffer);
+      LocalKeyName.Length = Length * sizeof(WCHAR);
+
+      if (Length == FullNameLength)
+        {
+          Status = NtCreateKey((PHANDLE) KeyHandle,
+                               samDesired,
+                               ObjectAttributes,
+                               0,
+                               ClassString,
+                               dwOptions,
+                               (PULONG)lpdwDisposition);
+          break;
+        }
+      Status = NtCreateKey (&LocalKeyHandle,
+                           KEY_ALL_ACCESS,
+                           &LocalObjectAttributes,
+                           0,
+                           NULL,
+                           0,
+                           &Disposition);
+      DPRINT("NtCreateKey(%wZ) called (Status %lx)\n", &LocalKeyName, Status);
+      if (!NT_SUCCESS(Status))
+       break;
+    }
+
+  RtlFreeUnicodeString (&LocalKeyName);
+
+  return Status;
+}
+
+
 /************************************************************************
  *  RegCreateKeyExA
  *
@@ -334,7 +434,7 @@ RegCreateKeyExA (HKEY hKey,
   UNICODE_STRING SubKeyString;
   UNICODE_STRING ClassString;
   OBJECT_ATTRIBUTES Attributes;
-  HKEY ParentKey;
+  HANDLE ParentKey;
   LONG ErrorCode;
   NTSTATUS Status;
 
@@ -356,25 +456,26 @@ RegCreateKeyExA (HKEY hKey,
       RtlCreateUnicodeStringFromAsciiz (&ClassString,
                                        lpClass);
     }
-  RtlCreateUnicodeStringFromAsciiz (&SubKeyString,
-                                   (LPSTR)lpSubKey);
+
+  RtlCreateUnicodeStringFromAsciiz(&SubKeyString,
+                                  (LPSTR)lpSubKey);
   InitializeObjectAttributes (&Attributes,
                              &SubKeyString,
                              OBJ_CASE_INSENSITIVE,
                              (HANDLE)ParentKey,
                              (PSECURITY_DESCRIPTOR)lpSecurityAttributes);
-  Status = NtCreateKey (phkResult,
-                       samDesired,
-                       &Attributes,
-                       0,
-                       (lpClass == NULL)? NULL : &ClassString,
-                       dwOptions,
-                       (PULONG)lpdwDisposition);
+  Status = CreateNestedKey(phkResult,
+                          &Attributes,
+                          (lpClass == NULL)? NULL : &ClassString,
+                          dwOptions,
+                          samDesired,
+                          lpdwDisposition);
   RtlFreeUnicodeString (&SubKeyString);
   if (lpClass != NULL)
     {
       RtlFreeUnicodeString (&ClassString);
     }
+
   DPRINT("Status %x\n", Status);
   if (!NT_SUCCESS(Status))
     {
@@ -393,20 +494,20 @@ RegCreateKeyExA (HKEY hKey,
  * @implemented
  */
 LONG STDCALL
-RegCreateKeyExW(HKEY hKey,
-    LPCWSTR   lpSubKey,
-    DWORD     Reserved,
-    LPWSTR    lpClass,
-    DWORD     dwOptions,
-    REGSAM    samDesired,
-    LPSECURITY_ATTRIBUTES lpSecurityAttributes,
-    PHKEY     phkResult,
-    LPDWORD   lpdwDisposition)
+RegCreateKeyExW (HKEY hKey,
+                LPCWSTR lpSubKey,
+                DWORD Reserved,
+                LPWSTR lpClass,
+                DWORD dwOptions,
+                REGSAM samDesired,
+                LPSECURITY_ATTRIBUTES lpSecurityAttributes,
+                PHKEY phkResult,
+                LPDWORD lpdwDisposition)
 {
   UNICODE_STRING SubKeyString;
   UNICODE_STRING ClassString;
   OBJECT_ATTRIBUTES Attributes;
-  HKEY ParentKey;
+  HANDLE ParentKey;
   LONG ErrorCode;
   NTSTATUS Status;
 
@@ -432,13 +533,12 @@ RegCreateKeyExW(HKEY hKey,
                              OBJ_CASE_INSENSITIVE,
                              (HANDLE)ParentKey,
                              (PSECURITY_DESCRIPTOR)lpSecurityAttributes);
-  Status = NtCreateKey (phkResult,
-                       samDesired,
-                       &Attributes,
-                       0,
-                       (lpClass == NULL)? NULL : &ClassString,
-                       dwOptions,
-                       (PULONG)lpdwDisposition);
+  Status = CreateNestedKey(phkResult,
+                          &Attributes,
+                           (lpClass == NULL)? NULL : &ClassString,
+                           dwOptions,
+                           samDesired,
+                           lpdwDisposition);
   DPRINT("Status %x\n", Status);
   if (!NT_SUCCESS(Status))
     {
@@ -506,7 +606,7 @@ RegDeleteKeyA (HKEY hKey,
 {
   OBJECT_ATTRIBUTES ObjectAttributes;
   UNICODE_STRING SubKeyName;
-  HKEY ParentKey;
+  HANDLE ParentKey;
   HANDLE TargetKey;
   NTSTATUS Status;
   LONG ErrorCode;
@@ -563,7 +663,7 @@ RegDeleteKeyW (HKEY hKey,
 {
   OBJECT_ATTRIBUTES ObjectAttributes;
   UNICODE_STRING SubKeyName;
-  HKEY ParentKey;
+  HANDLE ParentKey;
   HANDLE TargetKey;
   NTSTATUS Status;
   LONG ErrorCode;
@@ -617,7 +717,7 @@ RegDeleteValueA (HKEY hKey,
                 LPCSTR lpValueName)
 {
   UNICODE_STRING ValueName;
-  HKEY KeyHandle;
+  HANDLE KeyHandle;
   LONG ErrorCode;
   NTSTATUS Status;
 
@@ -658,7 +758,7 @@ RegDeleteValueW (HKEY hKey,
   UNICODE_STRING ValueName;
   NTSTATUS Status;
   LONG ErrorCode;
-  HKEY KeyHandle;
+  HANDLE KeyHandle;
 
   Status = MapDefaultKey (&KeyHandle,
                          hKey);
@@ -760,10 +860,10 @@ RegEnumKeyExA (HKEY hKey,
   ANSI_STRING StringA;
   LONG ErrorCode = ERROR_SUCCESS;
   DWORD NameLength;
-  DWORD ClassLength;
+  DWORD ClassLength = 0;
   DWORD BufferSize;
   DWORD ResultSize;
-  HKEY KeyHandle;
+  HANDLE KeyHandle;
   NTSTATUS Status;
 
   DPRINT("RegEnumKeyExA(hKey 0x%x, dwIndex %d, lpName 0x%x, *lpcbName %d, lpClass 0x%x, lpcbClass %d)\n",
@@ -774,6 +874,7 @@ RegEnumKeyExA (HKEY hKey,
       SetLastError (ERROR_INVALID_PARAMETER);
       return ERROR_INVALID_PARAMETER;
     }
+
   Status = MapDefaultKey(&KeyHandle,
                         hKey);
   if (!NT_SUCCESS(Status))
@@ -791,16 +892,18 @@ RegEnumKeyExA (HKEY hKey,
     {
       NameLength = 0;
     }
+
   if (lpClass)
     {
       if (*lpcbClass > 0)
-        {
-          ClassLength = min (*lpcbClass -1, REG_MAX_NAME_SIZE) * sizeof(WCHAR);
+       {
+         ClassLength = min (*lpcbClass -1, REG_MAX_NAME_SIZE) * sizeof(WCHAR);
        }
       else
-        {
+       {
          ClassLength = 0;
        }
+
       /* The class name should start at a dword boundary */
       BufferSize = ((sizeof(KEY_NODE_INFORMATION) + NameLength + 3) & ~3) + ClassLength;
     }
@@ -808,11 +911,10 @@ RegEnumKeyExA (HKEY hKey,
     {
       BufferSize = sizeof(KEY_BASIC_INFORMATION) + NameLength;
     }
-  
+
   KeyInfo = RtlAllocateHeap (ProcessHeap,
                             0,
                             BufferSize);
-  
   if (KeyInfo == NULL)
     {
       SetLastError (ERROR_OUTOFMEMORY);
@@ -833,7 +935,7 @@ RegEnumKeyExA (HKEY hKey,
   else
     {
       if (lpClass == NULL)
-        {
+       {
          if (KeyInfo->Basic.NameLength > NameLength)
            {
              ErrorCode = ERROR_BUFFER_OVERFLOW;
@@ -846,7 +948,7 @@ RegEnumKeyExA (HKEY hKey,
            }
        }
       else
-        {
+       {
          if (KeyInfo->Node.NameLength > NameLength ||
              KeyInfo->Node.ClassLength > ClassLength)
            {
@@ -854,13 +956,13 @@ RegEnumKeyExA (HKEY hKey,
            }
          else
            {
-              StringA.Buffer = lpClass;
+             StringA.Buffer = lpClass;
              StringA.Length = 0;
              StringA.MaximumLength = *lpcbClass;
              StringU.Buffer = (PWCHAR)((ULONG_PTR)KeyInfo->Node.Name + KeyInfo->Node.ClassOffset);
              StringU.Length = KeyInfo->Node.ClassLength;
              StringU.MaximumLength = KeyInfo->Node.ClassLength;
-              RtlUnicodeStringToAnsiString (&StringA, &StringU, FALSE);
+             RtlUnicodeStringToAnsiString (&StringA, &StringU, FALSE);
              lpClass[StringA.Length] = 0;
              *lpcbClass = StringA.Length;
              StringU.Buffer = KeyInfo->Node.Name;
@@ -868,26 +970,27 @@ RegEnumKeyExA (HKEY hKey,
              StringU.MaximumLength = KeyInfo->Node.NameLength;
            }
        }
+
       if (ErrorCode == ERROR_SUCCESS)
-        {
-          StringA.Buffer = lpName;
+       {
+         StringA.Buffer = lpName;
          StringA.Length = 0;
          StringA.MaximumLength = *lpcbName;
-          RtlUnicodeStringToAnsiString (&StringA, &StringU, FALSE);
+         RtlUnicodeStringToAnsiString (&StringA, &StringU, FALSE);
          lpName[StringA.Length] = 0;
          *lpcbName = StringA.Length;
          if (lpftLastWriteTime != NULL)
            {
              if (lpClass == NULL)
-               {
-                 lpftLastWriteTime->dwLowDateTime = KeyInfo->Basic.LastWriteTime.u.LowPart;
-                 lpftLastWriteTime->dwHighDateTime = KeyInfo->Basic.LastWriteTime.u.HighPart;
-               }
+               {
+                 lpftLastWriteTime->dwLowDateTime = KeyInfo->Basic.LastWriteTime.u.LowPart;
+                 lpftLastWriteTime->dwHighDateTime = KeyInfo->Basic.LastWriteTime.u.HighPart;
+               }
              else
-               {
-                 lpftLastWriteTime->dwLowDateTime = KeyInfo->Node.LastWriteTime.u.LowPart;
-                 lpftLastWriteTime->dwHighDateTime = KeyInfo->Node.LastWriteTime.u.HighPart;
-               }
+               {
+                 lpftLastWriteTime->dwLowDateTime = KeyInfo->Node.LastWriteTime.u.LowPart;
+                 lpftLastWriteTime->dwHighDateTime = KeyInfo->Node.LastWriteTime.u.HighPart;
+               }
            }
        }
     }
@@ -934,8 +1037,8 @@ RegEnumKeyExW (HKEY hKey,
   ULONG BufferSize;
   ULONG ResultSize;
   ULONG NameLength;
-  ULONG ClassLength;
-  HKEY KeyHandle;
+  ULONG ClassLength = 0;
+  HANDLE KeyHandle;
   LONG ErrorCode = ERROR_SUCCESS;
   NTSTATUS Status;
 
@@ -947,6 +1050,7 @@ RegEnumKeyExW (HKEY hKey,
       SetLastError (ErrorCode);
       return ErrorCode;
     }
+
   if (*lpcbName > 0)
     {
       NameLength = min (*lpcbName - 1, REG_MAX_NAME_SIZE) * sizeof (WCHAR);
@@ -955,22 +1059,25 @@ RegEnumKeyExW (HKEY hKey,
     {
       NameLength = 0;
     }
+
   if (lpClass)
     {
       if (*lpcbClass > 0)
-        {
-          ClassLength = min (*lpcbClass - 1, REG_MAX_NAME_SIZE) * sizeof(WCHAR);
+       {
+         ClassLength = min (*lpcbClass - 1, REG_MAX_NAME_SIZE) * sizeof(WCHAR);
        }
       else
-        {
+       {
          ClassLength = 0;
        }
+
       BufferSize = ((sizeof(KEY_NODE_INFORMATION) + NameLength + 3) & ~3) + ClassLength;
     }
   else
     {
       BufferSize = sizeof(KEY_BASIC_INFORMATION) + NameLength;
     }
+
   KeyInfo = RtlAllocateHeap (ProcessHeap,
                             0,
                             BufferSize);
@@ -994,7 +1101,7 @@ RegEnumKeyExW (HKEY hKey,
   else
     {
       if (lpClass == NULL)
-        {
+       {
          if (KeyInfo->Basic.NameLength > NameLength)
            {
              ErrorCode = ERROR_BUFFER_OVERFLOW;
@@ -1009,7 +1116,7 @@ RegEnumKeyExW (HKEY hKey,
            }
        }
       else
-        {
+       {
          if (KeyInfo->Node.NameLength > NameLength ||
              KeyInfo->Node.ClassLength > ClassLength)
            {
@@ -1018,7 +1125,7 @@ RegEnumKeyExW (HKEY hKey,
          else
            {
              RtlCopyMemory (lpName,
-                            KeyInfo->Node.Name,
+                            KeyInfo->Node.Name,
                             KeyInfo->Node.NameLength);
              *lpcbName = KeyInfo->Node.NameLength / sizeof(WCHAR);
              lpName[*lpcbName] = 0;
@@ -1029,6 +1136,7 @@ RegEnumKeyExW (HKEY hKey,
              lpClass[*lpcbClass] = 0;
            }
        }
+
       if (ErrorCode == ERROR_SUCCESS && lpftLastWriteTime != NULL)
        {
          if (lpClass == NULL)
@@ -1052,6 +1160,7 @@ RegEnumKeyExW (HKEY hKey,
     {
       SetLastError(ErrorCode);
     }
+
   return ErrorCode;
 }
 
@@ -1079,9 +1188,9 @@ RegEnumValueA (HKEY hKey,
 
   ULONG NameLength;
   ULONG BufferSize;
-  ULONG DataLength;
+  ULONG DataLength = 0;
   ULONG ResultSize;
-  HKEY KeyHandle;
+  HANDLE KeyHandle;
   LONG ErrorCode;
   NTSTATUS Status;
   UNICODE_STRING StringU;
@@ -1107,6 +1216,7 @@ RegEnumValueA (HKEY hKey,
     {
       NameLength = 0;
     }
+
   if (lpData)
     {
       DataLength = min (*lpcbData * sizeof(WCHAR), REG_MAX_DATA_SIZE);
@@ -1118,7 +1228,7 @@ RegEnumValueA (HKEY hKey,
     }
 
   ValueInfo = RtlAllocateHeap (ProcessHeap,
-                               0,
+                              0,
                               BufferSize);
   if (ValueInfo == NULL)
     {
@@ -1142,8 +1252,8 @@ RegEnumValueA (HKEY hKey,
     {
       if (lpData)
         {
-          IsStringType = (ValueInfo->Full.Type == REG_SZ) || 
-                        (ValueInfo->Full.Type == REG_MULTI_SZ) || 
+         IsStringType = (ValueInfo->Full.Type == REG_SZ) ||
+                        (ValueInfo->Full.Type == REG_MULTI_SZ) ||
                         (ValueInfo->Full.Type == REG_EXPAND_SZ);
          if (ValueInfo->Full.NameLength > NameLength ||
              (!IsStringType && ValueInfo->Full.DataLength > *lpcbData) ||
@@ -1161,45 +1271,47 @@ RegEnumValueA (HKEY hKey,
                  StringA.Buffer = (PCHAR)lpData;
                  StringA.Length = 0;
                  StringA.MaximumLength = *lpcbData;
-                 RtlUnicodeStringToAnsiString (&StringA,
-                                               &StringU,
-                                               FALSE);
-                 *lpcbData = StringA.Length;
+                 RtlUnicodeStringToAnsiString (&StringA,
+                                               &StringU,
+                                               FALSE);
+                 *lpcbData = StringA.Length;
                }
              else
-               {
-                 RtlCopyMemory(lpData,
-                               (PVOID)((ULONG_PTR)ValueInfo + ValueInfo->Full.DataOffset),
-                               ValueInfo->Full.DataLength);
+               {
+                 RtlCopyMemory (lpData,
+                                (PVOID)((ULONG_PTR)ValueInfo + ValueInfo->Full.DataOffset),
+                                ValueInfo->Full.DataLength);
                  *lpcbData = ValueInfo->Full.DataLength;
                }
-              StringU.Buffer = ValueInfo->Full.Name;
+
+             StringU.Buffer = ValueInfo->Full.Name;
              StringU.Length = ValueInfo->Full.NameLength;
              StringU.MaximumLength = NameLength;
            }
        }
       else
-        {
+       {
          if (ValueInfo->Basic.NameLength > NameLength)
            {
              ErrorCode = ERROR_BUFFER_OVERFLOW;
            }
          else
            {
-              StringU.Buffer = ValueInfo->Basic.Name;
+             StringU.Buffer = ValueInfo->Basic.Name;
              StringU.Length = ValueInfo->Basic.NameLength;
              StringU.MaximumLength = NameLength;
            }
        }
+
       if (ErrorCode == ERROR_SUCCESS)
         {
          StringA.Buffer = (PCHAR)lpValueName;
          StringA.Length = 0;
          StringA.MaximumLength = *lpcbValueName;
          RtlUnicodeStringToAnsiString (&StringA,
-                                       &StringU,
+                                       &StringU,
                                        FALSE);
-          StringA.Buffer[StringA.Length] = 0;
+         StringA.Buffer[StringA.Length] = 0;
          *lpcbValueName = StringA.Length;
          if (lpType)
            {
@@ -1207,13 +1319,15 @@ RegEnumValueA (HKEY hKey,
            }
        }
     }
+
   RtlFreeHeap (ProcessHeap,
-               0,
+              0,
               ValueInfo);
   if (ErrorCode != ERROR_SUCCESS)
     {
       SetLastError(ErrorCode);
     }
+
   return ErrorCode;
 }
 
@@ -1241,12 +1355,12 @@ RegEnumValueW (HKEY hKey,
 
   ULONG NameLength;
   ULONG BufferSize;
-  ULONG DataLength;
+  ULONG DataLength = 0;
   ULONG ResultSize;
-  HKEY KeyHandle;
+  HANDLE KeyHandle;
   LONG ErrorCode;
   NTSTATUS Status;
-    
+
   ErrorCode = ERROR_SUCCESS;
 
   Status = MapDefaultKey (&KeyHandle,
@@ -1257,7 +1371,7 @@ RegEnumValueW (HKEY hKey,
       SetLastError (ErrorCode);
       return ErrorCode;
     }
-  
+
   if (*lpcbValueName > 0)
     {
       NameLength = min (*lpcbValueName - 1, REG_MAX_NAME_SIZE) * sizeof(WCHAR);
@@ -1266,6 +1380,7 @@ RegEnumValueW (HKEY hKey,
     {
       NameLength = 0;
     }
+
   if (lpData)
     {
       DataLength = min(*lpcbData, REG_MAX_DATA_SIZE);
@@ -1298,13 +1413,13 @@ RegEnumValueW (HKEY hKey,
   else
     {
       if (lpData)
-        {
-          if (ValueInfo->Full.DataLength > DataLength ||
+       {
+         if (ValueInfo->Full.DataLength > DataLength ||
              ValueInfo->Full.NameLength > NameLength)
-            {
+           {
              ErrorCode = ERROR_BUFFER_OVERFLOW;
            }
-          else
+         else
            {
              RtlCopyMemory (lpValueName,
                             ValueInfo->Full.Name,
@@ -1318,7 +1433,7 @@ RegEnumValueW (HKEY hKey,
            }
        }
       else
-        {
+       {
          if (ValueInfo->Basic.NameLength > NameLength)
            {
              ErrorCode = ERROR_BUFFER_OVERFLOW;
@@ -1331,19 +1446,27 @@ RegEnumValueW (HKEY hKey,
              *lpcbValueName = (DWORD)(ValueInfo->Basic.NameLength / sizeof(WCHAR));
              lpValueName[*lpcbValueName] = 0;
            }
+         if (NULL != lpcbData)
+           {
+             *lpcbData = (DWORD)ValueInfo->Full.DataLength;
+           }
        }
+
       if (ErrorCode == ERROR_SUCCESS && lpType != NULL)
        {
          *lpType = lpData ? ValueInfo->Full.Type : ValueInfo->Basic.Type;
        }
     }
+
   RtlFreeHeap (ProcessHeap,
               0,
               ValueInfo);
+
   if (ErrorCode != ERROR_SUCCESS)
     {
-      SetLastError(ErrorCode);
+      SetLastError (ErrorCode);
     }
+
   return ErrorCode;
 }
 
@@ -1356,7 +1479,7 @@ RegEnumValueW (HKEY hKey,
 LONG STDCALL
 RegFlushKey(HKEY hKey)
 {
-  HKEY KeyHandle;
+  HANDLE KeyHandle;
   LONG ErrorCode;
   NTSTATUS Status;
 
@@ -1389,47 +1512,48 @@ RegFlushKey(HKEY hKey)
 /************************************************************************
  *  RegGetKeySecurity
  *
- * @unimplemented
+ * @implemented
  */
 LONG STDCALL
-RegGetKeySecurity (HKEY hKey,
-                  SECURITY_INFORMATION SecurityInformation,
-                  PSECURITY_DESCRIPTOR pSecurityDescriptor,
-                  LPDWORD lpcbSecurityDescriptor)
+RegGetKeySecurity(HKEY hKey,
+                 SECURITY_INFORMATION SecurityInformation,
+                 PSECURITY_DESCRIPTOR pSecurityDescriptor,
+                 LPDWORD lpcbSecurityDescriptor)
 {
-#if 0
-  HKEY KeyHandle;
+  HANDLE KeyHandle;
   LONG ErrorCode;
   NTSTATUS Status;
 
-  if (hKey = HKEY_PERFORMANCE_DATA)
+  if (hKey == HKEY_PERFORMANCE_DATA)
     {
+      SetLastError(ERROR_INVALID_HANDLE);
       return ERROR_INVALID_HANDLE;
     }
 
-  Status = MapDefaultKey (&KeyHandle,
-                         hKey);
+  Status = MapDefaultKey(&KeyHandle,
+                        hKey);
   if (!NT_SUCCESS(Status))
     {
-      ErrorCode = RtlNtStatusToDosError (Status);
-      SetLastError (ErrorCode);
+      DPRINT("MapDefaultKey() failed (Status %lx)\n", Status);
+      ErrorCode = RtlNtStatusToDosError(Status);
+      SetLastError(ErrorCode);
       return ErrorCode;
     }
 
-  Status = NtQuerySecurityObject ()
+  Status = NtQuerySecurityObject(KeyHandle,
+                                SecurityInformation,
+                                pSecurityDescriptor,
+                                *lpcbSecurityDescriptor,
+                                lpcbSecurityDescriptor);
   if (!NT_SUCCESS(Status))
     {
-      ErrorCode = RtlNtStatusToDosError (Status);
-      SetLastError (ErrorCode);
+      DPRINT("NtQuerySecurityObject() failed (Status %lx)\n", Status);
+      ErrorCode = RtlNtStatusToDosError(Status);
+      SetLastError(ErrorCode);
       return ErrorCode;
     }
 
   return ERROR_SUCCESS;
-#endif
-
-  UNIMPLEMENTED;
-  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
-  return ERROR_CALL_NOT_IMPLEMENTED;
 }
 
 
@@ -1445,7 +1569,7 @@ RegLoadKeyA (HKEY hKey,
 {
   UNICODE_STRING FileName;
   UNICODE_STRING KeyName;
-  DWORD ErrorCode;
+  LONG ErrorCode;
 
   RtlCreateUnicodeStringFromAsciiz (&KeyName,
                                    (LPSTR)lpSubKey);
@@ -1478,11 +1602,12 @@ RegLoadKeyW (HKEY hKey,
   UNICODE_STRING FileName;
   UNICODE_STRING KeyName;
   HANDLE KeyHandle;
-  DWORD ErrorCode;
+  LONG ErrorCode;
   NTSTATUS Status;
 
   if (hKey == HKEY_PERFORMANCE_DATA)
     {
+      SetLastError(ERROR_INVALID_HANDLE);
       return ERROR_INVALID_HANDLE;
     }
 
@@ -1589,7 +1714,6 @@ RegNotifyChangeKeyValue (HKEY hKey,
 }
 
 
-
 /************************************************************************
  *  RegOpenKeyA
  *
@@ -1602,7 +1726,7 @@ RegOpenKeyA (HKEY hKey,
 {
   OBJECT_ATTRIBUTES ObjectAttributes;
   UNICODE_STRING SubKeyString;
-  HKEY KeyHandle;
+  HANDLE KeyHandle;
   LONG ErrorCode;
   NTSTATUS Status;
 
@@ -1622,7 +1746,7 @@ RegOpenKeyA (HKEY hKey,
                              OBJ_CASE_INSENSITIVE,
                              KeyHandle,
                              NULL);
-  Status = NtOpenKey (phkResult,
+  Status = NtOpenKey ((PHANDLE)phkResult,
                      MAXIMUM_ALLOWED,
                      &ObjectAttributes);
   RtlFreeUnicodeString (&SubKeyString);
@@ -1652,7 +1776,7 @@ RegOpenKeyW (HKEY hKey,
 {
   OBJECT_ATTRIBUTES ObjectAttributes;
   UNICODE_STRING SubKeyString;
-  HKEY KeyHandle;
+  HANDLE KeyHandle;
   LONG ErrorCode;
   NTSTATUS Status;
 
@@ -1672,7 +1796,7 @@ RegOpenKeyW (HKEY hKey,
                              OBJ_CASE_INSENSITIVE,
                              KeyHandle,
                              NULL);
-  Status = NtOpenKey (phkResult,
+  Status = NtOpenKey ((PHANDLE)phkResult,
                      MAXIMUM_ALLOWED,
                      &ObjectAttributes);
   if (!NT_SUCCESS(Status))
@@ -1700,7 +1824,7 @@ RegOpenKeyExA (HKEY hKey,
 {
   OBJECT_ATTRIBUTES ObjectAttributes;
   UNICODE_STRING SubKeyString;
-  HKEY KeyHandle;
+  HANDLE KeyHandle;
   LONG ErrorCode;
   NTSTATUS Status;
 
@@ -1720,7 +1844,7 @@ RegOpenKeyExA (HKEY hKey,
                              OBJ_CASE_INSENSITIVE,
                              KeyHandle,
                              NULL);
-  Status = NtOpenKey (phkResult,
+  Status = NtOpenKey ((PHANDLE)phkResult,
                      samDesired,
                      &ObjectAttributes);
   RtlFreeUnicodeString (&SubKeyString);
@@ -1749,7 +1873,7 @@ RegOpenKeyExW (HKEY hKey,
 {
   OBJECT_ATTRIBUTES ObjectAttributes;
   UNICODE_STRING SubKeyString;
-  HKEY KeyHandle;
+  HANDLE KeyHandle;
   LONG ErrorCode;
   NTSTATUS Status;
 
@@ -1777,7 +1901,7 @@ RegOpenKeyExW (HKEY hKey,
                              OBJ_CASE_INSENSITIVE,
                              KeyHandle,
                              NULL);
-  Status = NtOpenKey (phkResult,
+  Status = NtOpenKey ((PHANDLE)phkResult,
                      samDesired,
                      &ObjectAttributes);
   if (!NT_SUCCESS(Status))
@@ -1874,8 +1998,8 @@ RegQueryInfoKeyW (HKEY hKey,
   KEY_FULL_INFORMATION FullInfoBuffer;
   PKEY_FULL_INFORMATION FullInfo;
   ULONG FullInfoSize;
-  ULONG ClassLength;
-  HKEY KeyHandle;
+  ULONG ClassLength = 0;
+  HANDLE KeyHandle;
   NTSTATUS Status;
   LONG ErrorCode = ERROR_SUCCESS;
   ULONG Length;
@@ -1898,13 +2022,14 @@ RegQueryInfoKeyW (HKEY hKey,
   if (lpClass != NULL)
     {
       if (*lpcbClass > 0)
-        {
-          ClassLength = min(*lpcbClass - 1, REG_MAX_NAME_SIZE) * sizeof(WCHAR);
+       {
+         ClassLength = min(*lpcbClass - 1, REG_MAX_NAME_SIZE) * sizeof(WCHAR);
        }
       else
-        { 
+       {
          ClassLength = 0;
        }
+
       FullInfoSize = sizeof(KEY_FULL_INFORMATION) + ((ClassLength + 3) & ~3);
       FullInfo = RtlAllocateHeap (ProcessHeap,
                                  0,
@@ -1914,6 +2039,7 @@ RegQueryInfoKeyW (HKEY hKey,
          SetLastError (ERROR_OUTOFMEMORY);
          return ERROR_OUTOFMEMORY;
        }
+
       FullInfo->ClassLength = ClassLength;
     }
   else
@@ -1932,83 +2058,107 @@ RegQueryInfoKeyW (HKEY hKey,
   DPRINT("NtQueryKey() returned status 0x%X\n", Status);
   if (!NT_SUCCESS(Status))
     {
+      if (lpClass != NULL)
+       {
+         RtlFreeHeap (ProcessHeap,
+                      0,
+                      FullInfo);
+       }
+
       ErrorCode = RtlNtStatusToDosError (Status);
+      SetLastError (ErrorCode);
+      return ErrorCode;
     }
-  else
-    {
-      DPRINT("SubKeys %d\n", FullInfo->SubKeys);
-      if (lpcSubKeys != NULL)
-        {
-          *lpcSubKeys = FullInfo->SubKeys;
-        }
-
-      DPRINT("MaxNameLen %lu\n", FullInfo->MaxNameLen);
-      if (lpcbMaxSubKeyLen != NULL)
-        {
-          *lpcbMaxSubKeyLen = FullInfo->MaxNameLen / sizeof(WCHAR) + 1;
-        }
 
-      DPRINT("MaxClassLen %lu\n", FullInfo->MaxClassLen);
-      if (lpcbMaxClassLen != NULL)
-        {
-          *lpcbMaxClassLen = FullInfo->MaxClassLen / sizeof(WCHAR) + 1;
-        }
+  DPRINT("SubKeys %d\n", FullInfo->SubKeys);
+  if (lpcSubKeys != NULL)
+    {
+      *lpcSubKeys = FullInfo->SubKeys;
+    }
 
-      DPRINT("Values %lu\n", FullInfo->Values);
-      if (lpcValues)
-        {
-          *lpcValues = FullInfo->Values;
-        }
+  DPRINT("MaxNameLen %lu\n", FullInfo->MaxNameLen);
+  if (lpcbMaxSubKeyLen != NULL)
+    {
+      *lpcbMaxSubKeyLen = FullInfo->MaxNameLen / sizeof(WCHAR) + 1;
+    }
 
-      DPRINT("MaxValueNameLen %lu\n", FullInfo->MaxValueNameLen);
-      if (lpcbMaxValueNameLen)
-        {
-          *lpcbMaxValueNameLen = FullInfo->MaxValueNameLen / sizeof(WCHAR) + 1;
-        }
+  DPRINT("MaxClassLen %lu\n", FullInfo->MaxClassLen);
+  if (lpcbMaxClassLen != NULL)
+    {
+      *lpcbMaxClassLen = FullInfo->MaxClassLen / sizeof(WCHAR) + 1;
+    }
 
-      DPRINT("MaxValueDataLen %lu\n", FullInfo->MaxValueDataLen);
-      if (lpcbMaxValueLen)
-        {
-          *lpcbMaxValueLen = FullInfo->MaxValueDataLen;
-        }
+  DPRINT("Values %lu\n", FullInfo->Values);
+  if (lpcValues != NULL)
+    {
+      *lpcValues = FullInfo->Values;
+    }
 
-      if (lpcbSecurityDescriptor)
-        {
-          *lpcbSecurityDescriptor = 0;
-          /* FIXME */
-        }
+  DPRINT("MaxValueNameLen %lu\n", FullInfo->MaxValueNameLen);
+  if (lpcbMaxValueNameLen != NULL)
+    {
+      *lpcbMaxValueNameLen = FullInfo->MaxValueNameLen / sizeof(WCHAR) + 1;
+    }
 
-      if (lpftLastWriteTime != NULL)
-        {
-          lpftLastWriteTime->dwLowDateTime = FullInfo->LastWriteTime.u.LowPart;
-          lpftLastWriteTime->dwHighDateTime = FullInfo->LastWriteTime.u.HighPart;
-        }
+  DPRINT("MaxValueDataLen %lu\n", FullInfo->MaxValueDataLen);
+  if (lpcbMaxValueLen != NULL)
+    {
+      *lpcbMaxValueLen = FullInfo->MaxValueDataLen;
+    }
 
-      if (lpClass != NULL)
-        {
-          if (FullInfo->ClassLength > ClassLength)
-            {
-             ErrorCode = ERROR_BUFFER_OVERFLOW;
-           }
-          else
-            {
-              RtlCopyMemory (lpClass,
-                            FullInfo->Class,
-                            FullInfo->ClassLength);
-             *lpcbClass = FullInfo->ClassLength / sizeof(WCHAR);
-             lpClass[*lpcbClass] = 0;
+  if (lpcbSecurityDescriptor != NULL)
+    {
+      Status = NtQuerySecurityObject(KeyHandle,
+                                    OWNER_SECURITY_INFORMATION |
+                                    GROUP_SECURITY_INFORMATION |
+                                    DACL_SECURITY_INFORMATION,
+                                    NULL,
+                                    0,
+                                    lpcbSecurityDescriptor);
+      if (!NT_SUCCESS(Status))
+       {
+         if (lpClass != NULL)
+           {
+             RtlFreeHeap(ProcessHeap,
+                         0,
+                         FullInfo);
            }
+
+         ErrorCode = RtlNtStatusToDosError(Status);
+         SetLastError(ErrorCode);
+         return ErrorCode;
        }
     }
+
+  if (lpftLastWriteTime != NULL)
+    {
+      lpftLastWriteTime->dwLowDateTime = FullInfo->LastWriteTime.u.LowPart;
+      lpftLastWriteTime->dwHighDateTime = FullInfo->LastWriteTime.u.HighPart;
+    }
+
   if (lpClass != NULL)
     {
+      if (FullInfo->ClassLength > ClassLength)
+       {
+             ErrorCode = ERROR_BUFFER_OVERFLOW;
+       }
+      else
+       {
+         RtlCopyMemory (lpClass,
+                        FullInfo->Class,
+                        FullInfo->ClassLength);
+         *lpcbClass = FullInfo->ClassLength / sizeof(WCHAR);
+         lpClass[*lpcbClass] = 0;
+       }
+
       RtlFreeHeap (ProcessHeap,
-                  0,
+                  0,
                   FullInfo);
     }
+
   if (ErrorCode != ERROR_SUCCESS)
     {
-      SetLastError(ErrorCode);
+      SetLastError (ErrorCode);
     }
 
   return ErrorCode;
@@ -2018,7 +2168,7 @@ RegQueryInfoKeyW (HKEY hKey,
 /************************************************************************
  *  RegQueryMultipleValuesA
  *
- * @unimplemented
+ * @implemented
  */
 LONG STDCALL
 RegQueryMultipleValuesA (HKEY hKey,
@@ -2027,9 +2177,55 @@ RegQueryMultipleValuesA (HKEY hKey,
                         LPSTR lpValueBuf,
                         LPDWORD ldwTotsize)
 {
-  UNIMPLEMENTED;
-  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
-  return ERROR_CALL_NOT_IMPLEMENTED;
+  ULONG i;
+  DWORD maxBytes = *ldwTotsize;
+  LPSTR bufptr = (LPSTR)lpValueBuf;
+  LONG ErrorCode;
+
+  if (maxBytes >= (1024*1024))
+    return ERROR_TRANSFER_TOO_LONG;
+
+  *ldwTotsize = 0;
+
+  DPRINT ("RegQueryMultipleValuesA(%p,%p,%ld,%p,%p=%ld)\n",
+         hKey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
+
+  for (i = 0; i < num_vals; i++)
+    {
+      val_list[i].ve_valuelen = 0;
+      ErrorCode = RegQueryValueExA (hKey,
+                                   val_list[i].ve_valuename,
+                                   NULL,
+                                   NULL,
+                                   NULL,
+                                   &val_list[i].ve_valuelen);
+      if (ErrorCode != ERROR_SUCCESS)
+       {
+         return ErrorCode;
+       }
+
+      if (lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
+       {
+         ErrorCode = RegQueryValueExA (hKey,
+                                       val_list[i].ve_valuename,
+                                       NULL,
+                                       &val_list[i].ve_type,
+                                       bufptr,
+                                       &val_list[i].ve_valuelen);
+         if (ErrorCode != ERROR_SUCCESS)
+           {
+             return ErrorCode;
+           }
+
+         val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
+
+         bufptr += val_list[i].ve_valuelen;
+       }
+
+      *ldwTotsize += val_list[i].ve_valuelen;
+    }
+
+  return (lpValueBuf != NULL && *ldwTotsize <= maxBytes) ? ERROR_SUCCESS : ERROR_MORE_DATA;
 }
 
 
@@ -2047,32 +2243,42 @@ RegQueryMultipleValuesW (HKEY hKey,
 {
   ULONG i;
   DWORD maxBytes = *ldwTotsize;
-  HRESULT status;
   LPSTR bufptr = (LPSTR)lpValueBuf;
+  LONG ErrorCode;
 
-  if ( maxBytes >= (1024*1024) )
+  if (maxBytes >= (1024*1024))
     return ERROR_TRANSFER_TOO_LONG;
 
   *ldwTotsize = 0;
 
-  //TRACE("(%p,%p,%ld,%p,%p=%ld)\n", hKey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
+  DPRINT ("RegQueryMultipleValuesW(%p,%p,%ld,%p,%p=%ld)\n",
+         hKey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
 
-  for(i=0; i < num_vals; ++i)
+  for (i = 0; i < num_vals; i++)
     {
-      val_list[i].ve_valuelen=0;
-      status = RegQueryValueExW(hKey, val_list[i].ve_valuename, NULL, NULL, NULL, &val_list[i].ve_valuelen);
-      if(status != ERROR_SUCCESS)
+      val_list[i].ve_valuelen = 0;
+      ErrorCode = RegQueryValueExW (hKey,
+                                   val_list[i].ve_valuename,
+                                   NULL,
+                                   NULL,
+                                   NULL,
+                                   &val_list[i].ve_valuelen);
+      if (ErrorCode != ERROR_SUCCESS)
        {
-          return status;
+         return ErrorCode;
        }
 
-      if(lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
+      if (lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
        {
-         status = RegQueryValueExW(hKey, val_list[i].ve_valuename, NULL, &val_list[i].ve_type,
-                                   bufptr, &val_list[i].ve_valuelen);
-         if(status != ERROR_SUCCESS)
+         ErrorCode = RegQueryValueExW (hKey,
+                                       val_list[i].ve_valuename,
+                                       NULL,
+                                       &val_list[i].ve_type,
+                                       bufptr,
+                                       &val_list[i].ve_valuelen);
+         if (ErrorCode != ERROR_SUCCESS)
            {
-             return status;
+             return ErrorCode;
            }
 
          val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
@@ -2082,7 +2288,8 @@ RegQueryMultipleValuesW (HKEY hKey,
 
       *ldwTotsize += val_list[i].ve_valuelen;
     }
-  return lpValueBuf != NULL && *ldwTotsize <= maxBytes ? ERROR_SUCCESS : ERROR_MORE_DATA;
+
+  return (lpValueBuf != NULL && *ldwTotsize <= maxBytes) ? ERROR_SUCCESS : ERROR_MORE_DATA;
 }
 
 
@@ -2105,8 +2312,8 @@ RegQueryValueExW (HKEY hKey,
   LONG ErrorCode = ERROR_SUCCESS;
   ULONG BufferSize;
   ULONG ResultSize;
-  HKEY KeyHandle;
-  ULONG MaxCopy = lpcbData ? *lpcbData : 0;
+  HANDLE KeyHandle;
+  ULONG MaxCopy = lpcbData != NULL && lpData != NULL ? *lpcbData : 0;
 
   DPRINT("hKey 0x%X  lpValueName %S  lpData 0x%X  lpcbData %d\n",
         hKey, lpValueName, lpData, lpcbData ? *lpcbData : 0);
@@ -2128,7 +2335,7 @@ RegQueryValueExW (HKEY hKey,
 
   RtlInitUnicodeString (&ValueName,
                        lpValueName);
-  BufferSize = sizeof (KEY_VALUE_PARTIAL_INFORMATION) + MaxCopy;
+  BufferSize = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data[0]) + MaxCopy;
   ValueInfo = RtlAllocateHeap (ProcessHeap,
                               0,
                               BufferSize);
@@ -2145,7 +2352,7 @@ RegQueryValueExW (HKEY hKey,
                            BufferSize,
                            &ResultSize);
   DPRINT("Status 0x%X\n", Status);
-  if (Status == STATUS_BUFFER_TOO_SMALL)
+  if (Status == STATUS_BUFFER_OVERFLOW)
     {
       /* Return ERROR_SUCCESS and the buffer space needed for a successful call */
       MaxCopy = 0;
@@ -2156,6 +2363,10 @@ RegQueryValueExW (HKEY hKey,
       ErrorCode = RtlNtStatusToDosError (Status);
       SetLastError (ErrorCode);
       MaxCopy = 0;
+      if (lpcbData != NULL)
+       {
+         ResultSize = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data[0]) + *lpcbData;
+       }
     }
 
   if (lpType != NULL)
@@ -2163,27 +2374,35 @@ RegQueryValueExW (HKEY hKey,
       *lpType = ValueInfo->Type;
     }
 
-  if (NT_SUCCESS(Status))
-    RtlMoveMemory (lpData,
-                  ValueInfo->Data,
-                  min(ValueInfo->DataLength,MaxCopy));
+  if (NT_SUCCESS(Status) && lpData != NULL)
+    {
+      RtlMoveMemory (lpData,
+                    ValueInfo->Data,
+                    min(ValueInfo->DataLength, MaxCopy));
+    }
 
   if ((ValueInfo->Type == REG_SZ) ||
       (ValueInfo->Type == REG_MULTI_SZ) ||
       (ValueInfo->Type == REG_EXPAND_SZ))
     {
-      if (MaxCopy > ValueInfo->DataLength / sizeof(WCHAR))
-       ((PWSTR)lpData)[ValueInfo->DataLength / sizeof(WCHAR)] = 0;
-      
-      if (lpcbData) {
-       *lpcbData = (ResultSize - sizeof(*ValueInfo)) / sizeof(WCHAR);
-       DPRINT("(string) Returning Size: %d\n", *lpcbData);
-      }
+      if (lpData != NULL && MaxCopy > ValueInfo->DataLength)
+       {
+         ((PWSTR)lpData)[ValueInfo->DataLength / sizeof(WCHAR)] = 0;
+       }
+
+      if (lpcbData != NULL)
+       {
+         *lpcbData = (ResultSize - FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data[0]));
+         DPRINT("(string) Returning Size: %lu\n", *lpcbData);
+       }
     }
   else
-    if (lpcbData) {
-      *lpcbData = ResultSize - sizeof(*ValueInfo);
-      DPRINT("(other) Returning Size: %d\n", *lpcbData);
+    {
+      if (lpcbData != NULL)
+       {
+         *lpcbData = ResultSize - FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data[0]);
+         DPRINT("(other) Returning Size: %lu\n", *lpcbData);
+       }
     }
 
   DPRINT("Type %d  Size %d\n", ValueInfo->Type, ValueInfo->DataLength);
@@ -2201,15 +2420,13 @@ RegQueryValueExW (HKEY hKey,
  *
  * @implemented
  */
-LONG
-STDCALL
-RegQueryValueExA(
-  HKEY  hKey,
-  LPCSTR  lpValueName,
-  LPDWORD lpReserved,
-  LPDWORD lpType,
-  LPBYTE  lpData,
-  LPDWORD lpcbData)
+LONG STDCALL
+RegQueryValueExA (HKEY hKey,
+                 LPCSTR lpValueName,
+                 LPDWORD lpReserved,
+                 LPDWORD lpType,
+                 LPBYTE  lpData,
+                 LPDWORD lpcbData)
 {
   UNICODE_STRING ValueName;
   UNICODE_STRING ValueData;
@@ -2218,7 +2435,7 @@ RegQueryValueExA(
   DWORD Length;
   DWORD Type;
 
-  if ((lpData) && (!lpcbData))
+  if (lpData != NULL && lpcbData == NULL)
     {
       SetLastError(ERROR_INVALID_PARAMETER);
       return ERROR_INVALID_PARAMETER;
@@ -2228,15 +2445,14 @@ RegQueryValueExA(
     {
       ValueData.Length = *lpcbData * sizeof(WCHAR);
       ValueData.MaximumLength = ValueData.Length + sizeof(WCHAR);
-      ValueData.Buffer = RtlAllocateHeap(
-        ProcessHeap,
-        0,
-        ValueData.MaximumLength);
+      ValueData.Buffer = RtlAllocateHeap (ProcessHeap,
+                                         0,
+                                         ValueData.MaximumLength);
       if (!ValueData.Buffer)
-        {
-          SetLastError(ERROR_OUTOFMEMORY);
-          return ERROR_OUTOFMEMORY;
-        }
+       {
+         SetLastError(ERROR_OUTOFMEMORY);
+         return ERROR_OUTOFMEMORY;
+       }
     }
   else
     {
@@ -2245,37 +2461,58 @@ RegQueryValueExA(
       ValueData.MaximumLength = 0;
     }
 
-  RtlCreateUnicodeStringFromAsciiz(&ValueName, (LPSTR)lpValueName);
+  RtlCreateUnicodeStringFromAsciiz (&ValueName,
+                                   (LPSTR)lpValueName);
+
+  if (NULL != lpcbData)
+    {
+      Length = *lpcbData * sizeof(WCHAR);
+    }
+  ErrorCode = RegQueryValueExW (hKey,
+                               ValueName.Buffer,
+                               lpReserved,
+                               &Type,
+                               (LPBYTE)ValueData.Buffer,
+                               NULL == lpcbData ? NULL : &Length);
+  DPRINT("ErrorCode %lu\n", ErrorCode);
 
-  /* Convert length from USHORT to DWORD */
-  Length = ValueData.Length / sizeof(WCHAR);
-  ErrorCode = RegQueryValueExW
-    (hKey,
-     ValueName.Buffer,
-     lpReserved,
-     &Type,
-     (LPBYTE)ValueData.Buffer,
-     &Length);
-  if (lpType) *lpType = Type;
-  if ((ErrorCode == ERROR_SUCCESS) && (ValueData.Buffer != NULL))
+  if (ErrorCode == ERROR_SUCCESS ||
+      ErrorCode == ERROR_MORE_DATA)
     {
+      if (lpType != NULL)
+       {
+         *lpType = Type;
+       }
+
       if ((Type == REG_SZ) || (Type == REG_MULTI_SZ) || (Type == REG_EXPAND_SZ))
-        {
-          RtlInitAnsiString(&AnsiString, NULL);
-          AnsiString.Buffer = lpData;
-          AnsiString.MaximumLength = *lpcbData;
-         ValueData.Length = Length * sizeof(WCHAR);
-         ValueData.MaximumLength = ValueData.Length + sizeof(WCHAR);
-          RtlUnicodeStringToAnsiString(&AnsiString, &ValueData, FALSE);
-        } else {
-          RtlMoveMemory(lpData, ValueData.Buffer, 
-                       min(*lpcbData,Length));
-        }
+       {
+         if (ErrorCode == ERROR_SUCCESS && ValueData.Buffer != NULL)
+           {
+             RtlInitAnsiString(&AnsiString, NULL);
+             AnsiString.Buffer = lpData;
+             AnsiString.MaximumLength = *lpcbData;
+             ValueData.Length = Length;
+             ValueData.MaximumLength = ValueData.Length + sizeof(WCHAR);
+             RtlUnicodeStringToAnsiString(&AnsiString, &ValueData, FALSE);
+           }
+         Length = Length / sizeof(WCHAR);
+       }
+      else if (lpcbData != NULL)
+       {
+         Length = min(*lpcbData, Length);
+         if (ErrorCode == ERROR_SUCCESS && ValueData.Buffer != NULL)
+           {
+             RtlMoveMemory(lpData, ValueData.Buffer, Length);
+           }
+       }
+
+      if (lpcbData != NULL)
+       {
+         *lpcbData = Length;
+       }
     }
-  
-  *lpcbData = Length;
 
-  if (ValueData.Buffer)
+  if (ValueData.Buffer != NULL)
     {
       RtlFreeHeap(ProcessHeap, 0, ValueData.Buffer);
     }
@@ -2384,7 +2621,7 @@ RegQueryValueW (HKEY hKey,
 {
   OBJECT_ATTRIBUTES ObjectAttributes;
   UNICODE_STRING SubKeyString;
-  HKEY KeyHandle;
+  HANDLE KeyHandle;
   HANDLE RealKey;
   LONG ErrorCode;
   BOOL CloseRealKey;
@@ -2452,19 +2689,28 @@ RegReplaceKeyA (HKEY hKey,
                LPCSTR lpNewFile,
                LPCSTR lpOldFile)
 {
-  UNICODE_STRING lpSubKeyW;
-  UNICODE_STRING lpNewFileW;
-  UNICODE_STRING lpOldFileW;
-  LONG ret;
-
-  RtlCreateUnicodeStringFromAsciiz( &lpSubKeyW, (PCSZ)lpSubKey );
-  RtlCreateUnicodeStringFromAsciiz( &lpOldFileW, (PCSZ)lpOldFile );
-  RtlCreateUnicodeStringFromAsciiz( &lpNewFileW, (PCSZ)lpNewFile );
-  ret = RegReplaceKeyW( hKey, lpSubKeyW.Buffer, lpNewFileW.Buffer, lpOldFileW.Buffer );
-  RtlFreeUnicodeString( &lpOldFileW );
-  RtlFreeUnicodeString( &lpNewFileW );
-  RtlFreeUnicodeString( &lpSubKeyW );
-  return ret;
+  UNICODE_STRING SubKey;
+  UNICODE_STRING NewFile;
+  UNICODE_STRING OldFile;
+  LONG ErrorCode;
+
+  RtlCreateUnicodeStringFromAsciiz (&SubKey,
+                                   (PCSZ)lpSubKey);
+  RtlCreateUnicodeStringFromAsciiz (&OldFile,
+                                   (PCSZ)lpOldFile);
+  RtlCreateUnicodeStringFromAsciiz (&NewFile,
+                                   (PCSZ)lpNewFile);
+
+  ErrorCode = RegReplaceKeyW (hKey,
+                             SubKey.Buffer,
+                             NewFile.Buffer,
+                             OldFile.Buffer);
+
+  RtlFreeUnicodeString (&OldFile);
+  RtlFreeUnicodeString (&NewFile);
+  RtlFreeUnicodeString (&SubKey);
+
+  return ErrorCode;
 }
 
 
@@ -2479,9 +2725,120 @@ RegReplaceKeyW (HKEY hKey,
                LPCWSTR lpNewFile,
                LPCWSTR lpOldFile)
 {
-  UNIMPLEMENTED;
-  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
-  return ERROR_CALL_NOT_IMPLEMENTED;
+  OBJECT_ATTRIBUTES KeyObjectAttributes;
+  OBJECT_ATTRIBUTES NewObjectAttributes;
+  OBJECT_ATTRIBUTES OldObjectAttributes;
+  UNICODE_STRING SubKeyName;
+  UNICODE_STRING NewFileName;
+  UNICODE_STRING OldFileName;
+  BOOLEAN CloseRealKey;
+  HANDLE RealKeyHandle;
+  HANDLE KeyHandle;
+  LONG ErrorCode;
+  NTSTATUS Status;
+
+  if (hKey == HKEY_PERFORMANCE_DATA)
+    {
+      return ERROR_INVALID_HANDLE;
+    }
+
+  Status = MapDefaultKey (&KeyHandle,
+                         hKey);
+  if (!NT_SUCCESS(Status))
+    {
+      ErrorCode = RtlNtStatusToDosError (Status);
+      SetLastError (ErrorCode);
+      return ErrorCode;
+    }
+
+  /* Open the real key */
+  if (lpSubKey != NULL && *lpSubKey != (WCHAR)0)
+    {
+      RtlInitUnicodeString (&SubKeyName,
+                           (PWSTR)lpSubKey);
+      InitializeObjectAttributes (&KeyObjectAttributes,
+                                 &SubKeyName,
+                                 OBJ_CASE_INSENSITIVE,
+                                 KeyHandle,
+                                 NULL);
+      Status = NtOpenKey (&RealKeyHandle,
+                         KEY_ALL_ACCESS,
+                         &KeyObjectAttributes);
+      if (!NT_SUCCESS(Status))
+       {
+         ErrorCode = RtlNtStatusToDosError (Status);
+         SetLastError (ErrorCode);
+         return ErrorCode;
+       }
+      CloseRealKey = TRUE;
+    }
+  else
+    {
+      RealKeyHandle = KeyHandle;
+      CloseRealKey = FALSE;
+    }
+
+  /* Convert new file name */
+  if (!RtlDosPathNameToNtPathName_U ((LPWSTR)lpNewFile,
+                                    &NewFileName,
+                                    NULL,
+                                    NULL))
+    {
+      if (CloseRealKey)
+       {
+         NtClose (RealKeyHandle);
+       }
+      SetLastError (ERROR_INVALID_PARAMETER);
+      return ERROR_INVALID_PARAMETER;
+    }
+
+  InitializeObjectAttributes (&NewObjectAttributes,
+                             &NewFileName,
+                             OBJ_CASE_INSENSITIVE,
+                             NULL,
+                             NULL);
+
+  /* Convert old file name */
+  if (!RtlDosPathNameToNtPathName_U ((LPWSTR)lpOldFile,
+                                    &OldFileName,
+                                    NULL,
+                                    NULL))
+    {
+      RtlFreeUnicodeString (&NewFileName);
+      if (CloseRealKey)
+       {
+         NtClose (RealKeyHandle);
+       }
+      SetLastError (ERROR_INVALID_PARAMETER);
+      return ERROR_INVALID_PARAMETER;
+    }
+
+  InitializeObjectAttributes (&OldObjectAttributes,
+                             &OldFileName,
+                             OBJ_CASE_INSENSITIVE,
+                             NULL,
+                             NULL);
+
+  Status = NtReplaceKey (&NewObjectAttributes,
+                        RealKeyHandle,
+                        &OldObjectAttributes);
+
+  RtlFreeUnicodeString (&OldFileName);
+  RtlFreeUnicodeString (&NewFileName);
+
+  if (CloseRealKey)
+    {
+      NtClose (RealKeyHandle);
+    }
+
+  if (!NT_SUCCESS(Status))
+    {
+      ErrorCode = RtlNtStatusToDosError (Status);
+      SetLastError (ErrorCode);
+      return ErrorCode;
+    }
+
+  return ERROR_SUCCESS;
 }
 
 
@@ -2495,29 +2852,95 @@ RegRestoreKeyA (HKEY hKey,
                LPCSTR lpFile,
                DWORD dwFlags)
 {
-  UNICODE_STRING lpFileW;
-  LONG ret;
+  UNICODE_STRING FileName;
+  LONG ErrorCode;
 
-  RtlCreateUnicodeStringFromAsciiz( &lpFileW, (PCSZ)lpFile );
-  ret = RegRestoreKeyW( hKey, lpFileW.Buffer, dwFlags );
-  RtlFreeUnicodeString( &lpFileW );
-  return ret;
+  RtlCreateUnicodeStringFromAsciiz (&FileName,
+                                   (PCSZ)lpFile);
+
+  ErrorCode = RegRestoreKeyW (hKey,
+                             FileName.Buffer,
+                             dwFlags);
+
+  RtlFreeUnicodeString (&FileName);
+
+  return ErrorCode;
 }
 
 
 /************************************************************************
  *  RegRestoreKeyW
  *
- * @unimplemented
+ * @implemented
  */
 LONG STDCALL
 RegRestoreKeyW (HKEY hKey,
                LPCWSTR lpFile,
                DWORD dwFlags)
 {
-  UNIMPLEMENTED;
-  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
-  return ERROR_CALL_NOT_IMPLEMENTED;
+  OBJECT_ATTRIBUTES ObjectAttributes;
+  IO_STATUS_BLOCK IoStatusBlock;
+  UNICODE_STRING FileName;
+  HANDLE FileHandle;
+  HANDLE KeyHandle;
+  LONG ErrorCode;
+  NTSTATUS Status;
+
+  if (hKey == HKEY_PERFORMANCE_DATA)
+    {
+      return ERROR_INVALID_HANDLE;
+    }
+
+  Status = MapDefaultKey (&KeyHandle,
+                         hKey);
+  if (!NT_SUCCESS(Status))
+    {
+      ErrorCode = RtlNtStatusToDosError (Status);
+      SetLastError (ErrorCode);
+      return ErrorCode;
+    }
+
+  if (!RtlDosPathNameToNtPathName_U ((LPWSTR)lpFile,
+                                    &FileName,
+                                    NULL,
+                                    NULL))
+    {
+      SetLastError (ERROR_INVALID_PARAMETER);
+      return ERROR_INVALID_PARAMETER;
+    }
+
+  InitializeObjectAttributes (&ObjectAttributes,
+                             &FileName,
+                             OBJ_CASE_INSENSITIVE,
+                             NULL,
+                             NULL);
+
+  Status = NtOpenFile (&FileHandle,
+                      FILE_GENERIC_READ,
+                      &ObjectAttributes,
+                      &IoStatusBlock,
+                      FILE_SHARE_READ,
+                      FILE_SYNCHRONOUS_IO_NONALERT);
+  RtlFreeUnicodeString (&FileName);
+  if (!NT_SUCCESS(Status))
+    {
+      ErrorCode = RtlNtStatusToDosError (Status);
+      SetLastError (ErrorCode);
+      return ErrorCode;
+    }
+
+  Status = NtRestoreKey (KeyHandle,
+                        FileHandle,
+                        (ULONG)dwFlags);
+  NtClose (FileHandle);
+  if (!NT_SUCCESS(Status))
+    {
+      ErrorCode = RtlNtStatusToDosError (Status);
+      SetLastError (ErrorCode);
+      return ErrorCode;
+    }
+
+  return ERROR_SUCCESS;
 }
 
 
@@ -2527,9 +2950,9 @@ RegRestoreKeyW (HKEY hKey,
  * @implemented
  */
 LONG STDCALL
-RegSaveKeyA(HKEY hKey,
-           LPCSTR lpFile,
-           LPSECURITY_ATTRIBUTES lpSecurityAttributes)
+RegSaveKeyA (HKEY hKey,
+            LPCSTR lpFile,
+            LPSECURITY_ATTRIBUTES lpSecurityAttributes)
 {
   UNICODE_STRING FileName;
   LONG ErrorCode;
@@ -2557,10 +2980,10 @@ RegSaveKeyW (HKEY hKey,
 {
   PSECURITY_DESCRIPTOR SecurityDescriptor = NULL;
   OBJECT_ATTRIBUTES ObjectAttributes;
-  UNICODE_STRING NtName;
+  UNICODE_STRING FileName;
   IO_STATUS_BLOCK IoStatusBlock;
   HANDLE FileHandle;
-  HKEY KeyHandle;
+  HANDLE KeyHandle;
   NTSTATUS Status;
   LONG ErrorCode;
 
@@ -2573,8 +2996,8 @@ RegSaveKeyW (HKEY hKey,
       return ErrorCode;
     }
 
-  if (!RtlDosPathNameToNtPathName_U ((LPWSTR)lpFile,
-                                    &NtName,
+  if (!RtlDosPathNameToNtPathName_U ((PWSTR)lpFile,
+                                    &FileName,
                                     NULL,
                                     NULL))
     {
@@ -2588,7 +3011,7 @@ RegSaveKeyW (HKEY hKey,
     }
 
   InitializeObjectAttributes (&ObjectAttributes,
-                             &NtName,
+                             &FileName,
                              OBJ_CASE_INSENSITIVE,
                              NULL,
                              SecurityDescriptor);
@@ -2603,7 +3026,7 @@ RegSaveKeyW (HKEY hKey,
                         FILE_OPEN_FOR_BACKUP_INTENT | FILE_SYNCHRONOUS_IO_NONALERT,
                         NULL,
                         0);
-  RtlFreeUnicodeString (&NtName);
+  RtlFreeUnicodeString (&FileName);
   if (!NT_SUCCESS(Status))
     {
       ErrorCode = RtlNtStatusToDosError (Status);
@@ -2635,12 +3058,15 @@ RegSetKeySecurity (HKEY hKey,
                   SECURITY_INFORMATION SecurityInformation,
                   PSECURITY_DESCRIPTOR pSecurityDescriptor)
 {
-  HKEY KeyHandle;
-  NTSTATUS Status;
+  HANDLE KeyHandle;
   LONG ErrorCode;
+  NTSTATUS Status;
 
   if (hKey == HKEY_PERFORMANCE_DATA)
-    return ERROR_INVALID_HANDLE;
+    {
+      SetLastError(ERROR_INVALID_HANDLE);
+      return ERROR_INVALID_HANDLE;
+    }
 
   Status = MapDefaultKey (&KeyHandle,
                          hKey);
@@ -2696,7 +3122,7 @@ RegSetValueExA (HKEY hKey,
       strlen(lpValueName) != 0)
     {
       RtlCreateUnicodeStringFromAsciiz (&ValueName,
-                                       (LPSTR)lpValueName);
+                                       (PSTR)lpValueName);
       pValueName = (LPWSTR)ValueName.Buffer;
     }
   else
@@ -2710,7 +3136,7 @@ RegSetValueExA (HKEY hKey,
     {
       RtlInitAnsiString (&AnsiString,
                         NULL);
-      AnsiString.Buffer = (LPSTR)lpData;
+      AnsiString.Buffer = (PSTR)lpData;
       AnsiString.Length = cbData;
       AnsiString.MaximumLength = cbData;
       RtlAnsiStringToUnicodeString (&Data,
@@ -2766,7 +3192,7 @@ RegSetValueExW (HKEY hKey,
 {
   UNICODE_STRING ValueName;
   PUNICODE_STRING pValueName;
-  HKEY KeyHandle;
+  HANDLE KeyHandle;
   NTSTATUS Status;
   LONG ErrorCode;
 
@@ -2858,6 +3284,7 @@ RegSetValueA (HKEY hKey,
                            dwType,
                            Data.Buffer,
                            DataSize);
+
   RtlFreeHeap (ProcessHeap,
               0,
               Data.Buffer);
@@ -2880,7 +3307,7 @@ RegSetValueW (HKEY hKey,
 {
   OBJECT_ATTRIBUTES ObjectAttributes;
   UNICODE_STRING SubKeyString;
-  HKEY KeyHandle;
+  HANDLE KeyHandle;
   HANDLE RealKey;
   LONG ErrorCode;
   BOOL CloseRealKey;
@@ -2966,7 +3393,7 @@ RegUnLoadKeyA (HKEY hKey,
  * @implemented
  */
 LONG STDCALL
-RegUnLoadKeyW (HKEY  hKey,
+RegUnLoadKeyW (HKEY hKey,
               LPCWSTR lpSubKey)
 {
   OBJECT_ATTRIBUTES ObjectAttributes;
@@ -2976,7 +3403,10 @@ RegUnLoadKeyW (HKEY  hKey,
   NTSTATUS Status;
 
   if (hKey == HKEY_PERFORMANCE_DATA)
-    return ERROR_INVALID_HANDLE;
+    {
+      SetLastError(ERROR_INVALID_HANDLE);
+      return ERROR_INVALID_HANDLE;
+    }
 
   Status = MapDefaultKey (&KeyHandle, hKey);
   if (!NT_SUCCESS(Status))