/*
 * COPYRIGHT:        See COPYING in the top level directory
 * PROJECT:          ReactOS Win32k subsystem
 * PURPOSE:          Window classes
 * FILE:             subsystems/win32/win32k/ntuser/class.c
 * PROGRAMER:        Thomas Weidenmueller <w3seek@reactos.com>
 */

#include <win32k.h>
DBG_DEFAULT_CHANNEL(UserClass);

REGISTER_SYSCLASS DefaultServerClasses[] =
{
  { ((PWSTR)((ULONG_PTR)(WORD)(0x8001))),
    CS_GLOBALCLASS|CS_DBLCLKS,
    NULL,
    0,
    IDC_ARROW,
    (HBRUSH)(COLOR_BACKGROUND + 1),
    FNID_DESKTOP,
    ICLS_DESKTOP
  },
  { ((PWSTR)((ULONG_PTR)(WORD)(0x8003))),
    CS_VREDRAW|CS_HREDRAW|CS_SAVEBITS,
    NULL, // Use User32 procs
    sizeof(LONG),
    IDC_ARROW,
    NULL,
    FNID_SWITCH,
    ICLS_SWITCH
  },
  { ((PWSTR)((ULONG_PTR)(WORD)(0x8000))),
    CS_DBLCLKS|CS_SAVEBITS,
    NULL, // Use User32 procs
    sizeof(LONG),
    IDC_ARROW,
    (HBRUSH)(COLOR_MENU + 1),
    FNID_MENU,
    ICLS_MENU
  },
  { L"ScrollBar",
    CS_DBLCLKS|CS_VREDRAW|CS_HREDRAW|CS_PARENTDC,
    NULL, // Use User32 procs
    sizeof(SBWND)-sizeof(WND),
    IDC_ARROW,
    NULL,
    FNID_SCROLLBAR,
    ICLS_SCROLLBAR
  },
  { ((PWSTR)((ULONG_PTR)(WORD)(0x8004))), // IconTitle is here for now...
    0,
    NULL, // Use User32 procs
    0,
    IDC_ARROW,
    0,
    FNID_ICONTITLE,
    ICLS_ICONTITLE
  },
  { L"Message",
    CS_GLOBALCLASS,
    NULL, // Use User32 procs
    0,
    IDC_ARROW,
    NULL,
    FNID_MESSAGEWND,
    ICLS_HWNDMESSAGE
  }  
};

static struct
{
    int FnId;
    int ClsId;
}  FnidToiCls[] =
{ /* Function Ids to Class indexes. */
 { FNID_SCROLLBAR,  ICLS_SCROLLBAR},
 { FNID_ICONTITLE,  ICLS_ICONTITLE},
 { FNID_MENU,       ICLS_MENU},
 { FNID_DESKTOP,    ICLS_DESKTOP},
 { FNID_SWITCH,     ICLS_SWITCH},
 { FNID_MESSAGEWND, ICLS_HWNDMESSAGE},
 { FNID_BUTTON,     ICLS_BUTTON},
 { FNID_COMBOBOX,   ICLS_COMBOBOX},
 { FNID_COMBOLBOX,  ICLS_COMBOLBOX},
 { FNID_DIALOG,     ICLS_DIALOG},  
 { FNID_EDIT,       ICLS_EDIT}, 
 { FNID_LISTBOX,    ICLS_LISTBOX},
 { FNID_MDICLIENT,  ICLS_MDICLIENT},
 { FNID_STATIC,     ICLS_STATIC},
 { FNID_IME,        ICLS_IME},
 { FNID_GHOST,      ICLS_GHOST},
 { FNID_TOOLTIPS,   ICLS_TOOLTIPS}
};

BOOL
FASTCALL
LookupFnIdToiCls(int FnId, int *iCls )
{
  int i;
  
  for ( i = 0; i < ARRAYSIZE(FnidToiCls); i++)
  {
     if (FnidToiCls[i].FnId == FnId)
     {
        if (iCls) *iCls = FnidToiCls[i].ClsId;
        return TRUE;
     }
  }
  if (iCls) *iCls = 0;
  return FALSE;
}

/* WINDOWCLASS ***************************************************************/

static VOID
IntFreeClassMenuName(IN OUT PCLS Class)
{
    /* Free the menu name, if it was changed and allocated */
    if (Class->lpszClientUnicodeMenuName != NULL && Class->MenuNameIsString)
    {
        UserHeapFree(Class->lpszClientUnicodeMenuName);
        Class->lpszClientUnicodeMenuName = NULL;
        Class->lpszClientAnsiMenuName = NULL;
    }
}

static VOID
IntDestroyClass(IN OUT PCLS Class)
{
    PDESKTOP pDesk;
    /* There shouldn't be any clones anymore */
    ASSERT(Class->cWndReferenceCount == 0);
    ASSERT(Class->pclsClone == NULL);

    if (Class->pclsBase == Class)
    {
        PCALLPROCDATA CallProc, NextCallProc;

        /* Destroy allocated callproc handles */
        CallProc = Class->spcpdFirst;
        while (CallProc != NULL)
        {
            NextCallProc = CallProc->spcpdNext;

            CallProc->spcpdNext = NULL;
            DestroyCallProc(NULL,
                            CallProc);

            CallProc = NextCallProc;
        }

        if (Class->pdce)
        {
           DceFreeClassDCE(((PDCE)Class->pdce)->hDC);
           Class->pdce = NULL;
        }

        IntFreeClassMenuName(Class);
    }

    pDesk = Class->rpdeskParent;
    Class->rpdeskParent = NULL;

    /* Free the structure */
    if (pDesk != NULL)
    {
        DesktopHeapFree(pDesk, Class);
    }
    else
    {
        UserHeapFree(Class);
    }
}


/* Clean all process classes. all process windows must cleaned first!! */
void FASTCALL DestroyProcessClasses(PPROCESSINFO Process )
{
    PCLS Class;
    PPROCESSINFO pi = (PPROCESSINFO)Process;
     
    if (pi != NULL)
    {
        /* Free all local classes */
        Class = pi->pclsPrivateList;
        while (Class != NULL)
        {
            pi->pclsPrivateList = Class->pclsNext;

            ASSERT(Class->pclsBase == Class);
            IntDestroyClass(Class);

            Class = pi->pclsPrivateList;
        }

        /* Free all global classes */
        Class = pi->pclsPublicList;
        while (Class != NULL)
        {
            pi->pclsPublicList = Class->pclsNext;

            ASSERT(Class->pclsBase == Class);
            IntDestroyClass(Class);

            Class = pi->pclsPublicList;
        }
    }
}

static BOOL
IntRegisterClassAtom(IN PUNICODE_STRING ClassName,
                     OUT RTL_ATOM *pAtom)
{
    WCHAR szBuf[65];
    PWSTR AtomName;
    NTSTATUS Status;

    if (ClassName->Length != 0)
    {
        /* FIXME: Don't limit to 64 characters! Use SEH when allocating memory! */
        if (ClassName->Length / sizeof(WCHAR) >= sizeof(szBuf) / sizeof(szBuf[0]))
        {
            EngSetLastError(ERROR_INVALID_PARAMETER);
            return (RTL_ATOM)0;
        }

        RtlCopyMemory(szBuf,
                      ClassName->Buffer,
                      ClassName->Length);
        szBuf[ClassName->Length / sizeof(WCHAR)] = UNICODE_NULL;
        AtomName = szBuf;
    }
    else
        AtomName = ClassName->Buffer;

    Status = RtlAddAtomToAtomTable(gAtomTable,
                                   AtomName,
                                   pAtom);

    if (!NT_SUCCESS(Status))
    {
        SetLastNtError(Status);
        return FALSE;
    }

    return TRUE;
}

static NTSTATUS
IntDeregisterClassAtom(IN RTL_ATOM Atom)
{
    return RtlDeleteAtomFromAtomTable(gAtomTable,
                                      Atom);
}

VOID
UserAddCallProcToClass(IN OUT PCLS Class,
                       IN PCALLPROCDATA CallProc)
{
    PCLS BaseClass;

    ASSERT(CallProc->spcpdNext == NULL);

    BaseClass = Class->pclsBase;
    ASSERT(CallProc->spcpdNext == NULL);
    CallProc->spcpdNext = BaseClass->spcpdFirst;
    BaseClass->spcpdFirst = CallProc;

    /* Update all clones */
    Class = Class->pclsClone;
    while (Class != NULL)
    {
        Class->spcpdFirst = BaseClass->spcpdFirst;
        Class = Class->pclsNext;
    }
}

static BOOL
IntSetClassAtom(IN OUT PCLS Class,
                IN PUNICODE_STRING ClassName)
{
    RTL_ATOM Atom = (RTL_ATOM)0;

    /* Update the base class first */
    Class = Class->pclsBase;

    if (!IntRegisterClassAtom(ClassName,
                              &Atom))
    {
        return FALSE;
    }

    IntDeregisterClassAtom(Class->atomClassName);

    Class->atomClassName = Atom;

    /* Update the clones */
    Class = Class->pclsClone;
    while (Class != NULL)
    {
        Class->atomClassName = Atom;

        Class = Class->pclsNext;
    }

    return TRUE;
}

