Added full implementations of [Get/Set]ComputerName[A/W].
authorEric Kohl <eric.kohl@reactos.org>
Sun, 8 Jun 2003 21:00:04 +0000 (21:00 +0000)
committerEric Kohl <eric.kohl@reactos.org>
Sun, 8 Jun 2003 21:00:04 +0000 (21:00 +0000)
svn path=/trunk/; revision=4874

reactos/lib/kernel32/makefile
reactos/lib/kernel32/misc/computername.c [new file with mode: 0644]
reactos/lib/kernel32/misc/getname.c [deleted file]
reactos/lib/kernel32/misc/stubs.c

index b4ba6bd..f17fbd9 100644 (file)
@@ -1,4 +1,4 @@
-# $Id: makefile,v 1.66 2003/05/29 00:36:41 hyperion Exp $
+# $Id: makefile,v 1.67 2003/06/08 21:00:04 ekohl Exp $
 
 PATH_TO_TOP = ../..
 
@@ -22,19 +22,19 @@ SYNCH_OBJECTS = synch/critical.o synch/event.o synch/intrlck.o synch/mutex.o \
                 synch/sem.o synch/timer.o synch/wait.o
 
 MISC_OBJECTS = misc/error.o misc/atom.o misc/handle.o misc/env.o \
-              misc/dllmain.o misc/comm.o misc/errormsg.o \
-              misc/console.o misc/time.o misc/toolhelp.o \
-              misc/stubs.o misc/ldr.o misc/res.o \
-              misc/sysinfo.o misc/profile.o \
-              misc/mbchars.o misc/muldiv.o misc/getname.o \
-              misc/perfcnt.o
+               misc/dllmain.o misc/comm.o misc/errormsg.o \
+               misc/console.o misc/time.o misc/toolhelp.o \
+               misc/stubs.o misc/ldr.o misc/res.o \
+               misc/sysinfo.o misc/profile.o \
+               misc/mbchars.o misc/muldiv.o misc/computername.o \
+               misc/perfcnt.o
 
 FILE_OBJECTS = file/file.o file/curdir.o file/lfile.o file/dir.o \
                file/iocompl.o file/volume.o file/deviceio.o file/dosdev.o \
-              file/create.o file/find.o file/copy.o file/pipe.o \
-              file/move.o file/lock.o file/rw.o file/delete.o \
-              file/npipe.o file/tape.o file/mailslot.o file/backup.o \
-              file/cnotify.o
+               file/create.o file/find.o file/copy.o file/pipe.o \
+               file/move.o file/lock.o file/rw.o file/delete.o \
+               file/npipe.o file/tape.o file/mailslot.o file/backup.o \
+               file/cnotify.o
 
 MEM_OBJECTS = mem/global.o mem/heap.o mem/isbad.o mem/local.o \
               mem/procmem.o mem/section.o mem/virtual.o
