Replace RtlNtStatusToDosError(STATUS_NO_MEMORY) with ERROR_OUTOFMEMORY.
[reactos.git] / reactos / lib / user32 / windows / class.c
index b54630f..423b74c 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: class.c,v 1.32 2003/08/19 00:49:42 weiden Exp $
+/* $Id$
  *
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS user32.dll
  * UPDATE HISTORY:
  *      09-05-2001  CSH  Created
  */
-#include <windows.h>
+
 #include <user32.h>
-#include <string.h>
-#include <stdlib.h>
-#include <debug.h>
-#include <window.h>
-#include <strpool.h>
 
-/*
- * @implemented
- */
-WINBOOL
-STDCALL
-GetClassInfoExA(
-  HINSTANCE hinst,
-  LPCSTR lpszClass,
-  LPWNDCLASSEXA lpwcx)
+static BOOL GetClassInfoExCommon(
+    HINSTANCE hInst,
+    LPCWSTR lpszClass,
+    LPWNDCLASSEXW lpwcx,
+    BOOL unicode)
 {
   LPWSTR str;
-  UNICODE_STRING str2;
+  UNICODE_STRING str2, str3;
   WNDCLASSEXW w;
   BOOL retval;
   NTSTATUS Status;
-  
-  if(!lpszClass || !lpwcx)
+
+  if ( !lpszClass || !lpwcx )
   {
     SetLastError(ERROR_INVALID_PARAMETER);
     return FALSE;
   }
-  
+
   if(IS_ATOM(lpszClass))
     str = (LPWSTR)lpszClass;
   else
-    Status = HEAP_strdupAtoW (&str, lpszClass, NULL);
-  if ( !NT_SUCCESS (Status) )
   {
-    SetLastError (RtlNtStatusToDosError(Status));
+    extern BOOL ControlsInitialized;
+
+    if (unicode)
+    {
+        str = HEAP_strdupW ( lpszClass, wcslen(lpszClass) );
+
+        if ( !str )
+        {
+          SetLastError (ERROR_OUTOFMEMORY);
+          return FALSE;
+        }
+    }
+
+    else
+    {
+        Status = HEAP_strdupAtoW(&str, (LPCSTR)lpszClass, NULL);
+
+        if (! NT_SUCCESS(Status))
+        {
+            SetLastError(RtlNtStatusToDosError(Status));
+            return FALSE;
+        }
+    }
+
+    /* Register built-in controls if not already done */
+    if ( !ControlsInitialized )
+    {
+      ControlsInitialized = ControlsInit(str);
+    }
+  }
+
+  str2.Length = str3.Length = 0;
+  str2.MaximumLength = str3.MaximumLength = 255;
+  str2.Buffer = (PWSTR)HEAP_alloc ( str2.MaximumLength * sizeof(WCHAR) );
+  if ( !str2.Buffer )
+  {
+    SetLastError (ERROR_OUTOFMEMORY);
+    if ( !IS_ATOM(str) )
+      HEAP_free ( str );
     return FALSE;
   }
-  
-  str2.Length = 0;
-  str2.MaximumLength = 255;
-  str2.Buffer = (PWSTR)RtlAllocateHeap(RtlGetProcessHeap(), 0, 
-                                       str2.MaximumLength * sizeof(WCHAR));
-  if(!str2.Buffer)
+
+  str3.Buffer = (PWSTR)HEAP_alloc ( str3.MaximumLength * sizeof(WCHAR) );
+  if ( !str3.Buffer )
   {
-    SetLastError (RtlNtStatusToDosError(STATUS_NO_MEMORY));
+    SetLastError (ERROR_OUTOFMEMORY);
+    HEAP_free ( str2.Buffer );
+    if ( !IS_ATOM(str) )
+      HEAP_free ( str );
     return FALSE;
   }
 
-  w.lpszMenuName = (LPCWSTR)&str2;  
-  retval = (BOOL)NtUserGetClassInfo(hinst, str, &w, TRUE, 0);
-  if(!IS_ATOM(str))
+  w.lpszMenuName = (LPCWSTR)&str2;
+  w.lpszClassName = (LPCWSTR)&str3;
+  
+  /* get info about system classes? */
+  if (!hInst) hInst = User32Instance;
+  
+  retval = (BOOL)NtUserGetClassInfo(hInst, str, &w, TRUE, 0);
+  
+  w.hInstance = (hInst == User32Instance) ? 0 : hInst;
+  
+  if ( !IS_ATOM(str) )
     HEAP_free(str);
+
   RtlCopyMemory ( lpwcx, &w, sizeof(WNDCLASSEXW) );
 
-  if (!IS_INTRESOURCE(w.lpszMenuName) && w.lpszMenuName)
+  if ( !IS_INTRESOURCE(w.lpszMenuName) && w.lpszMenuName )
+  {
+    if (unicode)
+        lpwcx->lpszMenuName = heap_string_poolW ( str2.Buffer, str2.Length );
+    else
+        ((LPWNDCLASSEXA) lpwcx)->lpszMenuName = heap_string_poolA ( str2.Buffer, str2.Length );
+  }
+
+  if ( !IS_ATOM(w.lpszClassName) && w.lpszClassName )
   {
-    lpwcx->lpszMenuName = heap_string_poolA (str2.Buffer, str2.Length);
+    if (unicode)
+        lpwcx->lpszClassName = heap_string_poolW ( str3.Buffer, str3.Length );
+    else
+        ((LPWNDCLASSEXA) lpwcx)->lpszClassName = heap_string_poolA ( str3.Buffer, str3.Length );
   }
-  RtlFreeHeap(RtlGetProcessHeap(), 0, str2.Buffer);
+
+  HEAP_free ( str2.Buffer );
+  HEAP_free ( str3.Buffer );
+
   return retval;
 }
 