//
// Same as User32:IntGetClsWndProc.
//
WNDPROC FASTCALL
IntGetClassWndProc(PCLS Class, BOOL Ansi)
{
  INT i;
  WNDPROC gcpd = NULL, Ret = NULL;

  if (Class->CSF_flags & CSF_SERVERSIDEPROC)
  {
     for ( i = FNID_FIRST; i <= FNID_SWITCH; i++)
     {
         if (GETPFNSERVER(i) == Class->lpfnWndProc)
         {
            if (Ansi)
               Ret = GETPFNCLIENTA(i);
            else
               Ret = GETPFNCLIENTW(i);
         }
     }
     return Ret;
  }
  Ret = Class->lpfnWndProc;

  if (Class->fnid <= FNID_GHOST && Class->fnid >= FNID_BUTTON)
  {
     if (Ansi)
     {
        if (GETPFNCLIENTW(Class->fnid) == Class->lpfnWndProc)
           Ret = GETPFNCLIENTA(Class->fnid);
     }
     else
     {
        if (GETPFNCLIENTA(Class->fnid) == Class->lpfnWndProc)
           Ret = GETPFNCLIENTW(Class->fnid);
     }
  }

  if ( Ret != Class->lpfnWndProc ||
       Ansi == !!(Class->CSF_flags & CSF_ANSIPROC) )
     return Ret;

  gcpd = (WNDPROC)UserGetCPD( Class,
                       (Ansi ? UserGetCPDA2U : UserGetCPDU2A )|UserGetCPDClass,
                       (ULONG_PTR)Ret);

  return (gcpd ? gcpd : Ret);
}


static
WNDPROC FASTCALL
IntSetClassWndProc(IN OUT PCLS Class,
                   IN WNDPROC WndProc,
                   IN BOOL Ansi)
{
   INT i;
   PCALLPROCDATA pcpd;
   WNDPROC Ret, chWndProc;

   Ret = IntGetClassWndProc(Class, Ansi);

   // If Server Side, downgrade to Client Side.
   if (Class->CSF_flags & CSF_SERVERSIDEPROC)
   {
      if (Ansi) Class->CSF_flags |= CSF_ANSIPROC;
      Class->CSF_flags &= ~CSF_SERVERSIDEPROC;
      Class->Unicode = !Ansi;
   }

   if (!WndProc) WndProc = Class->lpfnWndProc;

   chWndProc = WndProc;

   // Check if CallProc handle and retrieve previous call proc address and set.
   if (IsCallProcHandle(WndProc))
   {
      pcpd = UserGetObject(gHandleTable, WndProc, otCallProc);
      if (pcpd) chWndProc = pcpd->pfnClientPrevious;
   }

   Class->lpfnWndProc = chWndProc;

   // Clear test proc.
   chWndProc = NULL;

   // Switch from Client Side call to Server Side call if match. Ref: "deftest".
   for ( i = FNID_FIRST; i <= FNID_SWITCH; i++)
   {
       if (GETPFNCLIENTW(i) == Class->lpfnWndProc)
       {
          chWndProc = GETPFNSERVER(i);
          break;
       }
       if (GETPFNCLIENTA(i) == Class->lpfnWndProc)
       {
          chWndProc = GETPFNSERVER(i);
          break;
       }
   }
   // If match, set/reset to Server Side and clear ansi.
   if (chWndProc)
   {
      Class->lpfnWndProc = chWndProc;
      Class->Unicode = TRUE;
      Class->CSF_flags &= ~CSF_ANSIPROC;
      Class->CSF_flags |= CSF_SERVERSIDEPROC;      
   }
   else
   {
      Class->Unicode = !Ansi;

      if (Ansi)
         Class->CSF_flags |= CSF_ANSIPROC;
      else
         Class->CSF_flags &= ~CSF_ANSIPROC;
   }

   /* Update the clones */
   chWndProc = Class->lpfnWndProc;

   Class = Class->pclsClone;
   while (Class != NULL)
   {
      Class->Unicode = !Ansi;
      Class->lpfnWndProc = chWndProc;
                                
      Class = Class->pclsNext;
   }

   return Ret;
}

static PCLS
IntGetClassForDesktop(IN OUT PCLS BaseClass,
                      IN OUT PCLS *ClassLink,
                      IN PDESKTOP Desktop)
{
    SIZE_T ClassSize;
    PCLS Class;

    ASSERT(Desktop != NULL);
    ASSERT(BaseClass->pclsBase == BaseClass);

    if (BaseClass->rpdeskParent == Desktop)
    {
        /* It is most likely that a window is created on the same
           desktop as the window class. */

        return BaseClass;
    }

    if (BaseClass->rpdeskParent == NULL)
    {
        ASSERT(BaseClass->cWndReferenceCount == 0);
        ASSERT(BaseClass->pclsClone == NULL);

        /* Classes are also located in the shared heap when the class
           was created before the thread attached to a desktop. As soon
           as a window is created for such a class located on the shared
           heap, the class is cloned into the desktop heap on which the
           window is created. */
        Class = NULL;
    }
    else
    {
        /* The user is asking for a class object on a different desktop,
           try to find one! */
        Class = BaseClass->pclsClone;
        while (Class != NULL)
        {
            if (Class->rpdeskParent == Desktop)
            {
                ASSERT(Class->pclsBase == BaseClass);
                ASSERT(Class->pclsClone == NULL);
                break;
            }

            Class = Class->pclsNext;
        }
    }

    if (Class == NULL)
    {
        /* The window is created on a different desktop, we need to
           clone the class object to the desktop heap of the window! */
        ClassSize = sizeof(*BaseClass) + (SIZE_T)BaseClass->cbclsExtra;

        Class = DesktopHeapAlloc(Desktop,
                                 ClassSize);
        if (Class != NULL)
        {
            /* Simply clone the class */
            RtlCopyMemory( Class, BaseClass, ClassSize);

            TRACE("Clone Class 0x%x hM 0x%x\n %S\n",Class, Class->hModule, Class->lpszClientUnicodeMenuName);

            /* Restore module address if default user class Ref: Bug 4778 */
            if ( Class->hModule != hModClient &&
                 Class->fnid <= FNID_GHOST    &&
                 Class->fnid >= FNID_BUTTON )
            {
               Class->hModule = hModClient;
               TRACE("Clone Class 0x%x Reset hM 0x%x\n",Class, Class->hModule);
            }

            /* Update some pointers and link the class */
            Class->rpdeskParent = Desktop;
            Class->cWndReferenceCount = 0;

            if (BaseClass->rpdeskParent == NULL)
            {
                /* We don't really need the base class on the shared
                   heap anymore, delete it so the only class left is
                   the clone we just created, which now serves as the
                   new base class */
                ASSERT(BaseClass->pclsClone == NULL);
                ASSERT(Class->pclsClone == NULL);
                Class->pclsBase = Class;
                Class->pclsNext = BaseClass->pclsNext;

                /* Replace the base class */
                (void)InterlockedExchangePointer((PVOID*)ClassLink,
                                                 Class);

                /* Destroy the obsolete copy on the shared heap */
                BaseClass->pclsBase = NULL;
                BaseClass->pclsClone = NULL;
                IntDestroyClass(BaseClass);
            }
            else
            {
                /* Link in the clone */
                Class->pclsClone = NULL;
                Class->pclsBase = BaseClass;
                Class->pclsNext = BaseClass->pclsClone;
                (void)InterlockedExchangePointer((PVOID*)&BaseClass->pclsClone,
                                                 Class);
            }
        }
        else
        {
            EngSetLastError(ERROR_NOT_ENOUGH_MEMORY);
        }
    }
    return Class;
}

PCLS
IntReferenceClass(IN OUT PCLS BaseClass,
                  IN OUT PCLS *ClassLink,
                  IN PDESKTOP Desktop)
{
    PCLS Class;
    ASSERT(BaseClass->pclsBase == BaseClass);

    Class = IntGetClassForDesktop(BaseClass,
                                  ClassLink,
                                  Desktop);
    if (Class != NULL)
    {
        Class->cWndReferenceCount++;
    }

    return Class;
}

static
VOID
IntMakeCloneBaseClass(IN OUT PCLS Class,
                      IN OUT PCLS *BaseClassLink,
                      IN OUT PCLS *CloneLink)
{
    PCLS Clone;

    ASSERT(Class->pclsBase != Class);
    ASSERT(Class->pclsBase->pclsClone != NULL);
    ASSERT(Class->rpdeskParent != NULL);
    ASSERT(Class->cWndReferenceCount != 0);
    ASSERT(Class->pclsBase->rpdeskParent != NULL);
    ASSERT(Class->pclsBase->cWndReferenceCount == 0);

    /* Unlink the clone */
    *CloneLink = Class->pclsNext;
    Class->pclsClone = Class->pclsBase->pclsClone;

    /* Update the class information to make it a base class */
    Class->pclsBase = Class;
    Class->pclsNext = (*BaseClassLink)->pclsNext;

    /* Update all clones */
    Clone = Class->pclsClone;
    while (Clone != NULL)
    {
        ASSERT(Clone->pclsClone == NULL);
        Clone->pclsBase = Class;

        Clone = Clone->pclsNext;
    }

    /* Link in the new base class */
    (void)InterlockedExchangePointer((PVOID*)BaseClassLink,
                                     Class);
}


