Implement SetComputerNameExA/W
Modified: trunk/reactos/lib/kernel32/misc/computername.c
Modified: trunk/reactos/lib/kernel32/misc/stubs.c
Modified: trunk/reactos/w32api/include/winbase.h

Modified: trunk/reactos/lib/kernel32/misc/computername.c
--- trunk/reactos/lib/kernel32/misc/computername.c	2005-09-04 18:00:59 UTC (rev 17638)
+++ trunk/reactos/lib/kernel32/misc/computername.c	2005-09-04 20:11:51 UTC (rev 17639)
@@ -16,8 +16,7 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/* $Id$
- *
+/*
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS system libraries
  * PURPOSE:         Computer name functions
@@ -285,32 +284,16 @@
 /*
  * @implemented
  */
-BOOL STDCALL
-SetComputerNameA (LPCSTR lpComputerName)
-{
-  UNICODE_STRING ComputerName;
-  BOOL bResult;
-
-  RtlCreateUnicodeStringFromAsciiz (&ComputerName,
-				    (LPSTR)lpComputerName);
-
-  bResult = SetComputerNameW (ComputerName.Buffer);
-
-  RtlFreeUnicodeString (&ComputerName);
-
-  return bResult;
-}
-
-
-/*
- * @implemented
- */
 static BOOL
-IsValidComputerName (LPCWSTR lpComputerName)
+IsValidComputerName (
+    COMPUTER_NAME_FORMAT NameType,
+    LPCWSTR lpComputerName)
 {
   PWCHAR p;
   ULONG Length;
 
+  /* FIXME: do verification according to NameType */
+
   Length = 0;
   p = (PWCHAR)lpComputerName;
   while (*p != 0)
@@ -346,60 +329,137 @@
 }
 
 
+static BOOL SetComputerNameToRegistry(
+    LPCWSTR RegistryKey,
+    LPCWSTR ValueNameStr,
+    LPCWSTR lpBuffer)
+{
+    OBJECT_ATTRIBUTES ObjectAttributes;
+    UNICODE_STRING KeyName;
+    UNICODE_STRING ValueName;
+    HANDLE KeyHandle;
+    NTSTATUS Status;
+
+    RtlInitUnicodeString (&KeyName, RegistryKey);
+    InitializeObjectAttributes (&ObjectAttributes,
+        &KeyName,
+        OBJ_CASE_INSENSITIVE,
+        NULL,
+        NULL );
+
+    Status = NtOpenKey (&KeyHandle,
+        KEY_WRITE,
+        &ObjectAttributes);
+    if (!NT_SUCCESS(Status))
+    {
+        SetLastErrorByStatus (Status);
+        return FALSE;
+    }
+
+    RtlInitUnicodeString (&ValueName, ValueNameStr);
+
+    Status = NtSetValueKey (KeyHandle,
+        &ValueName,
+        0,
+        REG_SZ,
+        (PVOID)lpBuffer,
+        (wcslen (lpBuffer) + 1) * sizeof(WCHAR));
+    if (!NT_SUCCESS(Status))
+    {
+        ZwClose (KeyHandle);
+        SetLastErrorByStatus (Status);
+        return FALSE;
+    }
+
+    NtFlushKey (KeyHandle);
+    ZwClose (KeyHandle);
+
+    return TRUE;
+}
+
+
 /*
  * @implemented
  */
 BOOL STDCALL
+SetComputerNameA (LPCSTR lpComputerName)
+{
+    return SetComputerNameExA( ComputerNameNetBIOS, lpComputerName );
+}
+
+
+/*
+ * @implemented
+ */
+BOOL STDCALL
 SetComputerNameW (LPCWSTR lpComputerName)
 {
-  OBJECT_ATTRIBUTES ObjectAttributes;
-  UNICODE_STRING KeyName;
-  UNICODE_STRING ValueName;
-  HANDLE KeyHandle;
-  NTSTATUS Status;
+    return SetComputerNameExW( ComputerNameNetBIOS, lpComputerName );
+}
 