@@ -76,87 +126,80 @@ GetClassInfoExA(
 /*
  * @implemented
  */
-WINBOOL
+BOOL
+STDCALL
+GetClassInfoExA(
+  HINSTANCE hinst,
+  LPCSTR lpszClass,
+  LPWNDCLASSEXA lpwcx)
+{
+    return GetClassInfoExCommon(hinst, (LPWSTR)lpszClass, (LPWNDCLASSEXW)lpwcx, FALSE);
+}
+
+
+/*
+ * @implemented
+ */
+BOOL
 STDCALL
 GetClassInfoExW(
   HINSTANCE hinst,
   LPCWSTR lpszClass,
   LPWNDCLASSEXW lpwcx)
 {
-  LPWSTR str;
-  UNICODE_STRING str2;
-  WNDCLASSEXW w;
-  WINBOOL retval;
-  
-  if(!lpszClass || !lpwcx)
-  {
-    SetLastError(ERROR_INVALID_PARAMETER);
-    return FALSE;
-  }
-  
-  if(IS_ATOM(lpszClass))
-    str = (LPWSTR)lpszClass;
-  else
-    str = HEAP_strdupW (lpszClass, wcslen(lpszClass) );
-
-  str2.Length = 0;
-  str2.MaximumLength = 255;
-  str2.Buffer = (PWSTR)RtlAllocateHeap(RtlGetProcessHeap(), 0, 
-                                       str2.MaximumLength * sizeof(WCHAR));
-  if(!str2.Buffer)
-  {
-    SetLastError (RtlNtStatusToDosError(STATUS_NO_MEMORY));
-    return FALSE;
-  }
-
-  w.lpszMenuName = (LPCWSTR)&str2;  
-  retval = (BOOL)NtUserGetClassInfo(hinst, str, &w, TRUE, 0);
-  if(!IS_ATOM(str))
-    HEAP_free(str);
-  RtlCopyMemory ( lpwcx, &w, sizeof(WNDCLASSEXW) );
-
-  if (!IS_INTRESOURCE(w.lpszMenuName) && w.lpszMenuName)
-  {
-    lpwcx->lpszMenuName = heap_string_poolW (str2.Buffer, str2.Length);
-  }
-
-  RtlFreeHeap(RtlGetProcessHeap(), 0, str2.Buffer);
-  return retval;
+    return GetClassInfoExCommon(hinst, lpszClass, lpwcx, TRUE);
 }
 
 
 /*
  * @implemented
  */
-WINBOOL
+BOOL
 STDCALL
 GetClassInfoA(
   HINSTANCE hInstance,
   LPCSTR lpClassName,
   LPWNDCLASSA lpWndClass)
 {
-       WNDCLASSEXA w;
-       WINBOOL retval;
-       retval = GetClassInfoExA(hInstance,lpClassName,&w);
-    RtlCopyMemory (lpWndClass,&w.style,sizeof(WNDCLASSA));
-       return retval;
+  WNDCLASSEXA w;
+  BOOL retval;
+
+  if ( !lpClassName || !lpWndClass )
+  {
+    SetLastError(ERROR_INVALID_PARAMETER);
+    return FALSE;
+  }
+
+  retval = GetClassInfoExA(hInstance,lpClassName,&w);
+  if (retval)
+  {
+    RtlCopyMemory ( lpWndClass, &w.style, sizeof(WNDCLASSA) );
+  }
+  return retval;
 }
 
 /*
  * @implemented
  */
-WINBOOL
+BOOL
 STDCALL
 GetClassInfoW(
   HINSTANCE hInstance,
   LPCWSTR lpClassName,
   LPWNDCLASSW lpWndClass)
 {
-       WNDCLASSEXW w;
-       WINBOOL retval;
-       retval = GetClassInfoExW(hInstance,lpClassName,&w);
-    RtlCopyMemory (lpWndClass,&w.style,sizeof(WNDCLASSW));
-       return retval;
+  WNDCLASSEXW w;
+  BOOL retval;
+
+  if(!lpClassName || !lpWndClass)
+  {
+    SetLastError(ERROR_INVALID_PARAMETER);
+    return FALSE;
+  }
+
+  retval = GetClassInfoExW(hInstance,lpClassName,&w);
+  RtlCopyMemory (lpWndClass,&w.style,sizeof(WNDCLASSW));
+  return retval;
 }
 
 
@@ -164,24 +207,31 @@ GetClassInfoW(
  * @implemented
  */
 DWORD STDCALL
-GetClassLongA ( HWND hWnd, int nIndex )
-{ 
-  PUNICODE_STRING str;
-
-  if ( nIndex != GCL_MENUNAME )
-  {
-    return NtUserGetClassLong ( hWnd, nIndex, TRUE );
-  }
-
-  str = (PUNICODE_STRING)NtUserGetClassLong ( hWnd, nIndex, TRUE );
-  if ( IS_INTRESOURCE(str) )
-  {
-    return (DWORD)str;
-  }
-  else
-  {
-    return (DWORD)heap_string_poolA ( str->Buffer, str->Length );
-  }
+GetClassLongA(HWND hWnd, int nIndex)
+{
+   switch (nIndex)
+   {
+      case GCL_HBRBACKGROUND:
+         {
+            DWORD hBrush = NtUserGetClassLong(hWnd, GCL_HBRBACKGROUND, TRUE);
+            if (hBrush != 0 && hBrush < 0x4000)
+               hBrush = (DWORD)GetSysColorBrush((ULONG)hBrush - 1);
+            return hBrush;
+         }
+
+      case GCL_MENUNAME:
+         {
+            PUNICODE_STRING Name;
+            Name = (PUNICODE_STRING)NtUserGetClassLong(hWnd, nIndex, TRUE);
+            if (IS_INTRESOURCE(Name))
+               return (DWORD)Name;
+            else
+               return (DWORD)heap_string_poolA(Name->Buffer, Name->Length);
+         }
+
+      default:
+         return NtUserGetClassLong(hWnd, nIndex, TRUE);
+   }
 }
 
 /*
@@ -190,22 +240,29 @@ GetClassLongA ( HWND hWnd, int nIndex )
 DWORD STDCALL
 GetClassLongW ( HWND hWnd, int nIndex )
 {
-  PUNICODE_STRING str;
-
-  if ( nIndex != GCL_MENUNAME )
-  {
-    return NtUserGetClassLong ( hWnd, nIndex, FALSE );
-  }
-
-  str = (PUNICODE_STRING)NtUserGetClassLong(hWnd, nIndex, TRUE);
-  if ( IS_INTRESOURCE(str) )
-  {
-    return (DWORD)str;
-  }
-  else
-  {
-    return (DWORD)heap_string_poolW ( str->Buffer, str->Length );
-  }
+   switch (nIndex)
+   {
+      case GCL_HBRBACKGROUND:
+         {
+            DWORD hBrush = NtUserGetClassLong(hWnd, GCL_HBRBACKGROUND, TRUE);
+            if (hBrush != 0 && hBrush < 0x4000)
+               hBrush = (DWORD)GetSysColorBrush((ULONG)hBrush - 1);
+            return hBrush;
+         }
+
+      case GCL_MENUNAME:
+         {
+            PUNICODE_STRING Name;
+            Name = (PUNICODE_STRING)NtUserGetClassLong(hWnd, nIndex, FALSE);
+            if (IS_INTRESOURCE(Name))
+               return (DWORD)Name;
+            else
+               return (DWORD)heap_string_poolW(Name->Buffer, Name->Length);
+         }
+
+      default:
+         return NtUserGetClassLong(hWnd, nIndex, FALSE);
+   }
 }
 
 
@@ -222,6 +279,9 @@ GetClassNameA(
   LPWSTR ClassNameW;
   NTSTATUS Status;
 
+  if(!lpClassName)
+    return 0;
+
   ClassNameW = HEAP_alloc ( (nMaxCount+1)*sizeof(WCHAR) );
 
   result = NtUserGetClassName ( hWnd, ClassNameW, nMaxCount );
@@ -247,23 +307,12 @@ GetClassNameW(
   LPWSTR lpClassName,
   int nMaxCount)
 {
-  int result;
-  LPWSTR ClassNameW;
-
-  ClassNameW = HEAP_alloc ( (nMaxCount+1) * sizeof(WCHAR) );
-
-  result = NtUserGetClassName ( hWnd, ClassNameW, nMaxCount );
-
-  RtlCopyMemory ( lpClassName, ClassNameW, result );
-
-  HEAP_free ( ClassNameW );
-
-  return result;
+   return NtUserGetClassName(hWnd, lpClassName, nMaxCount);
 }
 
 
 /*
- * @unimplemented
+ * @implemented
  */
 WORD
 STDCALL
@@ -274,8 +323,10 @@ GetClassWord(
  * NOTE: Obsoleted in 32-bit windows
  */
 {
-  UNIMPLEMENTED;
-  return 0;
+    if ((nIndex < 0) && (nIndex != GCW_ATOM))
+        return 0;
+
+    return (WORD) NtUserGetClassLong ( hWnd, nIndex, TRUE );
 }
 
 
@@ -350,23 +401,167 @@ RealGetWindowClassA(
 }
 
 /*
- * @implemented
+ * Create a small icon based on a standard icon
  */
-ATOM
-STDCALL
-RegisterClassA(CONST WNDCLASSA *lpWndClass)
+static HICON
+CreateSmallIcon(HICON StdIcon)
 {
-  WNDCLASSEXA Class;
-
-  if ( !lpWndClass )
-    return 0;
-
-  RtlCopyMemory ( &Class.style, lpWndClass, sizeof(WNDCLASSA) );
-
-  Class.cbSize = sizeof(WNDCLASSEXA);
-  Class.hIconSm = INVALID_HANDLE_VALUE;
-
-  return RegisterClassExA ( &Class );
+   HICON SmallIcon = NULL;
+   ICONINFO StdInfo;
+   int SmallIconWidth;
+   int SmallIconHeight;
+   BITMAP StdBitmapInfo;
+   HDC hInfoDc = NULL;
+   HDC hSourceDc = NULL;
+   HDC hDestDc = NULL;
+   ICONINFO SmallInfo;
+   HBITMAP OldSourceBitmap = NULL;
+   HBITMAP OldDestBitmap = NULL;
+
+   SmallInfo.hbmColor = NULL;
+   SmallInfo.hbmMask = NULL;
+
+   /* We need something to work with... */
+   if (NULL == StdIcon)
+   {
+      goto cleanup;
+   }
+
+   SmallIconWidth = GetSystemMetrics(SM_CXSMICON);
+   SmallIconHeight = GetSystemMetrics(SM_CYSMICON);
+   if (! GetIconInfo(StdIcon, &StdInfo))
+   {
+      DPRINT1("Failed to get icon info for icon 0x%x\n", StdIcon);
+      goto cleanup;
+   }
+   if (! GetObjectW(StdInfo.hbmMask, sizeof(BITMAP), &StdBitmapInfo))
+   {
+      DPRINT1("Failed to get bitmap info for icon 0x%x bitmap 0x%x\n",
+              StdIcon, StdInfo.hbmColor);
+      goto cleanup;
+   }
+   if (StdBitmapInfo.bmWidth == SmallIconWidth &&
+       StdBitmapInfo.bmHeight == SmallIconHeight)
+   {
+      /* Icon already has the correct dimensions */
+      return StdIcon;
+   }
+
+   /* Get a handle to a info DC and handles to DCs which can be used to
+      select a bitmap into. This is done to avoid triggering a switch to
+      graphics mode (if we're currently in text/blue screen mode) */
+   hInfoDc = CreateICW(NULL, NULL, NULL, NULL);
+   if (NULL == hInfoDc)
+   {
+      DPRINT1("Failed to create info DC\n");
+      goto cleanup;
+   }
+   hSourceDc = CreateCompatibleDC(NULL);
+   if (NULL == hSourceDc)
+   {
+      DPRINT1("Failed to create source DC\n");
+      goto cleanup;
+   }
+   hDestDc = CreateCompatibleDC(NULL);
+   if (NULL == hDestDc)
+   {
+      DPRINT1("Failed to create dest DC\n");
+      goto cleanup;
+   }
+
+   OldSourceBitmap = SelectObject(hSourceDc, StdInfo.hbmColor);
+   if (NULL == OldSourceBitmap)
+   {
+      DPRINT1("Failed to select source color bitmap\n");
+      goto cleanup;
+   }
+   SmallInfo.hbmColor = CreateCompatibleBitmap(hInfoDc, SmallIconWidth,
+                                              SmallIconHeight);
+   if (NULL == SmallInfo.hbmColor)
+   {
+      DPRINT1("Failed to create color bitmap\n");
+      goto cleanup;
+   }
+   OldDestBitmap = SelectObject(hDestDc, SmallInfo.hbmColor);
+   if (NULL == OldDestBitmap)
+   {
+      DPRINT1("Failed to select dest color bitmap\n");
+      goto cleanup;
+   }
+   if (! StretchBlt(hDestDc, 0, 0, SmallIconWidth, SmallIconHeight,
+                    hSourceDc, 0, 0, StdBitmapInfo.bmWidth,
+                    StdBitmapInfo.bmHeight, SRCCOPY))
+   {
+     DPRINT1("Failed to stretch color bitmap\n");
+     goto cleanup;
+   }
+
+   if (NULL == SelectObject(hSourceDc, StdInfo.hbmMask))
+   {
+      DPRINT1("Failed to select source mask bitmap\n");
+      goto cleanup;
+   }
+   SmallInfo.hbmMask = CreateBitmap(SmallIconWidth, SmallIconHeight, 1, 1,
+                                    NULL);
+   if (NULL == SmallInfo.hbmMask)
+   {
+      DPRINT1("Failed to create mask bitmap\n");
+      goto cleanup;
+   }
+   if (NULL == SelectObject(hDestDc, SmallInfo.hbmMask))
+   {
+      DPRINT1("Failed to select dest mask bitmap\n");
+      goto cleanup;
+   }
+   if (! StretchBlt(hDestDc, 0, 0, SmallIconWidth, SmallIconHeight,
+                    hSourceDc, 0, 0, StdBitmapInfo.bmWidth,
+                    StdBitmapInfo.bmHeight, SRCCOPY))
+   {
+      DPRINT1("Failed to stretch mask bitmap\n");
+      goto cleanup;
+   }
+
+   SmallInfo.fIcon = TRUE;
+   SmallInfo.xHotspot = SmallIconWidth / 2;
+   SmallInfo.yHotspot = SmallIconHeight / 2;
+   SmallIcon = CreateIconIndirect(&SmallInfo);
+   if (NULL == SmallIcon)
+   {
+      DPRINT1("Failed to create icon\n");
+      goto cleanup;
+   }
+
+cleanup:
+   if (NULL != SmallInfo.hbmMask)
+   {
+      DeleteObject(SmallInfo.hbmMask);
+   }
+   if (NULL != OldDestBitmap)
+   {
+      SelectObject(hDestDc, OldDestBitmap);
+   }
+   if (NULL != SmallInfo.hbmColor)
+   {
+      DeleteObject(SmallInfo.hbmColor);
+   }
+   if (NULL != hDestDc)
+   {
+      DeleteDC(hDestDc);
+   }
+   if (NULL != OldSourceBitmap)
+   {
+      SelectObject(hSourceDc, OldSourceBitmap);
+   }
+   if (NULL != hSourceDc)
+   {
+      DeleteDC(hSourceDc);
+   }
+   if (NULL != hInfoDc)
+   {
+      DeleteDC(hInfoDc);
+   }
+
+   return SmallIcon;
 }
 
 /*
@@ -375,54 +570,85 @@ RegisterClassA(CONST WNDCLASSA *lpWndClass)
 ATOM STDCALL
 RegisterClassExA(CONST WNDCLASSEXA *lpwcx)
 {
-  RTL_ATOM Atom;
-  WNDCLASSEXW wndclass;
-  NTSTATUS Status;
-  LPWSTR ClassName = NULL;
-  LPWSTR MenuName = NULL;
-
-  if ( !lpwcx || (lpwcx->cbSize != sizeof(WNDCLASSEXA)) )
-    return 0;
-
-  if ( !lpwcx->lpszClassName )
-    return 0;
-
-  RtlCopyMemory ( &wndclass, lpwcx, sizeof(WNDCLASSEXW) );
-
-  if ( !IS_ATOM(lpwcx->lpszClassName) )
-  {
-    Status = HEAP_strdupAtoW ( &ClassName, (LPCSTR)lpwcx->lpszClassName, NULL );
-    if ( !NT_SUCCESS (Status) )
-    {
-      SetLastError (RtlNtStatusToDosError(Status));
+   RTL_ATOM Atom;
+   WNDCLASSEXA WndClass;
+   UNICODE_STRING ClassName;
+   UNICODE_STRING MenuName;
+   HMENU hMenu;
+
+   if (lpwcx == NULL || lpwcx->cbSize != sizeof(WNDCLASSEXA) ||
+       lpwcx->cbClsExtra < 0 || lpwcx->cbWndExtra < 0 ||
+       lpwcx->lpszClassName == NULL)
+   {
+      SetLastError(ERROR_INVALID_PARAMETER);
       return 0;
-    }
-    wndclass.lpszClassName = ClassName;
-  }
-
-  if ( !IS_INTRESOURCE(lpwcx->lpszMenuName) )
-  {
-    Status = HEAP_strdupAtoW ( &MenuName, (LPCSTR)lpwcx->lpszMenuName, NULL );
-    if ( !NT_SUCCESS (Status) )
-    {
-      if ( ClassName )
-       HEAP_free ( ClassName );
-      SetLastError (RtlNtStatusToDosError(Status));
+   }
+
+   /*
+    * On real Windows this looks more like:
+    *    if (lpwcx->hInstance == User32Instance &&
+    *        *(PULONG)((ULONG_PTR)NtCurrentTeb() + 0x6D4) & 0x400)
+    * But since I have no idea what the magic field in the
+    * TEB structure means, I rather decided to omit that.
+    * -- Filip Navara
+    */
+   if (lpwcx->hInstance == User32Instance)
+   {
+      SetLastError(ERROR_INVALID_PARAMETER);
       return 0;
-    }
-    wndclass.lpszMenuName = MenuName;
-  }
+   }
 
-  Atom = NtUserRegisterClassExWOW ( &wndclass, FALSE, 0, 0, 0 );
+   /* Yes, this is correct. We should modify the passed structure. */
+   if (lpwcx->hInstance == NULL)
+      ((WNDCLASSEXA*)lpwcx)->hInstance = GetModuleHandleW(NULL);
 
-  /* free strings if neccessary */
-  if ( MenuName  ) HEAP_free ( MenuName );
-  if ( ClassName ) HEAP_free ( ClassName );
-
-  return (ATOM)Atom;
-}
+   RtlCopyMemory(&WndClass, lpwcx, sizeof(WNDCLASSEXA));
 
+   if (NULL == WndClass.hIconSm)
+   {
+      WndClass.hIconSm = CreateSmallIcon(WndClass.hIcon);
+   }
 
+   if HIWORD(lpwcx->lpszMenuName)
+   {
+      hMenu = 0;
+      RtlCreateUnicodeStringFromAsciiz(&MenuName, WndClass.lpszMenuName);
+    }
+   else
+   {
+      MenuName.Length =
+      MenuName.MaximumLength = 0;
+      MenuName.Buffer = (LPWSTR)WndClass.lpszMenuName;
+      hMenu = LoadMenuA(WndClass.hInstance, lpwcx->lpszMenuName);
+   }
+   if (IS_ATOM(WndClass.lpszClassName))
+   {
+      ClassName.Length =
+      ClassName.MaximumLength = 0;
+      ClassName.Buffer = (LPWSTR)WndClass.lpszClassName;
+   } else
+   {
+      RtlCreateUnicodeStringFromAsciiz(&ClassName, WndClass.lpszClassName);
+   }
+
+   Atom = NtUserRegisterClassExWOW(
+      (WNDCLASSEXW*)&WndClass,
+      &ClassName,
+      &ClassName,
+      &MenuName,
+      NULL,
+      REGISTERCLASS_ANSI,
+      0,
+      hMenu);
+
+   if (!IS_ATOM(WndClass.lpszMenuName))
+      RtlFreeUnicodeString(&MenuName);
+   if (!IS_ATOM(WndClass.lpszClassName))
+      RtlFreeUnicodeString(&ClassName);
+
+   return (ATOM)Atom;
+}
 
 /*
  * @implemented
@@ -430,54 +656,94 @@ RegisterClassExA(CONST WNDCLASSEXA *lpwcx)
 ATOM STDCALL
 RegisterClassExW(CONST WNDCLASSEXW *lpwcx)
 {
-  RTL_ATOM Atom;
-  HANDLE hHeap;
-  WNDCLASSEXW wndclass;
-  LPWSTR ClassName = NULL;
-  LPWSTR MenuName = NULL;
-
-  if ( !lpwcx || (lpwcx->cbSize != sizeof(WNDCLASSEXA)) )
-    return 0;
+   WNDCLASSEXW WndClass;
+   UNICODE_STRING ClassName;
+   UNICODE_STRING MenuName;
+   HMENU hMenu;
+
+   if (lpwcx == NULL || lpwcx->cbSize != sizeof(WNDCLASSEXW) ||
+       lpwcx->cbClsExtra < 0 || lpwcx->cbWndExtra < 0 ||
+       lpwcx->lpszClassName == NULL)
+   {
+      SetLastError(ERROR_INVALID_PARAMETER);
+      return 0;
+   }
+
+   /*
+    * On real Windows this looks more like:
+    *    if (lpwcx->hInstance == User32Instance &&
+    *        *(PULONG)((ULONG_PTR)NtCurrentTeb() + 0x6D4) & 0x400)
+    * But since I have no idea what the magic field in the
+    * TEB structure means, I rather decided to omit that.
+    * -- Filip Navara
+    */
+   if (lpwcx->hInstance == User32Instance)
+   {
+      SetLastError(ERROR_INVALID_PARAMETER);
+      return 0;
+   }
 