VOID
IntDereferenceClass(IN OUT PCLS Class,
                    IN PDESKTOPINFO Desktop,
                    IN PPROCESSINFO pi)
{
    PCLS *PrevLink, BaseClass, CurrentClass;

    BaseClass = Class->pclsBase;

    if (--Class->cWndReferenceCount <= 0)
    {
        if (BaseClass == Class)
        {
            ASSERT(Class->pclsBase == Class);

            TRACE("IntDereferenceClass 0x%x\n", Class);
            /* Check if there are clones of the class on other desktops,
               link the first clone in if possible. If there are no clones
               then leave the class on the desktop heap. It will get moved
               to the shared heap when the thread detaches. */
            if (BaseClass->pclsClone != NULL)
            {
                if (BaseClass->Global)
                    PrevLink = &pi->pclsPublicList;
                else
                    PrevLink = &pi->pclsPrivateList;

                CurrentClass = *PrevLink;
                while (CurrentClass != BaseClass)
                {
                    ASSERT(CurrentClass != NULL);

                    PrevLink = &CurrentClass->pclsNext;
                    CurrentClass = CurrentClass->pclsNext;
                }

                ASSERT(*PrevLink == BaseClass);

                /* Make the first clone become the new base class */
                IntMakeCloneBaseClass(BaseClass->pclsClone,
                                      PrevLink,
                                      &BaseClass->pclsClone);

                /* Destroy the class, there's still another clone of the class
                   that now serves as a base class. Make sure we don't destruct
                   resources shared by all classes (Base = NULL)! */
                BaseClass->pclsBase = NULL;
                BaseClass->pclsClone = NULL;
                IntDestroyClass(BaseClass);
            }
        }
        else
        {
            TRACE("IntDereferenceClass1 0x%x\n", Class);

            /* Locate the cloned class and unlink it */
            PrevLink = &BaseClass->pclsClone;
            CurrentClass = BaseClass->pclsClone;
            while (CurrentClass != Class)
            {
                ASSERT(CurrentClass != NULL);

                PrevLink = &CurrentClass->pclsNext;
                CurrentClass = CurrentClass->pclsNext;
            }

            ASSERT(CurrentClass == Class);

            (void)InterlockedExchangePointer((PVOID*)PrevLink,
                                             Class->pclsNext);

            ASSERT(Class->pclsBase == BaseClass);
            ASSERT(Class->pclsClone == NULL);

            /* The class was just a clone, we don't need it anymore */
            IntDestroyClass(Class);
        }
    }
}

static BOOL
IntMoveClassToSharedHeap(IN OUT PCLS Class,
                         IN OUT PCLS **ClassLinkPtr)
{
    PCLS NewClass;
    SIZE_T ClassSize;

    ASSERT(Class->pclsBase == Class);
    ASSERT(Class->rpdeskParent != NULL);
    ASSERT(Class->cWndReferenceCount == 0);
    ASSERT(Class->pclsClone == NULL);

    ClassSize = sizeof(*Class) + (SIZE_T)Class->cbclsExtra;

    /* Allocate the new base class on the shared heap */
    NewClass = UserHeapAlloc(ClassSize);
    if (NewClass != NULL)
    {
        RtlCopyMemory(NewClass,
                      Class,
                      ClassSize);

        NewClass->rpdeskParent = NULL;
        NewClass->pclsBase = NewClass;

        /* Replace the class in the list */
        (void)InterlockedExchangePointer((PVOID*)*ClassLinkPtr,
                                         NewClass);
        *ClassLinkPtr = &NewClass->pclsNext;

        /* Free the obsolete class on the desktop heap */
        Class->pclsBase = NULL;
        IntDestroyClass(Class);
        return TRUE;
    }

    return FALSE;
}

static VOID
IntCheckDesktopClasses(IN PDESKTOP Desktop,
                       IN OUT PCLS *ClassList,
                       IN BOOL FreeOnFailure,
                       OUT BOOL *Ret)
{
    PCLS Class, NextClass, *Link;

    /* NOTE: We only need to check base classes! When classes are no longer needed
             on a desktop, the clones will be freed automatically as soon as possible.
             However, we need to move base classes to the shared heap, as soon as
             the last desktop heap where a class is allocated on is about to be destroyed.
             If we didn't move the class to the shared heap, the class would become
             inaccessible! */

    ASSERT(Desktop != NULL);

    Link = ClassList;
    Class = *Link;
    while (Class != NULL)
    {
        NextClass = Class->pclsNext;

        ASSERT(Class->pclsBase == Class);

        if (Class->rpdeskParent == Desktop &&
            Class->cWndReferenceCount == 0)
        {
            /* There shouldn't be any clones around anymore! */
            ASSERT(Class->pclsClone == NULL);

            /* FIXME: If process is terminating, don't move the class but rather destroy it! */
            /* FIXME: We could move the class to another desktop heap if there's still desktops
                       mapped into the process... */

            /* Move the class to the shared heap */
            if (IntMoveClassToSharedHeap(Class,
                                         &Link))
            {
                ASSERT(*Link == NextClass);
            }
            else
            {
                ASSERT(NextClass == Class->pclsNext);

                if (FreeOnFailure)
                {
                    /* Unlink the base class */
                    (void)InterlockedExchangePointer((PVOID*)Link,
                                                     Class->pclsNext);

                    /* We can free the old base class now */
                    Class->pclsBase = NULL;
                    IntDestroyClass(Class);
                }
                else
                {
                    Link = &Class->pclsNext;
                    *Ret = FALSE;
                }
            }
        }
        else
            Link = &Class->pclsNext;

        Class = NextClass;
    }
}

BOOL
IntCheckProcessDesktopClasses(IN PDESKTOP Desktop,
                              IN BOOL FreeOnFailure)
{
    PPROCESSINFO pi;
    BOOL Ret = TRUE;

    pi = GetW32ProcessInfo();

    /* Check all local classes */
    IntCheckDesktopClasses(Desktop,
                           &pi->pclsPrivateList,
                           FreeOnFailure,
                           &Ret);

    /* Check all global classes */
    IntCheckDesktopClasses(Desktop,
                           &pi->pclsPublicList,
                           FreeOnFailure,
                           &Ret);
    if (!Ret)
    {
        ERR("Failed to move process classes from desktop 0x%p to the shared heap!\n", Desktop);
        EngSetLastError(ERROR_NOT_ENOUGH_MEMORY);
    }

    return Ret;
}

