Commit in reactos/ntoskrnl/cm on MAIN
rtlfunc.c+86-81.36 -> 1.37
Fully implement RtlFormatCurrentUserKeyPath().

reactos/ntoskrnl/cm
rtlfunc.c 1.36 -> 1.37
diff -u -r1.36 -r1.37
--- rtlfunc.c	7 Mar 2004 11:59:09 -0000	1.36
+++ rtlfunc.c	20 Mar 2004 15:55:05 -0000	1.37
@@ -29,6 +29,7 @@
 		      BOOLEAN Create,
 		      PHANDLE KeyHandle)
 {
+  UNICODE_STRING KeyPath;
   UNICODE_STRING KeyName;
   WCHAR KeyBuffer[MAX_PATH];
   OBJECT_ATTRIBUTES ObjectAttributes;
@@ -80,10 +81,14 @@
 	break;
 
       case RTL_REGISTRY_USER:
-	Status = RtlFormatCurrentUserKeyPath(&KeyName);
+	Status = RtlFormatCurrentUserKeyPath (&KeyPath);
 	if (!NT_SUCCESS(Status))
 	  return(Status);
-	RtlAppendUnicodeToString(&KeyName, L"\\");
+	RtlAppendUnicodeStringToString (&KeyName,
+					&KeyPath);
+	RtlFreeUnicodeString (&KeyPath);
+	RtlAppendUnicodeToString (&KeyName,
+				  L"\\");
 	break;
 
       /* ReactOS specific */
@@ -207,16 +212,87 @@
 
 
 /*
- * @unimplemented
+ * @implemented
  */
 NTSTATUS STDCALL
-RtlFormatCurrentUserKeyPath(IN OUT PUNICODE_STRING KeyPath)
+RtlFormatCurrentUserKeyPath (OUT PUNICODE_STRING KeyPath)
 {
-  /* FIXME: !!! */
+  HANDLE TokenHandle;
+  UCHAR Buffer[256];
+  PSID_AND_ATTRIBUTES SidBuffer;
+  ULONG Length;
+  UNICODE_STRING SidString;
+  NTSTATUS Status;
+
+  DPRINT ("RtlFormatCurrentUserKeyPath() called\n");
+
+  Status = NtOpenThreadToken (NtCurrentThread (),
+			      TOKEN_READ,
+			      TRUE,
+			      &TokenHandle);
+  if (!NT_SUCCESS (Status))
+    {
+      if (Status != STATUS_NO_TOKEN)
+	{
+	  DPRINT1 ("NtOpenThreadToken() failed (Status %lx)\n", Status);
+	  return Status;
+	}
+
+      Status = NtOpenProcessToken (NtCurrentProcess (),
+				   TOKEN_READ,
+				   &TokenHandle);
+      if (!NT_SUCCESS (Status))
+	{
+	  DPRINT1 ("NtOpenProcessToken() failed (Status %lx)\n", Status);
+	  return Status;
+	}
+    }
+
+  SidBuffer = (PSID_AND_ATTRIBUTES)Buffer;
+  Status = NtQueryInformationToken (TokenHandle,
+				    TokenUser,
+				    (PVOID)SidBuffer,
+				    256,
+				    &Length);
+  NtClose (TokenHandle);
+  if (!NT_SUCCESS(Status))
+    {
+      DPRINT1 ("NtQueryInformationToken() failed (Status %lx)\n", Status);
+      return Status;
+    }
+
+  Status = RtlConvertSidToUnicodeString (&SidString,
+					 SidBuffer[0].Sid,
+					 TRUE);
+  if (!NT_SUCCESS(Status))
+    {
+      DPRINT1 ("RtlConvertSidToUnicodeString() failed (Status %lx)\n", Status);
+      return Status;
+    }
+
+  DPRINT ("SidString: '%wZ'\n", &SidString);
+
+  Length = SidString.Length + sizeof(L"\\Registry\\User\\");
+  DPRINT ("Length: %lu\n", Length);
+
   KeyPath->Length = 0;
-  RtlAppendUnicodeToString(KeyPath, L"\\Registry\\User\\.Default");
+  KeyPath->MaximumLength = Length;
+  KeyPath->Buffer = ExAllocatePool (NonPagedPool,
+				    KeyPath->MaximumLength);
+  if (KeyPath->Buffer == NULL)
+    {
+      DPRINT1 ("RtlAllocateHeap() failed\n");
+      RtlFreeUnicodeString (&SidString);
+      return STATUS_NO_TOKEN;
+    }
 
-  return(STATUS_SUCCESS);
+  RtlAppendUnicodeToString (KeyPath,
+			    L"\\Registry\\User\\");
+  RtlAppendUnicodeStringToString (KeyPath,
+				  &SidString);
+  RtlFreeUnicodeString (&SidString);
+
+  return STATUS_SUCCESS;
 }
 
 
@@ -225,7 +301,7 @@
 		   OUT PHANDLE KeyHandle)
 {
   OBJECT_ATTRIBUTES ObjectAttributes;
-  UNICODE_STRING KeyPath = ROS_STRING_INITIALIZER(L"\\Registry\\User\\.Default");
+  UNICODE_STRING KeyPath;
   NTSTATUS Status;
 
   Status = RtlFormatCurrentUserKeyPath(&KeyPath);
@@ -244,6 +320,8 @@
 	return(STATUS_SUCCESS);
     }
 
+  RtlInitUnicodeString (&KeyPath,
+			L"\\Registry\\User\\.Default");
   InitializeObjectAttributes(&ObjectAttributes,
 			     &KeyPath,
 			     OBJ_CASE_INSENSITIVE,
CVSspam 0.2.8