-  if ( !lpwcx->lpszClassName )
-    return 0;
+   /* Yes, this is correct. We should modify the passed structure. */
+   if (lpwcx->hInstance == NULL)
+      ((WNDCLASSEXW*)lpwcx)->hInstance = GetModuleHandleW(NULL);
 
-  hHeap = RtlGetProcessHeap();
-  RtlCopyMemory ( &wndclass, lpwcx, sizeof(WNDCLASSEXW) );
+   RtlCopyMemory(&WndClass, lpwcx, sizeof(WNDCLASSEXW));
 
-  /* copy strings if needed */
+   if (NULL == WndClass.hIconSm)
+   {
+      WndClass.hIconSm = CreateSmallIcon(WndClass.hIcon);
+   }
 
-  if ( !IS_ATOM(lpwcx->lpszClassName) )
-  {
-    ClassName = HEAP_strdupW ( lpwcx->lpszClassName, lstrlenW(lpwcx->lpszClassName) );
-    if ( !ClassName )
-    {
-      SetLastError(RtlNtStatusToDosError(STATUS_NO_MEMORY));
-      return 0;
+   if HIWORD(lpwcx->lpszMenuName)
+   {
+      hMenu = 0;
+      RtlInitUnicodeString(&MenuName, WndClass.lpszMenuName);
     }
-    wndclass.lpszClassName = ClassName;
-  }
+   else
+   {
+      MenuName.Length =
+      MenuName.MaximumLength = 0;
+      MenuName.Buffer = (LPWSTR)WndClass.lpszMenuName;
+      hMenu = LoadMenuW(WndClass.hInstance, lpwcx->lpszMenuName);
+   }
+
+   if (IS_ATOM(WndClass.lpszClassName))
+   {
+      ClassName.Length =
+      ClassName.MaximumLength = 0;
+      ClassName.Buffer = (LPWSTR)WndClass.lpszClassName;
+   } else
+   {
+      RtlInitUnicodeString(&ClassName, WndClass.lpszClassName);
+   }
+
+   return (ATOM)NtUserRegisterClassExWOW(
+      &WndClass,
+      &ClassName,
+      &ClassName,
+      &MenuName,
+      NULL,
+      0,
+      0,
+      hMenu);
+}
 