PCLS
FASTCALL
IntCreateClass(IN CONST WNDCLASSEXW* lpwcx,
               IN PUNICODE_STRING ClassName,
               IN PUNICODE_STRING MenuName,
               IN DWORD fnID,
               IN DWORD dwFlags,
               IN PDESKTOP Desktop,
               IN PPROCESSINFO pi)
{
    SIZE_T ClassSize;
    PCLS Class = NULL;
    RTL_ATOM Atom;
    WNDPROC WndProc;
    PWSTR pszMenuName = NULL;
    NTSTATUS Status = STATUS_SUCCESS;

    TRACE("lpwcx=%p ClassName=%wZ MenuName=%wZ dwFlags=%08x Desktop=%p pi=%p\n",
        lpwcx, ClassName, MenuName, dwFlags, Desktop, pi);

    if (!IntRegisterClassAtom(ClassName,
                              &Atom))
    {
        ERR("Failed to register class atom!\n");
        return NULL;
    }

    ClassSize = sizeof(*Class) + lpwcx->cbClsExtra;
    if (MenuName->Length != 0)
    {
        pszMenuName = UserHeapAlloc(MenuName->Length + sizeof(UNICODE_NULL) +
                                    RtlUnicodeStringToAnsiSize(MenuName));
        if (pszMenuName == NULL)
            goto NoMem;
    }

    if (Desktop != NULL)
    {
        Class = DesktopHeapAlloc(Desktop,
                                 ClassSize);
    }
    else
    {
        /* FIXME: The class was created before being connected
                  to a desktop. It is possible for the desktop window,
                  but should it be allowed for any other case? */
        Class = UserHeapAlloc(ClassSize);
    }

    if (Class != NULL)
    {
        int iCls = 0;

        RtlZeroMemory(Class, ClassSize);

        Class->rpdeskParent = Desktop;
        Class->pclsBase = Class;
        Class->atomClassName = Atom;
        Class->fnid = fnID;
        Class->CSF_flags = dwFlags;

        if (LookupFnIdToiCls(Class->fnid, &iCls))
        {
            gpsi->atomSysClass[iCls] = Class->atomClassName;
        }

        _SEH2_TRY
        {
            PWSTR pszMenuNameBuffer = pszMenuName;

            /* Need to protect with SEH since accessing the WNDCLASSEX structure
               and string buffers might raise an exception! We don't want to
               leak memory... */
            // What?! If the user interface was written correctly this would not be an issue!
            Class->lpfnWndProc = lpwcx->lpfnWndProc;
            Class->style = lpwcx->style;
            Class->cbclsExtra = lpwcx->cbClsExtra;
            Class->cbwndExtra = lpwcx->cbWndExtra;
            Class->hModule = lpwcx->hInstance;
            Class->hIcon = lpwcx->hIcon; /* FIXME */
            Class->hIconSm = lpwcx->hIconSm; /* FIXME */
            Class->hCursor = lpwcx->hCursor; /* FIXME */
            Class->hbrBackground = lpwcx->hbrBackground;

            /* Make a copy of the string */
            if (pszMenuNameBuffer != NULL)
            {
                Class->MenuNameIsString = TRUE;

                Class->lpszClientUnicodeMenuName = pszMenuNameBuffer;
                RtlCopyMemory(Class->lpszClientUnicodeMenuName,
                              MenuName->Buffer,
                              MenuName->Length);
                Class->lpszClientUnicodeMenuName[MenuName->Length / sizeof(WCHAR)] = UNICODE_NULL;

                pszMenuNameBuffer += (MenuName->Length / sizeof(WCHAR)) + 1;
            }
            else
                Class->lpszClientUnicodeMenuName = MenuName->Buffer;

            /* Save an ANSI copy of the string */
            if (pszMenuNameBuffer != NULL)
            {
                ANSI_STRING AnsiString;

                Class->lpszClientAnsiMenuName = (PSTR)pszMenuNameBuffer;
                AnsiString.MaximumLength = RtlUnicodeStringToAnsiSize(MenuName);
                AnsiString.Buffer = Class->lpszClientAnsiMenuName;
                Status = RtlUnicodeStringToAnsiString(&AnsiString,
                                                      MenuName,
                                                      FALSE);
                if (!NT_SUCCESS(Status))
                {
                    ERR("Failed to convert unicode menu name to ansi!\n");

                    /* Life would've been much prettier if ntoskrnl exported RtlRaiseStatus()... */
                    _SEH2_LEAVE;
                }
            }
            else
                Class->lpszClientAnsiMenuName = (PSTR)MenuName->Buffer;

            /* Save kernel use menu name and ansi class name */
            Class->lpszMenuName = Class->lpszClientUnicodeMenuName; // FIXME!
            //Class->lpszAnsiClassName = FIXME

            /* Server Side overrides class calling type (A/W)!
               User32 whine test_builtinproc: "deftest"
                  built-in winproc - window A/W type automatically detected */
            if (!(Class->CSF_flags & CSF_SERVERSIDEPROC))
            {
               int i;
               WndProc = NULL;
          /* Due to the wine class "deftest" and most likely no FNID to reference
             from, sort through the Server Side list and compare proc addresses
             for match. This method will be used in related code.
           */
               for ( i = FNID_FIRST; i <= FNID_SWITCH; i++)
               { // Open ANSI or Unicode, just match, set and break.
                   if (GETPFNCLIENTW(i) == Class->lpfnWndProc)
                   {
                      WndProc = GETPFNSERVER(i);
                      break;
                   }
                   if (GETPFNCLIENTA(i) == Class->lpfnWndProc)
                   {
                      WndProc = GETPFNSERVER(i);
                      break;
                   }
               }
               if (WndProc)
               {  // If a hit, we are Server Side so set the right flags and proc.
                  Class->CSF_flags |= CSF_SERVERSIDEPROC;
                  Class->CSF_flags &= ~CSF_ANSIPROC;
                  Class->lpfnWndProc = WndProc;
               }
            }

            if (!(Class->CSF_flags & CSF_ANSIPROC))
                Class->Unicode = TRUE;

            if (Class->style & CS_GLOBALCLASS)
                Class->Global = TRUE;
        }
        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
        {
            Status = _SEH2_GetExceptionCode();
        }
        _SEH2_END;

        if (!NT_SUCCESS(Status))
        {
            ERR("Failed creating the class: 0x%x\n", Status);

            SetLastNtError(Status);

            if (pszMenuName != NULL)
                UserHeapFree(pszMenuName);

            DesktopHeapFree(Desktop,
                            Class);
            Class = NULL;

            IntDeregisterClassAtom(Atom);
        }
    }
    else
    {
NoMem:
        ERR("Failed to allocate class on Desktop 0x%p\n", Desktop);

        if (pszMenuName != NULL)
            UserHeapFree(pszMenuName);

        IntDeregisterClassAtom(Atom);

        EngSetLastError(ERROR_NOT_ENOUGH_MEMORY);
    }

    TRACE("Created class 0x%x with name %wZ and proc 0x%x for atom 0x%x and hInstance 0x%x, global %d\n",
            Class, ClassName, Class->lpfnWndProc, Atom, Class->hModule, Class->Global);

    return Class;
}

static PCLS
IntFindClass(IN RTL_ATOM Atom,
             IN HINSTANCE hInstance,
             IN PCLS *ClassList,
             OUT PCLS **Link  OPTIONAL)
{
    PCLS Class, *PrevLink = ClassList;

    Class = *PrevLink;
    while (Class != NULL)
    {
        if (Class->atomClassName == Atom &&
            (hInstance == NULL || Class->hModule == hInstance) &&
            !(Class->CSF_flags & CSF_WOWDEFERDESTROY))
        {
            ASSERT(Class->pclsBase == Class);

            if (Link != NULL)
                *Link = PrevLink;
            break;
        }

        PrevLink = &Class->pclsNext;
        Class = Class->pclsNext;
    }

    return Class;
}

BOOL
IntGetAtomFromStringOrAtom(IN PUNICODE_STRING ClassName,
                           OUT RTL_ATOM *Atom)
{
    BOOL Ret = FALSE;

    if (ClassName->Length != 0)
    {
        WCHAR szBuf[65];
        PWSTR AtomName;
        NTSTATUS Status;

        /* NOTE: Caller has to protect the call with SEH! */

        if (ClassName->Length != 0)
        {
            /* FIXME: Don't limit to 64 characters! use SEH when allocating memory! */
            if (ClassName->Length / sizeof(WCHAR) >= sizeof(szBuf) / sizeof(szBuf[0]))
            {
                EngSetLastError(ERROR_INVALID_PARAMETER);
                return (RTL_ATOM)0;
            }

            /* We need to make a local copy of the class name! The caller could
               modify the buffer and we could overflow in RtlLookupAtomInAtomTable.
               We're protected by SEH, but the ranges that might be accessed were
               not probed... */
            RtlCopyMemory(szBuf,
                          ClassName->Buffer,
                          ClassName->Length);
            szBuf[ClassName->Length / sizeof(WCHAR)] = UNICODE_NULL;
            AtomName = szBuf;
        }
        else
            AtomName = ClassName->Buffer;

        /* Lookup the atom */
        Status = RtlLookupAtomInAtomTable(gAtomTable,
                                          AtomName,
                                          Atom);
        if (NT_SUCCESS(Status))
        {
            Ret = TRUE;
        }
        else
        {
            if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
            {
                EngSetLastError(ERROR_CLASS_DOES_NOT_EXIST);
            }
            else
            {
                SetLastNtError(Status);
            }
        }
    }
    else
    {
        ASSERT(IS_ATOM(ClassName->Buffer));
        *Atom = (RTL_ATOM)((ULONG_PTR)ClassName->Buffer);
        Ret = TRUE;
    }

    return Ret;
}

RTL_ATOM
IntGetClassAtom(IN PUNICODE_STRING ClassName,
                IN HINSTANCE hInstance  OPTIONAL,
                IN PPROCESSINFO pi  OPTIONAL,
                OUT PCLS *BaseClass  OPTIONAL,
                OUT PCLS **Link  OPTIONAL)
{
    RTL_ATOM Atom = (RTL_ATOM)0;

    ASSERT(BaseClass != NULL);

    if (IntGetAtomFromStringOrAtom(ClassName,
                                   &Atom) &&
        Atom != (RTL_ATOM)0)
    {
        PCLS Class;

        /* Attempt to locate the class object */

        ASSERT(pi != NULL);

        /* Step 1: Try to find an exact match of locally registered classes */
        Class = IntFindClass(Atom,
                             hInstance,
                             &pi->pclsPrivateList,
                             Link);
        if (Class != NULL)
        {  TRACE("Step 1: 0x%x\n",Class );
            goto FoundClass;
        }

        /* Step 2: Try to find any globally registered class. The hInstance
                   is not relevant for global classes */
        Class = IntFindClass(Atom,
                             NULL,
                             &pi->pclsPublicList,
                             Link);
        if (Class != NULL)
        { TRACE("Step 2: 0x%x 0x%x\n",Class, Class->hModule);
            goto FoundClass;
        }

        /* Step 3: Try to find any local class registered by user32 */
        Class = IntFindClass(Atom,
                             hModClient,
                             &pi->pclsPrivateList,
                             Link);
        if (Class != NULL)
        { TRACE("Step 3: 0x%x\n",Class );
            goto FoundClass;
        }

        /* Step 4: Try to find any global class registered by user32 */
        Class = IntFindClass(Atom,
                             hModClient,
                             &pi->pclsPublicList,
                             Link);
        if (Class == NULL)
        {
            EngSetLastError(ERROR_CLASS_DOES_NOT_EXIST);
            return (RTL_ATOM)0;
        }else{TRACE("Step 4: 0x%x\n",Class );}

FoundClass:
        *BaseClass = Class;
    }

    return Atom;
}

PCLS
IntGetAndReferenceClass(PUNICODE_STRING ClassName, HINSTANCE hInstance)
{
   PCLS *ClassLink, Class = NULL;
   RTL_ATOM ClassAtom;
   PTHREADINFO pti;

   pti = PsGetCurrentThreadWin32Thread();

   if ( !(pti->ppi->W32PF_flags & W32PF_CLASSESREGISTERED ))
   {
      UserRegisterSystemClasses();
   }

   /* Check the class. */

   TRACE("Finding Class %wZ for hInstance 0x%x\n", ClassName, hInstance);

   ClassAtom = IntGetClassAtom(ClassName,
                               hInstance,
                               pti->ppi,
                               &Class,
                               &ClassLink);

   if (ClassAtom == (RTL_ATOM)0)
   {
      if (IS_ATOM(ClassName->Buffer))
      {
         ERR("Class 0x%p not found\n", (DWORD_PTR) ClassName->Buffer);
      }
      else
      {
         ERR("Class \"%wZ\" not found\n", ClassName);
      }

      EngSetLastError(ERROR_CANNOT_FIND_WND_CLASS);
      return NULL;
   }

   TRACE("Referencing Class 0x%x with atom 0x%x\n", Class, ClassAtom);
   Class = IntReferenceClass(Class,
                             ClassLink,
                             pti->rpdesk);
   if (Class == NULL)
   {
       ERR("Failed to reference window class!\n");
       return NULL;
   }

   return Class;
}