diff --git a/reactos/lib/kernel32/misc/computername.c b/reactos/lib/kernel32/misc/computername.c
new file mode 100644 (file)
index 0000000..196c62e
--- /dev/null
@@ -0,0 +1,267 @@
+/*
+ *  ReactOS kernel
+ *  Copyright (C) 2003 ReactOS Team
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+/* $Id: computername.c,v 1.1 2003/06/08 20:59:30 ekohl Exp $
+ *
+ * COPYRIGHT:       See COPYING in the top level directory
+ * PROJECT:         ReactOS system libraries
+ * PURPOSE:         Computer name functions
+ * FILE:            lib/kernel32/misc/computername.c
+ * PROGRAMER:       Eric Kohl (ekohl@rz-online.de)
+ */
+
+/* INCLUDES ******************************************************************/
+
+#include <k32.h>
+
+#define NDEBUG
+#include <kernel32/kernel32.h>
+
+
+/* FUNCTIONS *****************************************************************/
+
+BOOL STDCALL
+GetComputerNameA (LPSTR lpBuffer,
+                 LPDWORD lpnSize)
+{
+  UNICODE_STRING UnicodeString;
+  ANSI_STRING AnsiString;
+  BOOL Result;
+
+  AnsiString.MaximumLength = *lpnSize;
+  AnsiString.Length = 0;
+  AnsiString.Buffer = lpBuffer;
+
+  UnicodeString.MaximumLength = *lpnSize * sizeof(WCHAR);
+  UnicodeString.Length = 0;
+  UnicodeString.Buffer = RtlAllocateHeap (RtlGetProcessHeap (),
+                                         0,
+                                         UnicodeString.MaximumLength);
+  if (UnicodeString.Buffer == NULL)
+    {
+      SetLastError (ERROR_OUTOFMEMORY);
+      return FALSE;
+    }
+
+  if (!GetComputerNameW (UnicodeString.Buffer, lpnSize))
+    {
+      RtlFreeUnicodeString (&UnicodeString);
+      return FALSE;
+    }
+
+  UnicodeString.Length = *lpnSize * sizeof(WCHAR);
+
+  RtlUnicodeStringToAnsiString (&AnsiString,
+                               &UnicodeString,
+                               FALSE);
+
+  RtlFreeUnicodeString (&UnicodeString);
+
+  return TRUE;
+}
+
+
+BOOL STDCALL
+GetComputerNameW (LPWSTR lpBuffer,
+                 LPDWORD lpnSize)
+{
+  PKEY_VALUE_PARTIAL_INFORMATION KeyInfo;
+  OBJECT_ATTRIBUTES ObjectAttributes;
+  UNICODE_STRING KeyName;
+  UNICODE_STRING ValueName;
+  HANDLE KeyHandle;
+  ULONG KeyInfoSize;
+  ULONG ReturnSize;
+  NTSTATUS Status;
+
+  RtlInitUnicodeString (&KeyName,
+                       L"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\ComputerName\\ComputerName");
+  InitializeObjectAttributes (&ObjectAttributes,
+                             &KeyName,
+                             OBJ_CASE_INSENSITIVE,
+                             NULL,
+                             NULL);
+  Status = NtOpenKey (&KeyHandle,
+                     KEY_READ,
+                     &ObjectAttributes);
+  if (!NT_SUCCESS(Status))
+    {
+      SetLastErrorByStatus (Status);
+      return FALSE;
+    }
+
+  KeyInfoSize = sizeof(KEY_VALUE_PARTIAL_INFORMATION) +
+               *lpnSize * sizeof(WCHAR);
+  KeyInfo = RtlAllocateHeap (RtlGetProcessHeap (),
+                            0,
+                            KeyInfoSize);
+  if (KeyInfo == NULL)
+    {
+      NtClose (KeyHandle);
+      SetLastError (ERROR_OUTOFMEMORY);
+      return FALSE;
+    }
+
+  RtlInitUnicodeString (&ValueName,
+                       L"ComputerName");
+
+  Status = NtQueryValueKey (KeyHandle,
+                           &ValueName,
+                           KeyValuePartialInformation,
+                           KeyInfo,
+                           KeyInfoSize,
+                           &ReturnSize);
+  if (!NT_SUCCESS(Status))
+    {
+      RtlFreeHeap (RtlGetProcessHeap (),
+                  0,
+                  KeyInfo);
+      NtClose (KeyHandle);
+      SetLastErrorByStatus (Status);
+      return FALSE;
+    }
+
+  *lpnSize =
+    (KeyInfo->DataLength != 0) ? (KeyInfo->DataLength / sizeof(WCHAR)) - 1 : 0;
+
+  RtlCopyMemory (lpBuffer,
+                KeyInfo->Data,
+                KeyInfo->DataLength);
+  lpBuffer[*lpnSize] = 0;
+
+  RtlFreeHeap (RtlGetProcessHeap (),
+              0,
+              KeyInfo);
+  NtClose (KeyHandle);
+
+  return TRUE;
+}
+
+
+BOOL STDCALL
+SetComputerNameA (LPCSTR lpComputerName)
+{
+  UNICODE_STRING ComputerName;
+  BOOL bResult;
+
+  RtlCreateUnicodeStringFromAsciiz (&ComputerName,
+                                   (LPSTR)lpComputerName);
+
+  bResult = SetComputerNameW (ComputerName.Buffer);
+
+  RtlFreeUnicodeString (&ComputerName);
+
+  return bResult;
+}
+
+
+static BOOL
+IsValidComputerName (LPCWSTR lpComputerName)
+{
+  PWCHAR p;
+  ULONG Length;
+
+  Length = 0;
+  p = (PWCHAR)lpComputerName;
+  while (*p != 0)
+    {
+      if (!(iswctype (*p, _ALPHA || _DIGIT) ||
+           *p == L'!' ||
+           *p == L'@' ||
+           *p == L'#' ||
+           *p == L'$' ||
+           *p == L'%' ||
+           *p == L'^' ||
+           *p == L'&' ||
+           *p == L'\'' ||
+           *p == L')' ||
+           *p == L'(' ||
+           *p == L'.' ||
+           *p == L'-' ||
+           *p == L'_' ||
+           *p == L'{' ||
+           *p == L'}' ||
+           *p == L'~'))
+       return FALSE;
+
+      Length++;
+      p++;
+    }
+
+  if (Length == 0 ||
+      Length > MAX_COMPUTERNAME_LENGTH)
+    return FALSE;
+
+  return TRUE;
+}
+
+
+BOOL STDCALL
+SetComputerNameW (LPCWSTR lpComputerName)
+{
+  OBJECT_ATTRIBUTES ObjectAttributes;
+  UNICODE_STRING KeyName;
+  UNICODE_STRING ValueName;
+  HANDLE KeyHandle;
+  NTSTATUS Status;
+
+  if (!IsValidComputerName (lpComputerName))
+    {
+      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;
+    }
+
+  RtlInitUnicodeString (&ValueName,
+                       L"ComputerName");
+
+  Status = NtSetValueKey (KeyHandle,
+                         &ValueName,
+                         0,
+                         REG_SZ,
+                         (PVOID)lpComputerName,
+                         (wcslen (lpComputerName) + 1) * sizeof(WCHAR));
+  if (!NT_SUCCESS(Status))
+    {
+      NtClose (KeyHandle);
+      SetLastErrorByStatus (Status);
+      return FALSE;
+    }
+
+  NtFlushKey (KeyHandle);
+  NtClose (KeyHandle);
+
+  return TRUE;
+}
+
+/* EOF */
diff --git a/reactos/lib/kernel32/misc/getname.c b/reactos/lib/kernel32/misc/getname.c
deleted file mode 100644 (file)
index 034c67b..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-/* $Id: getname.c,v 1.1 2002/12/06 13:14:14 robd Exp $
- *
- */
-#include <windows.h>
-
-
-WINBOOL
-STDCALL
-GetComputerNameW(LPWSTR lpBuffer, LPDWORD nSize)
-{
-    WCHAR Name[MAX_COMPUTERNAME_LENGTH + 1];
-    DWORD Size = 0;
-
-    /*
-     * FIXME: get the computer's name from the registry.
-     */
-    lstrcpyW(Name, L"ROSHost"); /* <-- FIXME -- */
-    Size = lstrlenW(Name) + 1;
-    if (Size > *nSize) {
-        *nSize = Size;
-        SetLastError(ERROR_BUFFER_OVERFLOW);
-        return FALSE;
-    }
-    lstrcpyW(lpBuffer, Name);
-    return TRUE;
-}
-
-
-WINBOOL
-STDCALL
-GetComputerNameA(LPSTR lpBuffer, LPDWORD nSize)
-{
-    WCHAR Name[MAX_COMPUTERNAME_LENGTH + 1];
-    int i;
-
-    if (FALSE == GetComputerNameW(Name, nSize)) {
-        return FALSE;
-    }
-
-/* FIXME --> */
-/* Use UNICODE to ANSI */
-    for (i = 0; Name[i]; ++i) {
-        lpBuffer[i] = (CHAR)Name[i];
-    }
-    lpBuffer[i] = '\0';
-/* FIXME <-- */
-
-    return TRUE;
-}
index 3013f29..72aa445 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: stubs.c,v 1.49 2003/04/20 19:42:11 gvg Exp $
+/* $Id: stubs.c,v 1.50 2003/06/08 20:59:30 ekohl Exp $
  *
  * KERNEL32.DLL stubs (unimplemented functions)
  * Remove from this file, if you implement them.
@@ -805,28 +805,6 @@ RegisterWowExec (
 }
 
 
-WINBOOL
-STDCALL
-SetComputerNameA (
-    LPCSTR  lpComputerName
-    )
-{
-    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
-    return FALSE;
-}
-
-
-WINBOOL
-STDCALL
-SetComputerNameW (
-    LPCWSTR lpComputerName
-    )
-{
-    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
-    return FALSE;
-}
-
-
 #ifndef _OLE2NLS_IN_BUILD_
 
 WINBOOL