-  if ( !IS_INTRESOURCE(lpwcx->lpszMenuName) )
-  {
-    MenuName = HEAP_strdupW ( lpwcx->lpszMenuName, lstrlenW(lpwcx->lpszMenuName) );
-    if ( !MenuName )
-    {
-      if ( ClassName )
-       HEAP_free ( MenuName );
-      SetLastError(RtlNtStatusToDosError(STATUS_NO_MEMORY));
-      return 0;
-    }
-    wndclass.lpszMenuName = MenuName;
-  }
+/*
+ * @implemented
+ */
+ATOM STDCALL
+RegisterClassA(CONST WNDCLASSA *lpWndClass)
+{
+   WNDCLASSEXA Class;
 
-  Atom = NtUserRegisterClassExWOW ( &wndclass, TRUE, 0, 0, 0 );
+   if (lpWndClass == NULL)
+      return 0;
 
-  /* free strings if neccessary */
-  if ( MenuName  ) HEAP_free ( MenuName  );
-  if ( ClassName ) HEAP_free ( ClassName );
+   RtlCopyMemory(&Class.style, lpWndClass, sizeof(WNDCLASSA));
+   Class.cbSize = sizeof(WNDCLASSEXA);
+   Class.hIconSm = NULL;
 
-  return (ATOM)Atom;
+   return RegisterClassExA(&Class);
 }
 
 /*
@@ -486,17 +752,16 @@ RegisterClassExW(CONST WNDCLASSEXW *lpwcx)
 ATOM STDCALL
 RegisterClassW(CONST WNDCLASSW *lpWndClass)
 {
-  WNDCLASSEXW Class;
-
-  if ( !lpWndClass )
-    return 0;
+   WNDCLASSEXW Class;
 
-  RtlCopyMemory ( &Class.style, lpWndClass, sizeof(WNDCLASSW) );
+   if (lpWndClass == NULL)
+      return 0;
 
-  Class.cbSize = sizeof(WNDCLASSEXW);
-  Class.hIconSm = INVALID_HANDLE_VALUE;
+   RtlCopyMemory(&Class.style, lpWndClass, sizeof(WNDCLASSW));
+   Class.cbSize = sizeof(WNDCLASSEXW);
+   Class.hIconSm = NULL;
 
-  return RegisterClassExW ( &Class );
+   return RegisterClassExW(&Class);
 }
 
 /*
@@ -509,8 +774,9 @@ SetClassLongA (
   int nIndex,
   LONG dwNewLong)
 {
+  UNICODE_STRING str2buf;
   PUNICODE_STRING str;
-  PUNICODE_STRING str2;
+  PUNICODE_STRING str2 = &str2buf;
 
   if ( nIndex != GCL_MENUNAME )
   {
@@ -522,7 +788,7 @@ SetClassLongA (
   }
   else
   {
-    RtlCreateUnicodeString ( str2, (LPWSTR)dwNewLong );
+    RtlCreateUnicodeStringFromAsciiz ( &str2buf,(LPSTR)dwNewLong );
   }
 
   str = (PUNICODE_STRING)NtUserSetClassLong(hWnd, nIndex, (DWORD)str2, TRUE);
@@ -552,8 +818,9 @@ SetClassLongW(
   int nIndex,
   LONG dwNewLong)
 {
+  UNICODE_STRING str2buf;
   PUNICODE_STRING str;
-  PUNICODE_STRING str2;
+  PUNICODE_STRING str2 = &str2buf;
 
   if (nIndex != GCL_MENUNAME )
   {
@@ -565,7 +832,7 @@ SetClassLongW(
   }
   else
   {
-    RtlCreateUnicodeStringFromAsciiz ( str2,(LPSTR)dwNewLong );
+    RtlCreateUnicodeString ( &str2buf, (LPWSTR)dwNewLong );
   }
 
   str = (PUNICODE_STRING)NtUserSetClassLong(hWnd, nIndex, (DWORD)str2, TRUE);
@@ -586,7 +853,7 @@ SetClassLongW(
 
 
 /*
- * @unimplemented
+ * @implemented
  */
 WORD
 STDCALL