RTL_ATOM
UserRegisterClass(IN CONST WNDCLASSEXW* lpwcx,
                  IN PUNICODE_STRING ClassName,
                  IN PUNICODE_STRING MenuName,
                  IN DWORD fnID,
                  IN DWORD dwFlags)
{
    PTHREADINFO pti;
    PPROCESSINFO pi;
    PCLS Class;
    RTL_ATOM ClassAtom;
    RTL_ATOM Ret = (RTL_ATOM)0;

    /* NOTE: Accessing the buffers in ClassName and MenuName may raise exceptions! */

    pti = GetW32ThreadInfo();

    pi = pti->ppi;

    // Need only to test for two conditions not four....... Fix more whine tests....
    if ( IntGetAtomFromStringOrAtom( ClassName, &ClassAtom) &&
                                     ClassAtom != (RTL_ATOM)0 &&
                                    !(dwFlags & CSF_SERVERSIDEPROC) ) // Bypass Server Sides
    {
       Class = IntFindClass( ClassAtom,
                             lpwcx->hInstance,
                            &pi->pclsPrivateList,
                             NULL);

       if (Class != NULL && !Class->Global)
       {
          // Local class already exists
          TRACE("Local Class 0x%p does already exist!\n", ClassAtom);
          EngSetLastError(ERROR_CLASS_ALREADY_EXISTS);
          return (RTL_ATOM)0;
       }

       if (lpwcx->style & CS_GLOBALCLASS)
       {
          Class = IntFindClass( ClassAtom,
                                NULL,
                               &pi->pclsPublicList,
                                NULL);

          if (Class != NULL && Class->Global)
          {
             TRACE("Global Class 0x%p does already exist!\n", ClassAtom);
             EngSetLastError(ERROR_CLASS_ALREADY_EXISTS);
             return (RTL_ATOM)0;
          }
       }
    }

    Class = IntCreateClass(lpwcx,
                           ClassName,
                           MenuName,
                           fnID,
                           dwFlags,
                           pti->rpdesk,
                           pi);

    if (Class != NULL)
    {
        PCLS *List;

        /* Register the class */
        if (Class->Global)
            List = &pi->pclsPublicList;
        else
            List = &pi->pclsPrivateList;

        Class->pclsNext = *List;
        (void)InterlockedExchangePointer((PVOID*)List,
                                         Class);

        Ret = Class->atomClassName;
    }
    else
    {
       ERR("UserRegisterClass: Yes, that is right, you have no Class!\n");
    }

    return Ret;
}

BOOL
UserUnregisterClass(IN PUNICODE_STRING ClassName,
                    IN HINSTANCE hInstance,
                    OUT PCLSMENUNAME pClassMenuName)
{
    PCLS *Link;
    PPROCESSINFO pi;
    RTL_ATOM ClassAtom;
    PCLS Class;

    pi = GetW32ProcessInfo();

    TRACE("UserUnregisterClass(%wZ, 0x%x)\n", ClassName, hInstance);

    /* NOTE: Accessing the buffer in ClassName may raise an exception! */
    ClassAtom = IntGetClassAtom(ClassName,
                                hInstance,
                                pi,
                                &Class,
                                &Link);
    if (ClassAtom == (RTL_ATOM)0)
    {
        TRACE("UserUnregisterClass: No Class found.\n");
        return FALSE;
    }

    ASSERT(Class != NULL);

    if (Class->cWndReferenceCount != 0 ||
        Class->pclsClone != NULL)
    {
        TRACE("UserUnregisterClass: Class has a Window. Ct %d : Clone 0x%x\n", Class->cWndReferenceCount, Class->pclsClone);
        EngSetLastError(ERROR_CLASS_HAS_WINDOWS);
        return FALSE;
    }

    /* Must be a base class! */
    ASSERT(Class->pclsBase == Class);

    /* Unlink the class */
    *Link = Class->pclsNext;

    if (NT_SUCCESS(IntDeregisterClassAtom(Class->atomClassName)))
    {
        TRACE("Class 0x%x\n", Class);
        TRACE("UserUnregisterClass: Good Exit!\n");
        /* Finally free the resources */
        IntDestroyClass(Class);
        return TRUE;
    }
    ERR("UserUnregisterClass: Can not deregister Class Atom.\n");
    return FALSE;
}

INT
UserGetClassName(IN PCLS Class,
                 IN OUT PUNICODE_STRING ClassName,
                 IN RTL_ATOM Atom,
                 IN BOOL Ansi)
{
    NTSTATUS Status = STATUS_SUCCESS;
    WCHAR szStaticTemp[32];
    PWSTR szTemp = NULL;
    ULONG BufLen = sizeof(szStaticTemp);
    INT Ret = 0;

    /* Note: Accessing the buffer in ClassName may raise an exception! */

    _SEH2_TRY
    {
        if (Ansi)
        {
            PANSI_STRING AnsiClassName = (PANSI_STRING)ClassName;
            UNICODE_STRING UnicodeClassName;

            /* Limit the size of the static buffer on the stack to the
               size of the buffer provided by the caller */
            if (BufLen / sizeof(WCHAR) > AnsiClassName->MaximumLength)
            {
                BufLen = AnsiClassName->MaximumLength * sizeof(WCHAR);
            }

            /* Find out how big the buffer needs to be */
            Status = RtlQueryAtomInAtomTable(gAtomTable,
                                             Class->atomClassName,
                                             NULL,
                                             NULL,
                                             szStaticTemp,
                                             &BufLen);
            if (Status == STATUS_BUFFER_TOO_SMALL)
            {
                if (BufLen / sizeof(WCHAR) > AnsiClassName->MaximumLength)
                {
                    /* The buffer required exceeds the ansi buffer provided,
                       pretend like we're using the ansi buffer and limit the
                       size to the buffer size provided */
                    BufLen = AnsiClassName->MaximumLength * sizeof(WCHAR);
                }

                /* Allocate a temporary buffer that can hold the unicode class name */
                szTemp = ExAllocatePoolWithTag(PagedPool,
                                               BufLen,
                                               USERTAG_CLASS);
                if (szTemp == NULL)
                {
                    EngSetLastError(ERROR_NOT_ENOUGH_MEMORY);
                    _SEH2_LEAVE;
                }

                /* Query the class name */
                Status = RtlQueryAtomInAtomTable(gAtomTable,
                                                 Atom ? Atom : Class->atomClassName,
                                                 NULL,
                                                 NULL,
                                                 szTemp,
                                                 &BufLen);
            }
            else
                szTemp = szStaticTemp;

            if (NT_SUCCESS(Status))
            {
                /* Convert the atom name to ansi */

                RtlInitUnicodeString(&UnicodeClassName,
                                     szTemp);

                Status = RtlUnicodeStringToAnsiString(AnsiClassName,
                                                      &UnicodeClassName,
                                                      FALSE);
                if (!NT_SUCCESS(Status))
                {
                    SetLastNtError(Status);
                    _SEH2_LEAVE;
                }
            }

            Ret = BufLen / sizeof(WCHAR);
        }
        else /* !ANSI */
        {
            BufLen = ClassName->MaximumLength;

            /* Query the atom name */
            Status = RtlQueryAtomInAtomTable(gAtomTable,
                                             Atom ? Atom : Class->atomClassName,
                                             NULL,
                                             NULL,
                                             ClassName->Buffer,
                                             &BufLen);

            if (!NT_SUCCESS(Status))
            {
                SetLastNtError(Status);
                _SEH2_LEAVE;
            }

            Ret = BufLen / sizeof(WCHAR);
        }
    }
    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
    {
        SetLastNtError(_SEH2_GetExceptionCode());
    }
    _SEH2_END;

    if (Ansi && szTemp != NULL && szTemp != szStaticTemp)
    {
        ExFreePool(szTemp);
    }

    return Ret;
}