-  if (!IsValidComputerName (lpComputerName))
+
+/*
+ * @implemented
+ */
+BOOL STDCALL
+SetComputerNameExA (
+    COMPUTER_NAME_FORMAT NameType,
+    LPCSTR lpBuffer)
+{
+    UNICODE_STRING Buffer;
+    BOOL bResult;
+
+    RtlCreateUnicodeStringFromAsciiz (&Buffer,
+				    (LPSTR)lpBuffer);
+
+    bResult = SetComputerNameExW (NameType, Buffer.Buffer);
+
+    RtlFreeUnicodeString (&Buffer);
+
+    return bResult;
+}
+
+
+/*
+ * @implemented
+ */
+BOOL STDCALL
+SetComputerNameExW (
+    COMPUTER_NAME_FORMAT NameType,
+    LPCWSTR lpBuffer)
+{
+  if (!IsValidComputerName (NameType, lpBuffer))
     {
       SetLastError (ERROR_INVALID_PARAMETER);
       return FALSE;
     }
 
-  RtlInitUnicodeString (&KeyName,
-			L"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\ComputerName\\ComputerName");
-  InitializeObjectAttributes (&ObjectAttributes,
-			      &KeyName,
-			      OBJ_CASE_INSENSITIVE,
-			      NULL,
-			      NULL);
-  Status = NtOpenKey (&KeyHandle,
-		      KEY_WRITE,
-		      &ObjectAttributes);
-  if (!NT_SUCCESS(Status))
-    {
-      SetLastErrorByStatus (Status);
-      return FALSE;
-    }
+  switch( NameType ) {
+    case ComputerNamePhysicalDnsDomain:
+      return SetComputerNameToRegistry
+        ( L"\\Registry\\Machine\\System\\CurrentControlSet"
+          L"\\Services\\Tcpip\\Parameters",
+          L"Domain",
+          lpBuffer );
 
-  RtlInitUnicodeString (&ValueName,
-			L"ComputerName");
+    case ComputerNamePhysicalDnsHostname:
+      return SetComputerNameToRegistry
+        ( L"\\Registry\\Machine\\System\\CurrentControlSet"
+          L"\\Services\\Tcpip\\Parameters",
+          L"Hostname",
+          lpBuffer );
 
-  Status = NtSetValueKey (KeyHandle,
-			  &ValueName,
-			  0,
-			  REG_SZ,
-			  (PVOID)lpComputerName,
-			  (wcslen (lpComputerName) + 1) * sizeof(WCHAR));
-  if (!NT_SUCCESS(Status))
-    {
-      ZwClose (KeyHandle);
-      SetLastErrorByStatus (Status);
-      return FALSE;
-    }
+    case ComputerNamePhysicalNetBIOS:
+      return SetComputerNameToRegistry
+        ( L"\\Registry\\Machine\\System\\CurrentControlSet"
+          L"\\Control\\ComputerName\\ComputerName",
+          L"ComputerName",
+          lpBuffer );
 
-  NtFlushKey (KeyHandle);
-  ZwClose (KeyHandle);
-
-  return TRUE;
+    default:
+        SetLastError (ERROR_INVALID_PARAMETER);
+        return FALSE;
+  }
 }
 
 /* EOF */

Modified: trunk/reactos/lib/kernel32/misc/stubs.c
--- trunk/reactos/lib/kernel32/misc/stubs.c	2005-09-04 18:00:59 UTC (rev 17638)
+++ trunk/reactos/lib/kernel32/misc/stubs.c	2005-09-04 20:11:51 UTC (rev 17639)
@@ -1,5 +1,4 @@
-/* $Id$
- *
+/*
  * KERNEL32.DLL stubs (STUB functions)
  * Remove from this file, if you implement them.
  */
@@ -1239,20 +1238,6 @@
  */
 BOOL
 STDCALL
-SetComputerNameExW (
-    COMPUTER_NAME_FORMAT NameType,
-    LPCWSTR lpBuffer
-    )
-{
-    STUB;
-    return 0;
-}
-
-/*
- * @unimplemented
- */
-BOOL
-STDCALL
 SetFirmwareEnvironmentVariableW(
     LPCWSTR lpName,
     LPCWSTR lpGuid,
@@ -1498,20 +1483,6 @@
  */
 BOOL
 STDCALL
-SetComputerNameExA (
-    COMPUTER_NAME_FORMAT NameType,
-    LPCSTR lpBuffer
-    )
-{
-    STUB;
-    return 0;
-}
-
-/*
- * @unimplemented
- */
-BOOL
-STDCALL
 SetFirmwareEnvironmentVariableA(
     LPCSTR lpName,
     LPCSTR lpGuid,

Modified: trunk/reactos/w32api/include/winbase.h
--- trunk/reactos/w32api/include/winbase.h	2005-09-04 18:00:59 UTC (rev 17638)
+++ trunk/reactos/w32api/include/winbase.h	2005-09-04 20:11:51 UTC (rev 17639)
@@ -2093,6 +2093,7 @@
 #define ReportEvent ReportEventW
 #define SearchPath SearchPathW
 #define SetComputerName SetComputerNameW
+#define SetComputerNameEx SetComputerNameExW
 #define SetCurrentDirectory SetCurrentDirectoryW
 #define SetDefaultCommConfig SetDefaultCommConfigW
 #if (_WIN32_WINNT >= 0x0502)
@@ -2291,6 +2292,7 @@
 #define ReportEvent ReportEventA
 #define SearchPath SearchPathA
 #define SetComputerName SetComputerNameA
+#define SetComputerNameEx SetComputerNameExA
 #define SetCurrentDirectory SetCurrentDirectoryA
 #define SetDefaultCommConfig SetDefaultCommConfigA
 #if (_WIN32_WINNT >= 0x0502)