@@ -598,8 +865,10 @@ SetClassWord(
  * NOTE: Obsoleted in 32-bit windows
  */
 {
-  UNIMPLEMENTED;
-  return 0;
+    if ((nIndex < 0) && (nIndex != GCW_ATOM))
+        return 0;
+
+    return (WORD) NtUserSetClassLong ( hWnd, nIndex, wNewWord, TRUE );
 }
 
 
@@ -632,30 +901,49 @@ SetWindowLongW(
 
 
 /*
- * @unimplemented
+ * @implemented
  */
-WINBOOL
+BOOL
 STDCALL
 UnregisterClassA(
   LPCSTR lpClassName,
   HINSTANCE hInstance)
 {
-  UNIMPLEMENTED;
-  return FALSE;
+  LPWSTR ClassName;
+  NTSTATUS Status;
+  BOOL Result;
+
+  if(!IS_ATOM(lpClassName))
+  {
+    Status = HEAP_strdupAtoW(&ClassName, lpClassName, NULL);
+    if(!NT_SUCCESS(Status))
+    {
+      SetLastError(RtlNtStatusToDosError(Status));
+      return FALSE;
+    }
+  }
+  else
+    ClassName = (LPWSTR)lpClassName;
+
+  Result = (BOOL)NtUserUnregisterClass((LPCWSTR)ClassName, hInstance, 0);
+
+  if(ClassName && !IS_ATOM(lpClassName))
+    HEAP_free(ClassName);
+
+  return Result;
 }
 
 
 /*
- * @unimplemented
+ * @implemented
  */
-WINBOOL
+BOOL
 STDCALL
 UnregisterClassW(
   LPCWSTR lpClassName,
   HINSTANCE hInstance)
 {
-  UNIMPLEMENTED;
-  return FALSE;
+  return (BOOL)NtUserUnregisterClass(lpClassName, hInstance, 0);
 }
 
 /* EOF */