static BOOL
IntSetClassMenuName(IN PCLS Class,
                    IN PUNICODE_STRING MenuName)
{
    BOOL Ret = FALSE;

    /* Change the base class first */
    Class = Class->pclsBase;

    if (MenuName->Length != 0)
    {
        ANSI_STRING AnsiString;
        PWSTR strBufW;

        AnsiString.MaximumLength = RtlUnicodeStringToAnsiSize(MenuName);

        strBufW = UserHeapAlloc(MenuName->Length + sizeof(UNICODE_NULL) +
                                AnsiString.MaximumLength);
        if (strBufW != NULL)
        {
            _SEH2_TRY
            {
                NTSTATUS Status;

                /* Copy the unicode string */
                RtlCopyMemory(strBufW,
                              MenuName->Buffer,
                              MenuName->Length);
                strBufW[MenuName->Length / sizeof(WCHAR)] = UNICODE_NULL;

                /* Create an ANSI copy of the string */
                AnsiString.Buffer = (PSTR)(strBufW + (MenuName->Length / sizeof(WCHAR)) + 1);
                Status = RtlUnicodeStringToAnsiString(&AnsiString,
                                                      MenuName,
                                                      FALSE);
                if (!NT_SUCCESS(Status))
                {
                    SetLastNtError(Status);
                    _SEH2_LEAVE;
                }

                Ret = TRUE;
            }
            _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
            {
                SetLastNtError(_SEH2_GetExceptionCode());
            }
            _SEH2_END;

            if (Ret)
            {
                /* Update the base class */
                IntFreeClassMenuName(Class);
                Class->lpszClientUnicodeMenuName = strBufW;
                Class->lpszClientAnsiMenuName = AnsiString.Buffer;
                Class->MenuNameIsString = TRUE;

                /* Update the clones */
                Class = Class->pclsClone;
                while (Class != NULL)
                {
                    Class->lpszClientUnicodeMenuName = strBufW;
                    Class->lpszClientAnsiMenuName = AnsiString.Buffer;
                    Class->MenuNameIsString = TRUE;

                    Class = Class->pclsNext;
                }
            }
            else
            {
                ERR("Failed to copy class menu name!\n");
                UserHeapFree(strBufW);
            }
        }
        else
            EngSetLastError(ERROR_NOT_ENOUGH_MEMORY);
    }
    else
    {
        ASSERT(IS_INTRESOURCE(MenuName->Buffer));

        /* Update the base class */
        IntFreeClassMenuName(Class);
        Class->lpszClientUnicodeMenuName = MenuName->Buffer;
        Class->lpszClientAnsiMenuName = (PSTR)MenuName->Buffer;
        Class->MenuNameIsString = FALSE;

        /* Update the clones */
        Class = Class->pclsClone;
        while (Class != NULL)
        {
            Class->lpszClientUnicodeMenuName = MenuName->Buffer;
            Class->lpszClientAnsiMenuName = (PSTR)MenuName->Buffer;
            Class->MenuNameIsString = FALSE;

            Class = Class->pclsNext;
        }

        Ret = TRUE;
    }

    return Ret;
}

ULONG_PTR
UserSetClassLongPtr(IN PCLS Class,
                    IN INT Index,
                    IN ULONG_PTR NewLong,
                    IN BOOL Ansi)
{
    ULONG_PTR Ret = 0;

    /* NOTE: For GCLP_MENUNAME and GCW_ATOM this function may raise an exception! */

    /* Change the information in the base class first, then update the clones */
    Class = Class->pclsBase;

    if (Index >= 0)
    {
        PULONG_PTR Data;

        TRACE("SetClassLong(%d, %x)\n", Index, NewLong);

        if (Index + sizeof(ULONG_PTR) < Index ||
            Index + sizeof(ULONG_PTR) > Class->cbclsExtra)
        {
            EngSetLastError(ERROR_INVALID_PARAMETER);
            return 0;
        }

        Data = (PULONG_PTR)((ULONG_PTR)(Class + 1) + Index);

        /* FIXME: Data might be a unaligned pointer! Might be a problem on
                  certain architectures, maybe using RtlCopyMemory is a
                  better choice for those architectures! */
        Ret = *Data;
        *Data = NewLong;

        /* Update the clones */
        Class = Class->pclsClone;
        while (Class != NULL)
        {
            *(PULONG_PTR)((ULONG_PTR)(Class + 1) + Index) = NewLong;
            Class = Class->pclsNext;
        }

        return Ret;
    }

    switch (Index)
    {
        case GCL_CBWNDEXTRA:
            Ret = (ULONG_PTR)Class->cbwndExtra;
            Class->cbwndExtra = (INT)NewLong;

            /* Update the clones */
            Class = Class->pclsClone;
            while (Class != NULL)
            {
                Class->cbwndExtra = (INT)NewLong;
                Class = Class->pclsNext;
            }

            break;

        case GCL_CBCLSEXTRA:
            EngSetLastError(ERROR_INVALID_PARAMETER);
            break;

        case GCLP_HBRBACKGROUND:
            Ret = (ULONG_PTR)Class->hbrBackground;
            Class->hbrBackground = (HBRUSH)NewLong;

            /* Update the clones */
            Class = Class->pclsClone;
            while (Class != NULL)
            {
                Class->hbrBackground = (HBRUSH)NewLong;
                Class = Class->pclsNext;
            }
            break;

        case GCLP_HCURSOR:
            /* FIXME: Get handle from pointer to CURSOR object */
            Ret = (ULONG_PTR)Class->hCursor;
            Class->hCursor = (HANDLE)NewLong;

            /* Update the clones */
            Class = Class->pclsClone;
            while (Class != NULL)
            {
                Class->hCursor = (HANDLE)NewLong;
                Class = Class->pclsNext;
            }
            break;

        case GCLP_HICON:
            /* FIXME: Get handle from pointer to ICON object */
            Ret = (ULONG_PTR)Class->hIcon;
            Class->hIcon = (HANDLE)NewLong;

            /* Update the clones */
            Class = Class->pclsClone;
            while (Class != NULL)
            {
                Class->hIcon = (HANDLE)NewLong;
                Class = Class->pclsNext;
            }
            break;

        case GCLP_HICONSM:
            /* FIXME: Get handle from pointer to ICON object */
            Ret = (ULONG_PTR)Class->hIconSm;
            Class->hIconSm = (HANDLE)NewLong;

            /* Update the clones */
            Class = Class->pclsClone;
            while (Class != NULL)
            {
                Class->hIconSm = (HANDLE)NewLong;
                Class = Class->pclsNext;
            }
            break;

        case GCLP_HMODULE:
            Ret = (ULONG_PTR)Class->hModule;
            Class->hModule = (HINSTANCE)NewLong;

           /* Update the clones */
            Class = Class->pclsClone;
            while (Class != NULL)
            {
                Class->hModule = (HINSTANCE)NewLong;
                Class = Class->pclsNext;
            }
            break;

        case GCLP_MENUNAME:
        {
            PUNICODE_STRING Value = (PUNICODE_STRING)NewLong;

            if (!IntSetClassMenuName(Class,
                                     Value))
            {
                ERR("Setting the class menu name failed!\n");
            }

            /* FIXME: Really return NULL? Wine does so... */
            break;
        }

        case GCL_STYLE:
            Ret = (ULONG_PTR)Class->style;
            Class->style = (UINT)NewLong;

            /* FIXME: What if the CS_GLOBALCLASS style is changed? should we
                      move the class to the appropriate list? For now, we save
                      the original value in Class->Global, so we can always
                      locate the appropriate list */

           /* Update the clones */
            Class = Class->pclsClone;
            while (Class != NULL)
            {
                Class->style = (UINT)NewLong;
                Class = Class->pclsNext;
            }
            break;

        case GCLP_WNDPROC:
            Ret = (ULONG_PTR)IntSetClassWndProc(Class,
                                                (WNDPROC)NewLong,
                                                Ansi);
            break;

        case GCW_ATOM:
        {
            PUNICODE_STRING Value = (PUNICODE_STRING)NewLong;

            Ret = (ULONG_PTR)Class->atomClassName;
            if (!IntSetClassAtom(Class,
                                 Value))
            {
                Ret = 0;
            }
            break;
        }

        default:
            EngSetLastError(ERROR_INVALID_INDEX);
            break;
    }

    return Ret;
}

static BOOL
UserGetClassInfo(IN PCLS Class,
                 OUT PWNDCLASSEXW lpwcx,
                 IN BOOL Ansi,
                 HINSTANCE hInstance)
{
    if (!Class) return FALSE;

    lpwcx->style = Class->style;

    // If fnId is set, clear the global bit. See wine class test check_style.
    if (Class->fnid)
       lpwcx->style &= ~CS_GLOBALCLASS;

    lpwcx->lpfnWndProc = IntGetClassWndProc(Class, Ansi);
    
    lpwcx->cbClsExtra = Class->cbclsExtra;
    lpwcx->cbWndExtra = Class->cbwndExtra;
    lpwcx->hIcon = Class->hIcon;        /* FIXME: Get handle from pointer */
    lpwcx->hCursor = Class->hCursor;    /* FIXME: Get handle from pointer */
    lpwcx->hbrBackground = Class->hbrBackground;

    /* Copy non-string to user first. */
    if (Ansi)
       ((PWNDCLASSEXA)lpwcx)->lpszMenuName = Class->lpszClientAnsiMenuName;
    else
       lpwcx->lpszMenuName = Class->lpszClientUnicodeMenuName;
/*
 *  FIXME: CLSMENUNAME has the answers! Copy the already made buffers from there!
 *  Cls: lpszMenuName and lpszAnsiClassName should be used by kernel space.
 *  lpszClientXxxMenuName should already be mapped to user space.
 */
    /* Copy string ptr to user. */
    if ( Class->lpszClientUnicodeMenuName != NULL && 
         Class->MenuNameIsString)
    {
       lpwcx->lpszMenuName = UserHeapAddressToUser(Ansi ?
                                 (PVOID)Class->lpszClientAnsiMenuName :
                                 (PVOID)Class->lpszClientUnicodeMenuName);
    }

    if (hInstance == hModClient)
        lpwcx->hInstance = NULL;
    else
        lpwcx->hInstance = hInstance;

    /* FIXME: Return the string? Okay! This is performed in User32! */
    //lpwcx->lpszClassName = (LPCWSTR)((ULONG_PTR)Class->atomClassName);

    lpwcx->hIconSm = Class->hIconSm; /* FIXME: Get handle from pointer */

    return TRUE;
}

//
// ???
//
BOOL
FASTCALL
UserRegisterSystemClasses(VOID)
{
    UINT i;
    UNICODE_STRING ClassName, MenuName;
    PPROCESSINFO ppi = GetW32ProcessInfo();
    WNDCLASSEXW wc;
    PCLS Class;
    BOOL Ret = TRUE;
    DWORD Flags = 0;

    if (ppi->W32PF_flags & W32PF_CLASSESREGISTERED)
       return TRUE;

    if ( hModClient == NULL)
       return FALSE;

    RtlZeroMemory(&ClassName, sizeof(ClassName));
    RtlZeroMemory(&MenuName, sizeof(MenuName));

    for (i = 0; i != ARRAYSIZE(DefaultServerClasses); i++)
    {
        if (!IS_ATOM(DefaultServerClasses[i].ClassName))
        {
           RtlInitUnicodeString(&ClassName, DefaultServerClasses[i].ClassName);
        }
        else
        {
           ClassName.Buffer = DefaultServerClasses[i].ClassName;
           ClassName.Length = 0;
           ClassName.MaximumLength = 0;
        }
        
        wc.cbSize = sizeof(wc);
        wc.style = DefaultServerClasses[i].Style;

        Flags |= CSF_SERVERSIDEPROC;

        if (DefaultServerClasses[i].ProcW)
        {
           wc.lpfnWndProc = DefaultServerClasses[i].ProcW;
           wc.hInstance = hModuleWin;
        }
        else
        {
           wc.lpfnWndProc = GETPFNSERVER(DefaultServerClasses[i].fiId);
           wc.hInstance = hModClient;
        }

        wc.cbClsExtra = 0;
        wc.cbWndExtra = DefaultServerClasses[i].ExtraBytes;
        wc.hIcon = NULL;
        wc.hCursor = DefaultServerClasses[i].hCursor;
        wc.hbrBackground = DefaultServerClasses[i].hBrush;
        wc.lpszMenuName = NULL;
        wc.lpszClassName = ClassName.Buffer;
        wc.hIconSm = NULL;

        Class = IntCreateClass( &wc,
                                &ClassName,
                                &MenuName,
                                 DefaultServerClasses[i].fiId,
                                 Flags,
                                 NULL,
                                 ppi);
        if (Class != NULL)
        {
            Class->pclsNext = ppi->pclsPublicList;
            (void)InterlockedExchangePointer((PVOID*)&ppi->pclsPublicList,
                                             Class);

            ppi->dwRegisteredClasses |= ICLASS_TO_MASK(DefaultServerClasses[i].iCls);
        }
        else
        {
            ERR("!!! Registering system class failed!\n");
            Ret = FALSE;
        }
    }
    if (Ret) ppi->W32PF_flags |= W32PF_CLASSESREGISTERED;
    return Ret;
}

/* SYSCALLS *****************************************************************/

RTL_ATOM
APIENTRY
NtUserRegisterClassExWOW(
    WNDCLASSEXW* lpwcx,
    PUNICODE_STRING ClassName,
    PUNICODE_STRING ClsNVersion,
    PCLSMENUNAME pClassMenuName,
    DWORD fnID,
    DWORD Flags,
    LPDWORD pWow)
/*
 * FUNCTION:
 *   Registers a new class with the window manager
 * ARGUMENTS:
 *   lpwcx          = Win32 extended window class structure
 *   bUnicodeClass = Whether to send ANSI or unicode strings
 *                   to window procedures
 * RETURNS:
 *   Atom identifying the new class
 */
{
    WNDCLASSEXW CapturedClassInfo = {0};
    UNICODE_STRING CapturedName = {0}, CapturedMenuName = {0};
    RTL_ATOM Ret = (RTL_ATOM)0;
    PPROCESSINFO ppi = GetW32ProcessInfo();

    if (Flags & ~(CSF_ANSIPROC))
    {
        ERR("NtUserRegisterClassExWOW Bad Flags!\n");
        EngSetLastError(ERROR_INVALID_FLAGS);
        return Ret;
    }

    UserEnterExclusive();

    TRACE("NtUserRegisterClassExWOW ClsN %wZ\n",ClassName);

    if ( !(ppi->W32PF_flags & W32PF_CLASSESREGISTERED ))
    {
       UserRegisterSystemClasses();
    }

    _SEH2_TRY
    {
        /* Probe the parameters and basic parameter checks */
        if (ProbeForReadUint(&lpwcx->cbSize) != sizeof(WNDCLASSEXW))
        {
            ERR("NtUserRegisterClassExWOW Wrong cbSize!\n");
            goto InvalidParameter;
        }

        ProbeForRead(lpwcx,
                     sizeof(WNDCLASSEXW),
                     sizeof(ULONG));
        RtlCopyMemory(&CapturedClassInfo,
                      lpwcx,
                      sizeof(WNDCLASSEXW));

        CapturedName = ProbeForReadUnicodeString(ClassName);

        ProbeForRead(pClassMenuName,
                     sizeof(CLSMENUNAME),
                     1);

        CapturedMenuName = ProbeForReadUnicodeString(pClassMenuName->pusMenuName);

        if ( CapturedName.Length & 1 ||
             CapturedMenuName.Length & 1 ||
             CapturedClassInfo.cbClsExtra < 0 ||
             CapturedClassInfo.cbClsExtra +
                CapturedName.Length +
                CapturedMenuName.Length +
                sizeof(CLS) < CapturedClassInfo.cbClsExtra ||
             CapturedClassInfo.cbWndExtra < 0 ||
             CapturedClassInfo.hInstance == NULL)
        {
            ERR("NtUserRegisterClassExWOW Invalid Parameter Error!\n");
            goto InvalidParameter;
        }

        if (CapturedName.Length != 0)
        {
            ProbeForRead(CapturedName.Buffer,
                         CapturedName.Length,
                         sizeof(WCHAR));
        }
        else
        {
            if (!IS_ATOM(CapturedName.Buffer))
            {
                ERR("NtUserRegisterClassExWOW ClassName Error!\n");
                goto InvalidParameter;
            }
        }

        if (CapturedMenuName.Length != 0)
        {
            ProbeForRead(CapturedMenuName.Buffer,
                         CapturedMenuName.Length,
                         sizeof(WCHAR));
        }
        else if (CapturedMenuName.Buffer != NULL &&
                 !IS_INTRESOURCE(CapturedMenuName.Buffer))
        {
            ERR("NtUserRegisterClassExWOW MenuName Error!\n");
InvalidParameter:
            EngSetLastError(ERROR_INVALID_PARAMETER);
            _SEH2_LEAVE;
        }

        if (IsCallProcHandle(lpwcx->lpfnWndProc))
        {  // Never seen this yet, but I'm sure it's a little haxxy trick!
           // If this pops up we know what todo!
           ERR("NtUserRegisterClassExWOW WndProc is CallProc!!\n");
        }

        TRACE("NtUserRegisterClassExWOW MnuN %wZ\n",&CapturedMenuName);

        /* Register the class */
        Ret = UserRegisterClass(&CapturedClassInfo,
                                &CapturedName,
                                &CapturedMenuName,
                                fnID,
                                Flags);
    }
    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
    {
        ERR("NtUserRegisterClassExWOW Exception Error!\n");
        SetLastNtError(_SEH2_GetExceptionCode());
    }
    _SEH2_END;
/*
    if (!Ret)
    {
       ERR("NtUserRegisterClassExWOW Null Return!\n");
    }
 */
    UserLeave();

    return Ret;
}

ULONG_PTR APIENTRY
NtUserSetClassLong(HWND hWnd,
                   INT Offset,
                   ULONG_PTR dwNewLong,
                   BOOL Ansi)
{
    PPROCESSINFO pi;
    PWND Window;
    ULONG_PTR Ret = 0;

    UserEnterExclusive();

    pi = GetW32ProcessInfo();

    Window = UserGetWindowObject(hWnd);
    if (Window != NULL)
    {
        if (Window->head.pti->ppi != pi)
        {
            EngSetLastError(ERROR_ACCESS_DENIED);
            goto Cleanup;
        }

        _SEH2_TRY
        {
            UNICODE_STRING Value;

            /* Probe the parameters */
            if (Offset == GCW_ATOM || Offset == GCLP_MENUNAME)
            {
                Value = ProbeForReadUnicodeString((PUNICODE_STRING)dwNewLong);
                if (Value.Length & 1)
                {
                    goto InvalidParameter;
                }

                if (Value.Length != 0)
                {
                    ProbeForRead(Value.Buffer,
                                 Value.Length,
                                 sizeof(WCHAR));
                }
                else
                {
                    if (Offset == GCW_ATOM && !IS_ATOM(Value.Buffer))
                    {
                        goto InvalidParameter;
                    }
                    else if (Offset == GCLP_MENUNAME && !IS_INTRESOURCE(Value.Buffer))
                    {
InvalidParameter:
                        EngSetLastError(ERROR_INVALID_PARAMETER);
                        _SEH2_LEAVE;
                    }
                }

                dwNewLong = (ULONG_PTR)&Value;
            }

            Ret = UserSetClassLongPtr(Window->pcls,
                                      Offset,
                                      dwNewLong,
                                      Ansi);
        }
        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
        {
            SetLastNtError(_SEH2_GetExceptionCode());
        }
        _SEH2_END;
    }

Cleanup:
    UserLeave();

    return Ret;
}

WORD
APIENTRY
NtUserSetClassWord(
  HWND hWnd,
  INT nIndex,
  WORD wNewWord)
{
/*
 * NOTE: Obsoleted in 32-bit windows
 */
   return(0);
}

BOOL APIENTRY
NtUserUnregisterClass(IN PUNICODE_STRING ClassNameOrAtom,
                      IN HINSTANCE hInstance,
                      OUT PCLSMENUNAME pClassMenuName)
{
    UNICODE_STRING CapturedClassName;
    BOOL Ret = FALSE;

    UserEnterExclusive();

    _SEH2_TRY
    {
        /* Probe the paramters */
        CapturedClassName = ProbeForReadUnicodeString(ClassNameOrAtom);
        if (CapturedClassName.Length & 1)
        {
            goto InvalidParameter;
        }

        if (CapturedClassName.Length != 0)
        {
            ProbeForRead(CapturedClassName.Buffer,
                         CapturedClassName.Length,
                         sizeof(WCHAR));
        }
        else
        {
            if (!IS_ATOM(CapturedClassName.Buffer))
            {
InvalidParameter:
                EngSetLastError(ERROR_INVALID_PARAMETER);
                _SEH2_LEAVE;
            }
        }

        /* Unregister the class */
        Ret = UserUnregisterClass(&CapturedClassName,
                                  hInstance,
                                  NULL); // Null for now~
    }
    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
    {
        SetLastNtError(_SEH2_GetExceptionCode());
    }
    _SEH2_END;

    UserLeave();
    return Ret;
}

/* NOTE: For system classes hInstance is not NULL here, but User32Instance */
BOOL APIENTRY
NtUserGetClassInfo(
   HINSTANCE hInstance,
   PUNICODE_STRING ClassName,
   LPWNDCLASSEXW lpWndClassEx,
   LPWSTR *ppszMenuName,
   BOOL Ansi)
{
   UNICODE_STRING CapturedClassName, SafeClassName;
   WNDCLASSEXW Safewcexw;
   PCLS Class;
   RTL_ATOM ClassAtom = 0;
   PPROCESSINFO ppi;
   BOOL Ret = TRUE;

   /* NOTE: Need exclusive lock because getting the wndproc might require the
            creation of a call procedure handle */
   UserEnterExclusive();

   ppi = GetW32ProcessInfo();

   if ( !(ppi->W32PF_flags & W32PF_CLASSESREGISTERED ))
   {
      UserRegisterSystemClasses();
   }

   _SEH2_TRY
   {
      /* Probe the paramters */
      CapturedClassName = ProbeForReadUnicodeString(ClassName);

      if (CapturedClassName.Length == 0)
      {
         TRACE("hInst %p atom %04X lpWndClassEx %p Ansi %d\n", hInstance, CapturedClassName.Buffer, lpWndClassEx, Ansi);
      }
      else
      {
         TRACE("hInst %p class %wZ lpWndClassEx %p Ansi %d\n", hInstance, &CapturedClassName, lpWndClassEx, Ansi);
      }

      if (CapturedClassName.Length & 1)
      {
         EngSetLastError(ERROR_INVALID_PARAMETER);
         Ret = FALSE;
         _SEH2_LEAVE;
      }

      if (CapturedClassName.Length != 0)
      {
         ProbeForRead( CapturedClassName.Buffer,
                       CapturedClassName.Length,
                       sizeof(WCHAR));

         RtlInitUnicodeString( &SafeClassName, CapturedClassName.Buffer);

         SafeClassName.Buffer = ExAllocatePoolWithTag( PagedPool,
                                                       SafeClassName.MaximumLength,
                                                       TAG_STRING);
         RtlCopyMemory( SafeClassName.Buffer,
                        CapturedClassName.Buffer,
                        SafeClassName.MaximumLength);
      }
      else
      {
         if (!IS_ATOM(CapturedClassName.Buffer))
         {
            ERR("NtUserGetClassInfo() got ClassName instead of Atom!\n");
            EngSetLastError(ERROR_INVALID_PARAMETER);
            Ret = FALSE;
            _SEH2_LEAVE;
         }

         SafeClassName.Buffer = CapturedClassName.Buffer;
         SafeClassName.Length = 0;
         SafeClassName.MaximumLength = 0;
      }

      ProbeForWrite( lpWndClassEx, sizeof(WNDCLASSEXW), sizeof(ULONG));

      RtlCopyMemory( &Safewcexw, lpWndClassEx, sizeof(WNDCLASSEXW));
   }
   _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
   {
      SetLastNtError(_SEH2_GetExceptionCode());
      Ret = FALSE;
   }
   _SEH2_END;

   // If null instance use client.
   if (!hInstance) hInstance = hModClient;

   if (Ret)
   {
      TRACE("GetClassInfo(%wZ, 0x%x)\n", SafeClassName, hInstance);
      ClassAtom = IntGetClassAtom( &SafeClassName,
                                    hInstance,
                                    ppi,
                                   &Class,
                                    NULL);
      if (ClassAtom != (RTL_ATOM)0)
      {
         if (hInstance == NULL) hInstance = hModClient;

         Ret = UserGetClassInfo( Class,
                                &Safewcexw,
                                 Ansi,
                                 hInstance);
      }
      else
      {
         EngSetLastError(ERROR_CLASS_DOES_NOT_EXIST);
         Ret = FALSE;
      }
   }
   _SEH2_TRY
   {
      if (Ret)
      {
         /* Emulate Function. */
         if (ppszMenuName) *ppszMenuName = (LPWSTR)Safewcexw.lpszMenuName;

         RtlCopyMemory(lpWndClassEx, &Safewcexw, sizeof(WNDCLASSEXW));

         // From Wine:
         /* We must return the atom of the class here instead of just TRUE. */
         /* Undocumented behavior! Return the class atom as a BOOL! */
         Ret = (BOOL)ClassAtom;
      }
   }
   _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
   {
      EngSetLastError(ERROR_CLASS_DOES_NOT_EXIST);
      Ret = FALSE;
   }
   _SEH2_END;

   if (SafeClassName.Length) ExFreePoolWithTag(SafeClassName.Buffer, TAG_STRING);
   UserLeave();
   return Ret;
}


INT APIENTRY
NtUserGetClassName (IN HWND hWnd,
                    IN BOOL Real,
                    OUT PUNICODE_STRING ClassName)
{
    PWND Window;
    UNICODE_STRING CapturedClassName;
    INT iCls, Ret = 0;
    RTL_ATOM Atom = 0;

    UserEnterShared();

    Window = UserGetWindowObject(hWnd);
    if (Window != NULL)
    {
        if (Real && Window->fnid && !(Window->fnid & FNID_DESTROY))
        {
           if (LookupFnIdToiCls(Window->fnid, &iCls))
           {
              Atom = gpsi->atomSysClass[iCls];
           }
        }

        _SEH2_TRY
        {
            ProbeForWriteUnicodeString(ClassName);
            CapturedClassName = *ClassName;

            /* Get the class name */
            Ret = UserGetClassName(Window->pcls,
                                   &CapturedClassName,
                                   Atom,
                                   FALSE);

            if (Ret != 0)
            {
                /* Update the Length field */
                ClassName->Length = CapturedClassName.Length;
            }
        }
        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
        {
            SetLastNtError(_SEH2_GetExceptionCode());
        }
        _SEH2_END;
    }

    UserLeave();

    return Ret;
}

/* Return Pointer to Class structure. */
PCLS APIENTRY
NtUserGetWOWClass(HINSTANCE hInstance,
                  PUNICODE_STRING ClassName)
{
  UNICODE_STRING SafeClassName;
  PPROCESSINFO pi;
  PCLS Class = NULL;
  RTL_ATOM ClassAtom = 0;
  BOOL Hit = FALSE;

  UserEnterExclusive();

  pi = GetW32ProcessInfo();

  _SEH2_TRY
  {
     if (ClassName->Length != 0)
     {
        ProbeForRead( ClassName->Buffer,
                      ClassName->Length,
                      sizeof(WCHAR));

        RtlInitUnicodeString( &SafeClassName, ClassName->Buffer);

        SafeClassName.Buffer = ExAllocatePoolWithTag( PagedPool,
                                                      SafeClassName.MaximumLength,
                                                      TAG_STRING);
        RtlCopyMemory( SafeClassName.Buffer,
                       ClassName->Buffer,
                       SafeClassName.MaximumLength);
     }
     else
     {
        if (!IS_ATOM(ClassName->Buffer))
        {
           ERR("NtUserGetWOWClass() got ClassName instead of Atom!\n");
           Hit = TRUE;
        }
        else
        {
           SafeClassName.Buffer = ClassName->Buffer;
           SafeClassName.Length = 0;
           SafeClassName.MaximumLength = 0;
        }
     }
  }
  _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
  {
     EngSetLastError(ERROR_CLASS_DOES_NOT_EXIST);
     Hit = TRUE;
  }
  _SEH2_END;

  if (!Hit)
  {
     ClassAtom = IntGetClassAtom( &SafeClassName,
                                   hInstance,
                                   pi,
                                  &Class,
                                   NULL);
     if (!ClassAtom)
     {
        EngSetLastError(ERROR_CLASS_DOES_NOT_EXIST);
     }
  }

  if (SafeClassName.Length) ExFreePoolWithTag(SafeClassName.Buffer, TAG_STRING);
  UserLeave();
//
// Don't forget to use DesktopPtrToUser( ? ) with return pointer in user space.
//
  return Class;
}

/* EOF */
