Commit in reactos on MAIN
include/csrss/csrss.h+1-11.48 -> 1.49
include/ddk/mmtypes.h+33-11.19 -> 1.20
include/napi/win32.h-21.20 -> 1.21
include/ntos/zwtypes.h+1-21.44 -> 1.45
ntoskrnl/ex/win32k.c+15-81.16 -> 1.17
ntoskrnl/include/internal/mm.h+131.92 -> 1.93
                         /ps.h+69-521.74 -> 1.75
ntoskrnl/ps/create.c+7-11.85 -> 1.86
           /kill.c+9-31.83 -> 1.84
           /process.c+95-501.151 -> 1.152
           /win32.c+9-41.9 -> 1.10
subsys/csrss/win32csr/desktopbg.c+10-301.10 -> 1.11
                     /guiconsole.c+1-241.21 -> 1.22
subsys/system/winlogon/winlogon.c+1-101.36 -> 1.37
subsys/win32k/include/csr.h+5-11.1 -> 1.2
                     /desktop.h+61.8 -> 1.9
                     /menu.h+1-11.29 -> 1.30
                     /window.h+2-21.60 -> 1.61
                     /winsta.h+2-61.25 -> 1.26
subsys/win32k/main/dllmain.c+78-721.81 -> 1.82
subsys/win32k/ntuser/caret.c+3-251.14 -> 1.15
                    /class.c+11-541.59 -> 1.60
                    /csr.c+58-21.2 -> 1.3
                    /desktop.c+200-181.23 -> 1.24
                    /hook.c+8-81.9 -> 1.10
                    /hotkey.c+7-81.10 -> 1.11
                    /menu.c+35-251.55 -> 1.56
                    /message.c+2-21.74 -> 1.75
                    /metric.c+2-21.22 -> 1.23
                    /misc.c+5-51.87 -> 1.88
                    /monitor.c+2-21.1 -> 1.2
                    /msgqueue.c+7-171.107 -> 1.108
                    /useratom.c+5-91.2 -> 1.3
                    /window.c+31-351.248 -> 1.249
                    /winpos.c+2-21.122 -> 1.123
                    /winsta.c+65-231.65 -> 1.66
subsys/win32k/objects/cursoricon.c+37-991.64 -> 1.65
+838-606
37 modified files
1. removed window station/desktop hacks from ntoskrnl
2. implemented window station/desktop inheritance on thread/process startup
3. implemented process locking
4. fixed some fields in the EPROCESS structure

reactos/include/csrss
csrss.h 1.48 -> 1.49
diff -u -r1.48 -r1.49
--- csrss.h	14 Nov 2004 18:47:09 -0000	1.48
+++ csrss.h	20 Nov 2004 16:46:04 -0000	1.49
@@ -524,7 +524,7 @@
 
 typedef struct
 {
-  WCHAR DesktopName[1];
+  HDESK DesktopHandle;
 } CSRSS_CREATE_DESKTOP_REQUEST, *PCSRSS_CREATE_DESKTOP_REQUEST;
 
 typedef struct

reactos/include/ddk
mmtypes.h 1.19 -> 1.20
diff -u -r1.19 -r1.20
--- mmtypes.h	17 Jul 2004 03:04:45 -0000	1.19
+++ mmtypes.h	20 Nov 2004 16:46:04 -0000	1.20
@@ -1,4 +1,4 @@
-/* $Id: mmtypes.h,v 1.19 2004/07/17 03:04:45 ion Exp $ */
+/* $Id: mmtypes.h,v 1.20 2004/11/20 16:46:04 weiden Exp $ */
 
 #ifndef _INCLUDE_DDK_MMTYPES_H
 #define _INCLUDE_DDK_MMTYPES_H
@@ -106,4 +106,36 @@
 	MmMaximumCacheType
 } MEMORY_CACHING_TYPE;
 
+typedef struct _MMWSL *PMMWSL;
+
+typedef struct _MMSUPPORT_FLAGS {
+    ULONG SessionSpace              : 1;
+    ULONG BeingTrimmed              : 1;
+    ULONG SessionLeader             : 1;
+    ULONG TrimHard                  : 1;
+    ULONG WorkingSetHard            : 1;
+    ULONG AddressSpaceBeingDeleted  : 1;
+    ULONG Available                 : 10;
+    ULONG AllowWorkingSetAdjustment : 8;
+    ULONG MemoryPriority            : 8;
+} MMSUPPORT_FLAGS, *PMMSUPPORT_FLAGS;
+
+typedef struct _MMSUPPORT
+{
+    LARGE_INTEGER   LastTrimTime;
+    MMSUPPORT_FLAGS Flags;
+    ULONG           PageFaultCount;
+    ULONG           PeakWorkingSetSize;
+    ULONG           WorkingSetSize;
+    ULONG           MinimumWorkingSetSize;
+    ULONG           MaximumWorkingSetSize;
+    PMMWSL          VmWorkingSetList;
+    LIST_ENTRY      WorkingSetExpansionLinks;
+    ULONG           Claim;
+    ULONG           NextEstimationSlot;
+    ULONG           NextAgingSlot;
+    ULONG           EstimatedAvailable;
+    ULONG           GrowthSinceLastEstimate;
+} MMSUPPORT, *PMMSUPPORT;
+
 #endif

reactos/include/napi
win32.h 1.20 -> 1.21
diff -u -r1.20 -r1.21
--- win32.h	12 Sep 2004 19:47:48 -0000	1.20
+++ win32.h	20 Nov 2004 16:46:04 -0000	1.21
@@ -31,8 +31,6 @@
   FAST_MUTEX CursorIconListLock;
   LIST_ENTRY CursorIconListHead;
   struct _KBDTABLES* KeyboardLayout;
-  struct _WINSTATION_OBJECT* WindowStation;
-  HANDLE hWindowStation;
   ULONG Flags;
   WORD GDIObjects;
   WORD UserObjects;

reactos/include/ntos
zwtypes.h 1.44 -> 1.45
diff -u -r1.44 -r1.45
--- zwtypes.h	6 Nov 2004 13:38:45 -0000	1.44
+++ zwtypes.h	20 Nov 2004 16:46:04 -0000	1.45
@@ -1231,8 +1231,7 @@
 #define ProcessWow64Information			26
 /* ReactOS private. */
 #define ProcessImageFileName			27
-#define ProcessDesktop                          28
-#define MaxProcessInfoClass			29
+#define MaxProcessInfoClass			28
 
 /*
  * thread query / set information class

reactos/ntoskrnl/ex
win32k.c 1.16 -> 1.17
diff -u -r1.16 -r1.17
--- win32k.c	15 Aug 2004 16:39:01 -0000	1.16
+++ win32k.c	20 Nov 2004 16:46:05 -0000	1.17
@@ -35,16 +35,23 @@
 POBJECT_TYPE EXPORTED ExDesktopObjectType = NULL;
 
 static GENERIC_MAPPING ExpWindowStationMapping = {
-    FILE_GENERIC_READ,
-	  FILE_GENERIC_WRITE,
-	  FILE_GENERIC_EXECUTE,
-	  FILE_ALL_ACCESS };
+  STANDARD_RIGHTS_READ | WINSTA_ENUMDESKTOPS | WINSTA_ENUMERATE |  WINSTA_READATTRIBUTES | WINSTA_READSCREEN,
+  STANDARD_RIGHTS_WRITE | WINSTA_ACCESSCLIPBOARD | WINSTA_CREATEDESKTOP | WINSTA_WRITEATTRIBUTES,
+  STANDARD_RIGHTS_EXECUTE | WINSTA_ACCESSGLOBALATOMS | WINSTA_EXITWINDOWS,
+  STANDARD_RIGHTS_REQUIRED | WINSTA_ACCESSCLIPBOARD | WINSTA_ACCESSGLOBALATOMS | WINSTA_CREATEDESKTOP |
+                             WINSTA_ENUMDESKTOPS | WINSTA_ENUMERATE | WINSTA_EXITWINDOWS |
+                             WINSTA_READATTRIBUTES | WINSTA_READSCREEN | WINSTA_WRITEATTRIBUTES
+};
 
 static GENERIC_MAPPING ExpDesktopMapping = {
-    FILE_GENERIC_READ,
-	  FILE_GENERIC_WRITE,
-	  FILE_GENERIC_EXECUTE,
-	  FILE_ALL_ACCESS };
+  STANDARD_RIGHTS_READ | DESKTOP_ENUMERATE | DESKTOP_READOBJECTS,
+  STANDARD_RIGHTS_WRITE | DESKTOP_CREATEMENU | DESKTOP_CREATEWINDOW | DESKTOP_HOOKCONTROL |
+                          DESKTOP_JOURNALPLAYBACK | DESKTOP_JOURNALRECORD | DESKTOP_WRITEOBJECTS,
+  STANDARD_RIGHTS_EXECUTE | DESKTOP_SWITCHDESKTOP,
+  STANDARD_RIGHTS_REQUIRED | DESKTOP_CREATEMENU | DESKTOP_CREATEWINDOW | DESKTOP_ENUMERATE |
+                             DESKTOP_HOOKCONTROL | DESKTOP_JOURNALPLAYBACK | DESKTOP_JOURNALRECORD |
+                             DESKTOP_READOBJECTS | DESKTOP_SWITCHDESKTOP | DESKTOP_WRITEOBJECTS
+};
 
 /* FUNCTIONS ****************************************************************/
 

reactos/ntoskrnl/include/internal
mm.h 1.92 -> 1.93
diff -u -r1.92 -r1.93
--- mm.h	9 Oct 2004 12:17:54 -0000	1.92
+++ mm.h	20 Nov 2004 16:46:05 -0000	1.93
@@ -152,6 +152,19 @@
 ULONG           PagefileLimit;
 } EPROCESS_QUOTA_BLOCK, *PEPROCESS_QUOTA_BLOCK;
 
+/*
+ * header mess....
+ */
+
+typedef struct _PAGEFAULT_HISTORY
+{
+    ULONG                                 CurrentIndex;
+    ULONG                                 MaxIndex;
+    KSPIN_LOCK                            SpinLock;
+    PVOID                                 Reserved;
+    struct _PROCESS_WS_WATCH_INFORMATION  WatchInfo[1];
+} PAGEFAULT_HISTORY, *PPAGEFAULT_HISTORY;
+
 #endif /* __USE_W32API */
 
 typedef struct

reactos/ntoskrnl/include/internal
ps.h 1.74 -> 1.75
diff -u -r1.74 -r1.75
--- ps.h	11 Nov 2004 22:23:52 -0000	1.74
+++ ps.h	20 Nov 2004 16:46:05 -0000	1.75
@@ -16,7 +16,7 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/* $Id: ps.h,v 1.74 2004/11/11 22:23:52 ion Exp $
+/* $Id: ps.h,v 1.75 2004/11/20 16:46:05 weiden Exp $
  *
  * FILE:            ntoskrnl/ke/kthread.c
  * PURPOSE:         Process manager definitions
@@ -283,6 +283,27 @@
 
 typedef struct _KPROCESS *PKPROCESS;
 
+typedef struct _HARDWARE_PTE_X86 {
+    ULONG Valid             : 1;
+    ULONG Write             : 1;
+    ULONG Owner             : 1;
+    ULONG WriteThrough      : 1;
+    ULONG CacheDisable      : 1;
+    ULONG Accessed          : 1;
+    ULONG Dirty             : 1;
+    ULONG LargePage         : 1;
+    ULONG Global            : 1;
+    ULONG CopyOnWrite       : 1;
+    ULONG Prototype         : 1;
+    ULONG reserved          : 1;
+    ULONG PageFrameNumber   : 20;
+} HARDWARE_PTE_X86, *PHARDWARE_PTE_X86;
+
+typedef struct _WOW64_PROCESS
+{
+  PVOID Wow64;
+} WOW64_PROCESS, *PWOW64_PROCESS;
+
 #endif /* __USE_W32API */
 
 struct _EPROCESS
@@ -306,7 +327,7 @@
   /* Time of process exit. */
   TIME                  ExitTime;                     /* 088 */
   /* Unknown. */
-  PVOID                 LockOwner;                    /* 090 */
+  PKTHREAD              LockOwner;                    /* 090 */
   /* Process id. */
   ULONG                 UniqueProcessId;              /* 094 */
   /* Unknown. */
@@ -325,30 +346,15 @@
   ULONG                 PeakVirtualSize;              /* 0BC */
   /* Unknown. */
   LARGE_INTEGER         VirtualSize;                  /* 0C0 */
-  struct
-  {
-    ULONG               LastTrimTime;
-    ULONG               LastTrimFaultCount;
-    ULONG               PageFaultCount;
-    ULONG               PeakWorkingSetSize;
-    ULONG               WorkingSetSize;
-    ULONG               MinimumWorkingSetSize;
-    ULONG               MaximumWorkingSetSize;
-    ULONG               VmWorkingSetList;
-    LIST_ENTRY          WorkingSetExpansionList;
-    UCHAR               AllowWorkingSetAdjustment;
-    UCHAR               AddressSpaceBeingDeleted;
-    UCHAR               ForegroundPrioritySwitch;
-    UCHAR               MemoryPriority;
-  } Vm;
-  PVOID                 LastProtoPteFault;
-  struct _EPORT*        DebugPort;
-  struct _EPORT*        ExceptionPort;
-  PVOID                 ObjectTable;
+
+  MMSUPPORT             Vm;
+  LIST_ENTRY            SessionProcessLinks;
+  struct _EPORT         *DebugPort;
+  struct _EPORT         *ExceptionPort;
+  HANDLE_TABLE          HandleTable;
   PVOID                 Token;
-  /*  FAST_MUTEX            WorkingSetLock; */
-  KMUTEX                WorkingSetLock;
-  PVOID                 WorkingSetPage;
+  FAST_MUTEX            WorkingSetLock;
+  ULONG                 WorkingSetPage;
   UCHAR                 ProcessOutswapEnabled;
   UCHAR                 ProcessOutswapped;
   UCHAR                 AddressSpaceInitialized;
@@ -360,14 +366,15 @@
   UCHAR                 ForkWasSuccessful;
   UCHAR                 MmAgressiveWsTrimMask;
   PKEVENT               VmOperationEvent;
-  PVOID                 PageDirectoryPte;
+  PVOID                 PaeTop;
   ULONG                 LastFaultCount;
+  ULONG                 ModifiedPageCount;
   PVOID                 VadRoot;
   PVOID                 VadHint;
   PVOID                 CloneRoot;
   ULONG                 NumberOfPrivatePages;
   ULONG                 NumberOfLockedPages;
-  USHORT                NextProcessColour;
+  USHORT                NextPageColor;
   UCHAR                 ExitProcessCalled;
   UCHAR                 CreateProcessReported;
   HANDLE                SectionHandle;
@@ -375,14 +382,20 @@
   PVOID                 SectionBaseAddress;
   PEPROCESS_QUOTA_BLOCK QuotaBlock;
   NTSTATUS              LastThreadExitStatus;
-  PVOID                 WorkingSetWatch;
+  PPAGEFAULT_HISTORY    WorkingSetWatch;
+  HANDLE                Win32WindowStation;
   HANDLE                InheritedFromUniqueProcessId;
-  ACCESS_MASK           GrantedAccess;
+  ULONG                 GrantedAccess;
   ULONG                 DefaultHardErrorProcessing;
   PVOID                 LdtInformation;
-  ULONG                 VadFreeHint;
+  PVOID                 VadFreeHint;
   PVOID                 VdmObjects;
-  KMUTANT               ProcessMutant;
+  PVOID                 DeviceObjects;
+  ULONG                 SessionId;
+  LIST_ENTRY            PhysicalVadList;
+  HARDWARE_PTE_X86      PageDirectoryPte;
+  ULONGLONG             Filler;
+  ULONG                 PaePageDirectoryPage;
   CHAR                  ImageFileName[16];
   ULONG                 VmTrimFaultValue;
   UCHAR                 SetTimerResolution;
@@ -390,29 +403,31 @@
   UCHAR                 SubSystemMinorVersion;
   UCHAR                 SubSystemMajorVersion;
   USHORT                SubSystemVersion;
-  struct _W32PROCESS*   Win32Process;
-  HANDLE                Win32WindowStation;
-   
-   /*
-    * Added by David Welch (welch@mcmail.com)
-    */
-  HANDLE                Win32Desktop;
+  struct _W32PROCESS    *Win32Process;
+  struct _EJOB          *Job;
+  ULONG                 JobStatus;
+  LIST_ENTRY            JobLinks;
+  PVOID                 LockedPagesList;
+  struct _EPORT         *SecurityPort;
+  PWOW64_PROCESS        Wow64;
+  LARGE_INTEGER         ReadOperationCount;
+  LARGE_INTEGER         WriteOperationCount;
+  LARGE_INTEGER         OtherOperationCount;
+  LARGE_INTEGER         ReadTransferCount;
+  LARGE_INTEGER         WriteTransferCount;
+  LARGE_INTEGER         OtherTransferCount;
+  ULONG                 CommitChargeLimit;
+  ULONG                 CommitChargePeak;
+  LIST_ENTRY            ThreadListHead;
+  PRTL_BITMAP           VadPhysicalPagesBitMap;
+  ULONG                 VadPhysicalPages;
+  KSPIN_LOCK            AweLock;
+
+  /*
+   * FIXME - ReactOS specified - remove the following fields ASAP!!!
+   */
   MADDRESS_SPACE        AddressSpace;
-  HANDLE_TABLE          HandleTable;
   LIST_ENTRY            ProcessListEntry;
-   
-   /*
-    * Added by Philip Susi for list of threads in process
-    */
-  LIST_ENTRY           ThreadListHead;
-
-  /* Added by Alex Ionescu (alex@relsoft.net)*/
-  ULONG					SessionId;
-  struct _EPORT*		SecurityPort;
-  
-  struct _EJOB*         Job;
-  UINT                  JobStatus;
-
   FAST_MUTEX            TebLock;
   PVOID                 TebBlock;
   PVOID                 TebLastAllocated;
@@ -651,6 +666,8 @@
 NTSTATUS PsDeleteCidHandle(HANDLE CidHandle, POBJECT_TYPE ObjectType);
 PCID_OBJECT PsLockCidHandle(HANDLE CidHandle, POBJECT_TYPE ObjectType);
 VOID PsUnlockCidObject(PCID_OBJECT CidObject);
+NTSTATUS PsLockProcess(PEPROCESS Process, BOOL Timeout);
+VOID PsUnlockProcess(PEPROCESS Process);
 
 #define ETHREAD_TO_KTHREAD(pEThread) (&(pEThread)->Tcb)
 #define KTHREAD_TO_ETHREAD(pKThread) (CONTAINING_RECORD((pKThread), ETHREAD, Tcb))

reactos/ntoskrnl/ps
create.c 1.85 -> 1.86
diff -u -r1.85 -r1.86
--- create.c	10 Nov 2004 02:51:00 -0000	1.85
+++ create.c	20 Nov 2004 16:46:05 -0000	1.86
@@ -1,4 +1,4 @@
-/* $Id: create.c,v 1.85 2004/11/10 02:51:00 ion Exp $
+/* $Id: create.c,v 1.86 2004/11/20 16:46:05 weiden Exp $
  *
  * COPYRIGHT:              See COPYING in the top level directory
  * PROJECT:                ReactOS kernel
@@ -386,6 +386,12 @@
   Thread->ThreadsProcess = NULL;
 
   PsDeleteCidHandle(Thread->Cid.UniqueThread, PsThreadType);
+  
+  if(Thread->Tcb.Win32Thread != NULL)
+  {
+    /* Free the W32THREAD structure if present */
+    ExFreePool (Thread->Tcb.Win32Thread);
+  }
 
   KeReleaseThread(ETHREAD_TO_KTHREAD(Thread));
   

reactos/ntoskrnl/ps
kill.c 1.83 -> 1.84
diff -u -r1.83 -r1.84
--- kill.c	13 Nov 2004 22:27:15 -0000	1.83
+++ kill.c	20 Nov 2004 16:46:05 -0000	1.84
@@ -1,4 +1,4 @@
-/* $Id: kill.c,v 1.83 2004/11/13 22:27:15 hbirr Exp $
+/* $Id: kill.c,v 1.84 2004/11/20 16:46:05 weiden Exp $
  *
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS kernel
@@ -20,6 +20,7 @@
 extern KSPIN_LOCK PiThreadLock;
 
 VOID PsTerminateCurrentThread(NTSTATUS ExitStatus);
+NTSTATUS STDCALL NtCallTerminatePorts(PETHREAD Thread);
 
 #define TAG_TERMINATE_APC   TAG('T', 'A', 'P', 'C')
 
@@ -135,12 +136,14 @@
    CurrentThread->ExitStatus = ExitStatus;
    KeQuerySystemTime((PLARGE_INTEGER)&CurrentThread->ExitTime);
    KeCancelTimer(&CurrentThread->Tcb.Timer);
+   
+   KeReleaseSpinLock(&PiThreadLock, oldIrql);
  
+   PsLockProcess(CurrentProcess, FALSE);
    /* Remove the thread from the thread list of its process */
    RemoveEntryList(&CurrentThread->ThreadListEntry);
    Last = IsListEmpty(&CurrentProcess->ThreadListHead);
-
-   KeReleaseSpinLock(&PiThreadLock, oldIrql);
+   PsUnlockProcess(CurrentProcess);
 
    /* Notify subsystems of the thread termination */
    PspRunCreateThreadNotifyRoutines(CurrentThread, FALSE);
@@ -188,6 +191,9 @@
    /* The last thread shall close the door on exit */
    if(Last)
    {
+    /* save the last thread exit status */
+    CurrentProcess->LastThreadExitStatus = ExitStatus;
+    
     PspRunCreateProcessNotifyRoutines(CurrentProcess, FALSE);
     PsTerminateWin32Process(CurrentProcess);
     PiTerminateProcess(CurrentProcess, ExitStatus);

reactos/ntoskrnl/ps
process.c 1.151 -> 1.152
diff -u -r1.151 -r1.152
--- process.c	13 Nov 2004 22:27:15 -0000	1.151
+++ process.c	20 Nov 2004 16:46:05 -0000	1.152
@@ -1,4 +1,4 @@
-/* $Id: process.c,v 1.151 2004/11/13 22:27:15 hbirr Exp $
+/* $Id: process.c,v 1.152 2004/11/20 16:46:05 weiden Exp $
  *
  * COPYRIGHT:         See COPYING in the top level directory
  * PROJECT:           ReactOS kernel
@@ -26,6 +26,7 @@
 LIST_ENTRY PsProcessListHead;
 static KSPIN_LOCK PsProcessListLock;
 static ULONG PiNextProcessUniqueId = 0; /* TODO */
+static LARGE_INTEGER ShortPsLockDelay, PsLockTimeout;
 
 static GENERIC_MAPPING PiProcessMapping = {PROCESS_READ,
 					   PROCESS_WRITE,
@@ -245,6 +246,8 @@
    KIRQL oldIrql;
    NTSTATUS Status;
    
+   ShortPsLockDelay.QuadPart = -100LL;
+   PsLockTimeout.QuadPart = -10000000LL; /* one second */
    /*
     * Register the process object type
     */
@@ -314,6 +317,10 @@
    MmInitializeAddressSpace(PsInitialSystemProcess,
 			    &PsInitialSystemProcess->AddressSpace);
    ObCreateHandleTable(NULL,FALSE,PsInitialSystemProcess);
+   
+   KeInitializeEvent(&PsInitialSystemProcess->LockEvent, SynchronizationEvent, FALSE);
+   PsInitialSystemProcess->LockCount = 0;
+   PsInitialSystemProcess->LockOwner = NULL;
 
 #if defined(__GNUC__)
    KProcess->DirectoryTableBase = 
@@ -329,7 +336,6 @@
    PsInitialSystemProcess->UniqueProcessId = 
      InterlockedIncrement((LONG *)&PiNextProcessUniqueId); /* TODO */
    PsInitialSystemProcess->Win32WindowStation = (HANDLE)0;
-   PsInitialSystemProcess->Win32Desktop = (HANDLE)0;
    
    KeAcquireSpinLock(&PsProcessListLock, &oldIrql);
    InsertHeadList(&PsProcessListHead, 
@@ -418,6 +424,12 @@
           KeWaitForSingleObject(&Context.Event, Executive, KernelMode, FALSE, NULL);
 	}
     }
+
+  if(((PEPROCESS)ObjectBody)->Win32Process != NULL)
+  {
+    /* delete the W32PROCESS structure if there's one associated */
+    ExFreePool (((PEPROCESS)ObjectBody)->Win32Process);
+  }
 }
 
 static NTSTATUS
@@ -694,47 +706,13 @@
    ObCreateHandleTable(pParentProcess,
 		       InheritObjectTable,
 		       Process);
-   MmCopyMmInfo(pParentProcess, Process);
-   if (pParentProcess->Win32WindowStation != (HANDLE)0)
-     {
-       /* Always duplicate the process window station. */
-       Process->Win32WindowStation = 0;
-       Status = ObDuplicateObject(pParentProcess,
-				  Process,
-				  pParentProcess->Win32WindowStation,
-				  &Process->Win32WindowStation,
-				  0,
-				  FALSE,
-				  DUPLICATE_SAME_ACCESS);
-       if (!NT_SUCCESS(Status))
-	 {
-	   KEBUGCHECK(0);
-	 }
-     }
-   else
-     {
-       Process->Win32WindowStation = (HANDLE)0;
-     }
-   if (pParentProcess->Win32Desktop != (HANDLE)0)
-     {
-       /* Always duplicate the process window station. */
-       Process->Win32Desktop = 0;
-       Status = ObDuplicateObject(pParentProcess,
-				  Process,
-				  pParentProcess->Win32Desktop,
-				  &Process->Win32Desktop,
-				  0,
-				  FALSE,
-				  DUPLICATE_SAME_ACCESS);
-       if (!NT_SUCCESS(Status))
-	 {
-	   KEBUGCHECK(0);
-	 }
-     }
-   else
-     {
-       Process->Win32Desktop = (HANDLE)0;
-     }
+   MmCopyMmInfo(ParentProcess, Process);
+   
+   KeInitializeEvent(&Process->LockEvent, SynchronizationEvent, FALSE);
+   Process->LockCount = 0;
+   Process->LockOwner = NULL;
+   
+   Process->Win32WindowStation = (HANDLE)0;
 
    KeAcquireSpinLock(&PsProcessListLock, &oldIrql);
    InsertHeadList(&PsProcessListHead, &Process->ProcessListEntry);
@@ -1381,11 +1359,6 @@
       case ProcessWow64Information:
       default:
 	Status = STATUS_INVALID_INFO_CLASS;
-
-     case ProcessDesktop:
-       Process->Win32Desktop = *(PHANDLE)ProcessInformation;
-       Status = STATUS_SUCCESS;
-       break;
      }
    ObDereferenceObject(Process);
    return(Status);
@@ -1977,7 +1950,7 @@
 
 /*
  * @implemented
- */                       
+ */
 VOID
 STDCALL
 PsSetProcessWin32Process(
@@ -1990,7 +1963,7 @@
 
 /*
  * @implemented
- */                       
+ */
 VOID
 STDCALL
 PsSetProcessWin32WindowStation(
@@ -2151,4 +2124,76 @@
 {
 	UNIMPLEMENTED;
 }
+
+NTSTATUS
+PsLockProcess(PEPROCESS Process, BOOL Timeout)
+{
+  ULONG Attempts = 0;
+  PKTHREAD PrevLockOwner;
+  NTSTATUS Status = STATUS_UNSUCCESSFUL;
+  PLARGE_INTEGER Delay = (Timeout ? &PsLockTimeout : NULL);
+  PKTHREAD CallingThread = KeGetCurrentThread();
+  
+  KeEnterCriticalRegion();
+  
+  for(;;)
+  {
+    if(Process->Pcb.State == PROCESS_STATE_TERMINATED)
+    {
+      KeLeaveCriticalRegion();
+      return STATUS_PROCESS_IS_TERMINATING;
+    }
+
+    /* FIXME - why don't we have InterlockedCompareExchangePointer in ntoskrnl?! */
+    PrevLockOwner = (PKTHREAD)InterlockedCompareExchange((LONG*)&Process->LockOwner, (LONG)CallingThread, (LONG)NULL);
+    if(PrevLockOwner == NULL || PrevLockOwner == CallingThread)
+    {
+      /* we got the lock or already locked it */
+      if(InterlockedIncrement((LONG*)&Process->LockCount) == 1)
+      {
+        KeClearEvent(&Process->LockEvent);
+      }
+
+      return STATUS_SUCCESS;
+    }
+    else
+    {
+      if(++Attempts > 2)
+      {
+        Status = KeWaitForSingleObject(&Process->LockEvent,
+                                       Executive,
+                                       KernelMode,
+                                       FALSE,
+                                       Delay);
+        if(Status == STATUS_TIMEOUT)
+        {
+          KeLeaveCriticalRegion();
+          break;
+        }
+      }
+      else
+      {
+        KeDelayExecutionThread(KernelMode, FALSE, &ShortPsLockDelay);
+      }
+    }
+  }
+  
+  return Status;
+}
+
+VOID
+PsUnlockProcess(PEPROCESS Process)
+{
+  assert(Process->LockOwner == KeGetCurrentThread());
+  
+  if(InterlockedDecrement((LONG*)&Process->LockCount) == 0)
+  {
+    /* FIXME - why don't we have InterlockedExchangePointer in ntoskrnl?! */
+    InterlockedExchange((LONG*)&Process->LockOwner, (LONG)NULL);
+    KeSetEvent(&Process->LockEvent, IO_NO_INCREMENT, FALSE);
+  }
+  
+  KeLeaveCriticalRegion();
+}
+
 /* EOF */

reactos/ntoskrnl/ps
win32.c 1.9 -> 1.10
diff -u -r1.9 -r1.10
--- win32.c	28 Sep 2004 15:02:29 -0000	1.9
+++ win32.c	20 Nov 2004 16:46:05 -0000	1.10
@@ -16,7 +16,7 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/* $Id: win32.c,v 1.9 2004/09/28 15:02:29 weiden Exp $
+/* $Id: win32.c,v 1.10 2004/11/20 16:46:05 weiden Exp $
  *
  * COPYRIGHT:              See COPYING in the top level directory
  * PROJECT:                ReactOS kernel
@@ -100,17 +100,20 @@
 
   if (Process->Win32Process == NULL)
     {
+      /* FIXME - lock the process */
       Process->Win32Process = ExAllocatePool (NonPagedPool,
 					      PspWin32ProcessSize);
+
       if (Process->Win32Process == NULL)
 	return STATUS_NO_MEMORY;
 
       RtlZeroMemory (Process->Win32Process,
 		     PspWin32ProcessSize);
+      /* FIXME - unlock the process */
 
       if (PspWin32ProcessCallback != NULL)
 	{
-	  PspWin32ProcessCallback (Process, TRUE);
+          PspWin32ProcessCallback (Process, TRUE);
 	}
     }
 
@@ -145,7 +148,8 @@
       PspWin32ProcessCallback (Process, FALSE);
     }
 
-  ExFreePool (Process->Win32Process);
+  /* don't delete the W32PROCESS structure at this point, wait until the
+     EPROCESS structure is being freed */
 }
 
 
@@ -159,7 +163,8 @@
       PspWin32ThreadCallback (Thread, FALSE);
     }
 
-    ExFreePool (Thread->Tcb.Win32Thread);
+    /* don't delete the W32THREAD structure at this point, wait until the
+       ETHREAD structure is being freed */
   }
 }
 

reactos/subsys/csrss/win32csr
desktopbg.c 1.10 -> 1.11
diff -u -r1.10 -r1.11
--- desktopbg.c	24 Sep 2004 15:07:38 -0000	1.10
+++ desktopbg.c	20 Nov 2004 16:46:05 -0000	1.11
@@ -1,4 +1,4 @@
-/* $Id: desktopbg.c,v 1.10 2004/09/24 15:07:38 navaraf Exp $
+/* $Id: desktopbg.c,v 1.11 2004/11/20 16:46:05 weiden Exp $
  *
  * reactos/subsys/csrss/win32csr/desktopbg.c
  *
@@ -72,7 +72,7 @@
         PAINTSTRUCT PS;
         RECT rc;
         HDC hDC;
-        
+
         if(GetUpdateRect(Wnd, &rc, FALSE) &&
            (hDC = BeginPaint(Wnd, &PS)))
         {
@@ -94,7 +94,7 @@
       case WM_NOTIFY:
       {
         PPRIVATE_NOTIFY_DESKTOP nmh = (PPRIVATE_NOTIFY_DESKTOP)lParam;
-        
+
         /* Use WM_NOTIFY for private messages since it can't be sent between
            processes! */
         switch(nmh->hdr.code)
@@ -125,7 +125,7 @@
             VisibleDesktopWindow = NULL;
             return Result;
           }
-          
+
           default:
             DPRINT("Unknown notification code 0x%x sent to the desktop window!\n", nmh->code);
             return 0;
@@ -140,23 +140,9 @@
 DtbgInit()
 {
   WNDCLASSEXW Class;
-  HWINSTA WindowStation;
   ATOM ClassAtom;
 
-  /* Attach to window station */
-  WindowStation = OpenWindowStationW(L"WinSta0", FALSE, GENERIC_ALL);
-  if (NULL == WindowStation)
-    {
-      DPRINT1("Failed to open window station\n");
-      return FALSE;
-    }
-  if (! SetProcessWindowStation(WindowStation))
-    {
-      DPRINT1("Failed to set process window station\n");
-      return FALSE;
-    }
-
-  /* 
+  /*
    * Create the desktop window class
    */
   Class.cbSize = sizeof(WNDCLASSEXW);
@@ -229,7 +215,6 @@
 
 CSR_API(CsrCreateDesktop)
 {
-  HDESK Desktop;
   DTBG_THREAD_DATA ThreadData;
   HANDLE ThreadHandle;
 
@@ -247,16 +232,11 @@
         }
     }
 
-  Desktop = OpenDesktopW(Request->Data.CreateDesktopRequest.DesktopName,
-                         0, FALSE, GENERIC_ALL);
-  if (NULL == Desktop)
-    {
-      DPRINT1("Failed to open desktop %S\n",
-              Request->Data.CreateDesktopRequest.DesktopName);
-      return Reply->Status = STATUS_UNSUCCESSFUL;
-    }
+  /*
+   * the desktop handle we got from win32k is in the scope of CSRSS so we can just use it
+   */
+  ThreadData.Desktop = Request->Data.CreateDesktopRequest.DesktopHandle;
 
-  ThreadData.Desktop = Desktop;
   ThreadData.Event = CreateEventW(NULL, FALSE, FALSE, NULL);
   if (NULL == ThreadData.Event)
     {
@@ -296,7 +276,7 @@
   nmh.hdr.hwndFrom = Request->Data.ShowDesktopRequest.DesktopWindow;
   nmh.hdr.idFrom = 0;
   nmh.hdr.code = PM_SHOW_DESKTOP;
-  
+
   nmh.ShowDesktop.Width = (int)Request->Data.ShowDesktopRequest.Width;
   nmh.ShowDesktop.Height = (int)Request->Data.ShowDesktopRequest.Height;
 

reactos/subsys/csrss/win32csr
guiconsole.c 1.21 -> 1.22
diff -u -r1.21 -r1.22
--- guiconsole.c	26 Sep 2004 15:55:53 -0000	1.21
+++ guiconsole.c	20 Nov 2004 16:46:05 -0000	1.22
@@ -1,4 +1,4 @@
-/* $Id: guiconsole.c,v 1.21 2004/09/26 15:55:53 weiden Exp $
+/* $Id: guiconsole.c,v 1.22 2004/11/20 16:46:05 weiden Exp $
  *
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS system libraries
@@ -875,31 +875,8 @@
 static BOOL FASTCALL
 GuiInit(VOID)
 {
-  HDESK Desktop;
-  NTSTATUS Status;
   WNDCLASSEXW wc;
 
-  Desktop = OpenDesktopW(L"Default", 0, FALSE, GENERIC_ALL);
-  if (NULL == Desktop)
-    {
-      DPRINT1("Failed to open desktop\n");
-      return FALSE;
-    }
-  Status = NtSetInformationProcess(NtCurrentProcess(),
-                                   ProcessDesktop,
-                                   &Desktop,
-                                   sizeof(Desktop));
-  if (!NT_SUCCESS(Status))
-    {
-      DPRINT1("Cannot set default desktop.\n");
-      return FALSE;
-    }
-  if (! SetThreadDesktop(Desktop))
-    {
-      DPRINT1("Failed to set thread desktop\n");
-      return FALSE;
-    }
-
   if (NULL == NotifyWnd)
     {
       PrivateCsrssManualGuiCheck(+1);

reactos/subsys/system/winlogon
winlogon.c 1.36 -> 1.37
diff -u -r1.36 -r1.37
--- winlogon.c	20 Nov 2004 15:55:45 -0000	1.36
+++ winlogon.c	20 Nov 2004 16:46:05 -0000	1.37
@@ -1,4 +1,4 @@
-/* $Id: winlogon.c,v 1.36 2004/11/20 15:55:45 ekohl Exp $
+/* $Id: winlogon.c,v 1.37 2004/11/20 16:46:05 weiden Exp $
  * 
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS kernel
@@ -555,7 +555,6 @@
   LSA_OPERATIONAL_MODE Mode;
   ULONG AuthenticationPackage;
 #endif
-  NTSTATUS Status;
 
   hAppInstance = hInstance;
   
@@ -598,14 +597,6 @@
    * Switch to winlogon desktop
    */
   /* FIXME: Do start up in the application desktop for now. */
-  Status = NtSetInformationProcess(NtCurrentProcess(),
-                                   ProcessDesktop,
-                                   &WLSession->ApplicationDesktop,
-                                   sizeof(HDESK));
-  if(!NT_SUCCESS(Status))
-  {
-    DbgPrint("WL: Cannot set default desktop for winlogon.\n");
-  }
   SetThreadDesktop(WLSession->ApplicationDesktop);
   if(!SwitchDesktop(WLSession->ApplicationDesktop))
   {

reactos/subsys/win32k/include
csr.h 1.1 -> 1.2
diff -u -r1.1 -r1.2
--- csr.h	28 May 2004 21:33:41 -0000	1.1
+++ csr.h	20 Nov 2004 16:46:05 -0000	1.2
@@ -1,4 +1,4 @@
-/* $Id: csr.h,v 1.1 2004/05/28 21:33:41 gvg Exp $
+/* $Id: csr.h,v 1.2 2004/11/20 16:46:05 weiden Exp $
  *
  * COPYRIGHT:        See COPYING in the top level directory
  * PROJECT:          ReactOS kernel
@@ -10,8 +10,12 @@
 #ifndef CSR_H_INCLUDED
 #define CSR_H_INCLUDED
 
+extern PEPROCESS CsrProcess;
+
 extern NTSTATUS FASTCALL CsrInit(void);
 extern NTSTATUS FASTCALL CsrNotify(PCSRSS_API_REQUEST Request, PCSRSS_API_REPLY Reply);
+extern NTSTATUS FASTCALL CsrCloseHandle(HANDLE Handle);
+extern NTSTATUS STDCALL CsrInsertObject(PVOID, PACCESS_STATE, ACCESS_MASK, ULONG, PVOID*, PHANDLE);
 
 #endif /* CSR_H_INCLUDED */
 

reactos/subsys/win32k/include
desktop.h 1.8 -> 1.9
diff -u -r1.8 -r1.9
--- desktop.h	9 Jul 2004 20:57:38 -0000	1.8
+++ desktop.h	20 Nov 2004 16:46:05 -0000	1.9
@@ -59,6 +59,12 @@
    ACCESS_MASK DesiredAccess,
    PDESKTOP_OBJECT *Object);
 
+NTSTATUS FASTCALL
+IntParseDesktopPath(PEPROCESS Process,
+                    PUNICODE_STRING DesktopPath,
+                    HWINSTA *hWinSta,
+                    HDESK *hDesktop);
+
 #define IntIsActiveDesktop(Desktop) \
   ((Desktop)->WindowStation->ActiveDesktop == (Desktop))
 

reactos/subsys/win32k/include
menu.h 1.29 -> 1.30
diff -u -r1.29 -r1.30
--- menu.h	24 Feb 2004 13:27:02 -0000	1.29
+++ menu.h	20 Nov 2004 16:46:05 -0000	1.30
@@ -33,7 +33,7 @@
 
 typedef struct _MENU_OBJECT
 {
-  PW32PROCESS W32Process;
+  PEPROCESS Process;
   LIST_ENTRY ListEntry;
   FAST_MUTEX MenuItemsLock;
   PMENU_ITEM MenuItemList;

reactos/subsys/win32k/include
window.h 1.60 -> 1.61
diff -u -r1.60 -r1.61
--- window.h	28 Sep 2004 15:02:30 -0000	1.60
+++ window.h	20 Nov 2004 16:46:05 -0000	1.61
@@ -129,7 +129,7 @@
   (hWnd == HWND_BROADCAST || hWnd == HWND_TOPMOST)
 
 #define IntGetWindowObject(hWnd) \
-  IntGetProcessWindowObject(PsGetWin32Process(), hWnd)
+  IntGetProcessWindowObject(PsGetWin32Thread(), hWnd)
 
 #define IntReferenceWindowObject(WndObj) \
   ObmReferenceObjectByPointer(WndObj, otWindow)
@@ -161,7 +161,7 @@
 
 
 PWINDOW_OBJECT FASTCALL
-IntGetProcessWindowObject(PW32PROCESS ProcessData, HWND hWnd);
+IntGetProcessWindowObject(PW32THREAD Thread, HWND hWnd);
 
 BOOL FASTCALL
 IntIsWindow(HWND hWnd);

reactos/subsys/win32k/include
winsta.h 1.25 -> 1.26
diff -u -r1.25 -r1.26
--- winsta.h	1 May 2004 16:43:14 -0000	1.25
+++ winsta.h	20 Nov 2004 16:46:05 -0000	1.26
@@ -6,12 +6,6 @@
 #include <internal/ex.h>
 #include <internal/ps.h>
 #include "msgqueue.h"
-
-#define PROCESS_WINDOW_STATION() \
-  ((HWINSTA)(IoGetCurrentProcess()->Win32WindowStation))
-
-#define SET_PROCESS_WINDOW_STATION(WinSta) \
-  ((IoGetCurrentProcess()->Win32WindowStation) = (PVOID)(WinSta))
   
 #define WINSTA_ROOT_NAME	L"\\Windows\\WindowStations"
 #define WINSTA_ROOT_NAME_LENGTH	23
@@ -51,6 +45,8 @@
    IN PUNICODE_STRING WinStaName,
    IN OPTIONAL PUNICODE_STRING DesktopName);
 
+PWINSTATION_OBJECT FASTCALL IntGetWinStaObj(VOID);
+
 #endif /* _WIN32K_WINSTA_H */
 
 /* EOF */

reactos/subsys/win32k/main
dllmain.c 1.81 -> 1.82
diff -u -r1.81 -r1.82
--- dllmain.c	16 Nov 2004 16:27:48 -0000	1.81
+++ dllmain.c	20 Nov 2004 16:46:05 -0000	1.82
@@ -16,12 +16,15 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/* $Id: dllmain.c,v 1.81 2004/11/16 16:27:48 blight Exp $
+/* $Id: dllmain.c,v 1.82 2004/11/20 16:46:05 weiden Exp $
  *
  *  Entry Point for win32k.sys
  */
 #include <w32k.h>
-#include <roscfg.h>
+
+#define NDEBUG
+#include <win32k/debug1.h>
+#include <debug.h>
 
 #ifdef __USE_W32API
 typedef NTSTATUS (STDCALL *PW32_PROCESS_CALLBACK)(
@@ -51,18 +54,11 @@
 		     BOOLEAN Create)
 {
   PW32PROCESS Win32Process;
-  NTSTATUS Status;
-
-#if 0
-  DbgPrint ("Win32kProcessCallback() called\n");
-#endif
-
+  
   Win32Process = Process->Win32Process;
   if (Create)
     {
-#if 0
-      DbgPrint ("  Create process\n");
-#endif
+      DPRINT("Creating W32 process PID:%d at IRQ level: %lu\n", Process->UniqueProcessId, KeGetCurrentIrql());
 
       InitializeListHead(&Win32Process->ClassListHead);
       ExInitializeFastMutex(&Win32Process->ClassListLock);
@@ -77,31 +73,14 @@
       ExInitializeFastMutex(&Win32Process->CursorIconListLock);
 
       Win32Process->KeyboardLayout = W32kGetDefaultKeyLayout();
-      Win32Process->WindowStation = NULL;
-      if (Process->Win32WindowStation != NULL)
-	{
-	  Status = 
-	    IntValidateWindowStationHandle(Process->Win32WindowStation,
-					   UserMode,
-					   GENERIC_ALL,
-					   &Win32Process->WindowStation);
-	  if (!NT_SUCCESS(Status))
-	    {
-	      DbgPrint("Win32K: Failed to reference a window station for "
-		       "process.\n");
-	    }
-	}
       
       /* setup process flags */
       Win32Process->Flags = 0;
     }
   else
     {
-#if 0
-      DbgPrint ("  Destroy process\n");
-      DbgPrint ("  IRQ level: %lu\n", KeGetCurrentIrql ());
-#endif
-	  IntRemoveProcessWndProcHandles((HANDLE)Process->UniqueProcessId);
+      DPRINT("Destroying W32 process PID:%d at IRQ level: %lu\n", Process->UniqueProcessId, KeGetCurrentIrql());
+      IntRemoveProcessWndProcHandles((HANDLE)Process->UniqueProcessId);
       IntCleanupMenus(Process, Win32Process);
       IntCleanupCurIcons(Process, Win32Process);
       CleanupMonitorImpl();
@@ -129,19 +108,63 @@
 {
   struct _EPROCESS *Process;
   PW32THREAD Win32Thread;
-  NTSTATUS Status;
-
-#if 0
-  DbgPrint ("Win32kThreadCallback() called\n");
-#endif
 
   Process = Thread->ThreadsProcess;
   Win32Thread = Thread->Tcb.Win32Thread;
   if (Create)
     {
-#if 0
-      DbgPrint ("  Create thread\n");
-#endif
+      HWINSTA hWinSta = NULL;
+      HDESK hDesk = NULL;
+      NTSTATUS Status;
+      PUNICODE_STRING DesktopPath;
+      PRTL_USER_PROCESS_PARAMETERS ProcessParams = (Process->Peb ? Process->Peb->ProcessParameters : NULL);
+
+      DPRINT("Creating W32 thread TID:%d at IRQ level: %lu\n", Thread->Cid.UniqueThread, KeGetCurrentIrql());
+      
+      /*
+       * inherit the thread desktop and process window station (if not yet inherited) from the process startup
+       * info structure. See documentation of CreateProcess()
+       */
+      DesktopPath = (ProcessParams ? ((ProcessParams->DesktopInfo.Length > 0) ? &ProcessParams->DesktopInfo : NULL) : NULL);
+      Status = IntParseDesktopPath(Process,
+                                   DesktopPath,
+                                   &hWinSta,
+                                   &hDesk);
+      if(NT_SUCCESS(Status))
+      {
+        if(hWinSta != NULL)
+        {
+          if(Process != CsrProcess)
+          {
+            HWINSTA hProcessWinSta = (HWINSTA)InterlockedCompareExchangePointer((PVOID)&Process->Win32WindowStation, (PVOID)hWinSta, NULL);
+            if(hProcessWinSta != NULL)
+            {
+              /* our process is already assigned to a different window station, we don't need the handle anymore */
+              NtClose(hWinSta);
+            }
+          }
+          else
+          {
+            NtClose(hWinSta);
+          }
+        }
+
+        Win32Thread->hDesktop = hDesk;
+
+        Status = ObReferenceObjectByHandle(hDesk,
+                 0,
+                 ExDesktopObjectType,
+                 KernelMode,
+                 (PVOID*)&Win32Thread->Desktop,
+                 NULL);
+
+        if(!NT_SUCCESS(Status))
+        {
+          DPRINT1("Unable to reference thread desktop handle 0x%x\n", hDesk);
+          Win32Thread->Desktop = NULL;
+          NtClose(hDesk);
+        }
+      }
 
       Win32Thread->IsExiting = FALSE;
       IntDestroyCaret(Win32Thread);
@@ -152,31 +175,10 @@
       ExInitializeFastMutex(&Win32Thread->WindowListLock);
       InitializeListHead(&Win32Thread->W32CallbackListHead);
       ExInitializeFastMutex(&Win32Thread->W32CallbackListLock);
-
-      /* By default threads get assigned their process's desktop. */
-      Win32Thread->Desktop = NULL;
-      Win32Thread->hDesktop = NULL;
-      if (Process->Win32Desktop != NULL)
-	{
-	  Status = ObReferenceObjectByHandle(Process->Win32Desktop,
-					     GENERIC_ALL,
-					     ExDesktopObjectType,
-					     UserMode,
-					     (PVOID*)&Win32Thread->Desktop,
-					     NULL);
-	  if (!NT_SUCCESS(Status))
-	    {
-	      DbgPrint("Win32K: Failed to reference a desktop for thread.\n");
-	    }
-	  
-	  Win32Thread->hDesktop = Process->Win32Desktop;
-	}
     }
   else
     {
-#if 0
-      DbgPrint ("  Destroy thread\n");
-#endif
+      DPRINT("Destroying W32 thread TID:%d at IRQ level: %lu\n", Thread->Cid.UniqueThread, KeGetCurrentIrql());
 
       Win32Thread->IsExiting = TRUE;
       HOOK_DestroyThreadHooks(Thread);
@@ -186,6 +188,10 @@
       IntBlockInput(Win32Thread, FALSE);
       MsqDestroyMessageQueue(Win32Thread->MessageQueue);
       IntCleanupThreadCallbacks(Win32Thread);
+      if(Win32Thread->Desktop != NULL)
+      {
+        ObDereferenceObject(Win32Thread->Desktop);
+      }
     }
 
   return STATUS_SUCCESS;
@@ -215,7 +221,7 @@
 				    1);
   if (Result == FALSE)
     {
-      DbgPrint("Adding system services failed!\n");
+      DPRINT1("Adding system services failed!\n");
       return STATUS_UNSUCCESSFUL;
     }
 
@@ -232,49 +238,49 @@
   Status = InitWindowStationImpl();
   if (!NT_SUCCESS(Status))
   {
-    DbgPrint("Failed to initialize window station implementation!\n");
+    DPRINT1("Failed to initialize window station implementation!\n");
     return STATUS_UNSUCCESSFUL;
   }
 
   Status = InitClassImpl();
   if (!NT_SUCCESS(Status))
   {
-    DbgPrint("Failed to initialize window class implementation!\n");
+    DPRINT1("Failed to initialize window class implementation!\n");
     return STATUS_UNSUCCESSFUL;
   }
 
   Status = InitDesktopImpl();
   if (!NT_SUCCESS(Status))
   {
-    DbgPrint("Failed to initialize desktop implementation!\n");
+    DPRINT1("Failed to initialize desktop implementation!\n");
     return STATUS_UNSUCCESSFUL;
   }
 
   Status = InitWindowImpl();
   if (!NT_SUCCESS(Status))
   {
-    DbgPrint("Failed to initialize window implementation!\n");
+    DPRINT1("Failed to initialize window implementation!\n");
     return STATUS_UNSUCCESSFUL;
   }
 
   Status = InitMenuImpl();
   if (!NT_SUCCESS(Status))
   {
-    DbgPrint("Failed to initialize menu implementation!\n");
+    DPRINT1("Failed to initialize menu implementation!\n");
     return STATUS_UNSUCCESSFUL;
   }
 
   Status = InitInputImpl();
   if (!NT_SUCCESS(Status))
     {
-      DbgPrint("Failed to initialize input implementation.\n");
+      DPRINT1("Failed to initialize input implementation.\n");
       return(Status);
     }
 
   Status = InitKeyboardImpl();
   if (!NT_SUCCESS(Status))
     {
-      DbgPrint("Failed to initialize keyboard implementation.\n");
+      DPRINT1("Failed to initialize keyboard implementation.\n");
       return(Status);
     }
 
@@ -288,28 +294,28 @@
   Status = MsqInitializeImpl();
   if (!NT_SUCCESS(Status))
     {
-      DbgPrint("Failed to initialize message queue implementation.\n");
+      DPRINT1("Failed to initialize message queue implementation.\n");
       return(Status);
     }
 
   Status = InitTimerImpl();
   if (!NT_SUCCESS(Status))
     {
-      DbgPrint("Failed to initialize timer implementation.\n");
+      DPRINT1("Failed to initialize timer implementation.\n");
       return(Status);
     }
 
   Status = InitAcceleratorImpl();
   if (!NT_SUCCESS(Status))
     {
-      DbgPrint("Failed to initialize accelerator implementation.\n");
+      DPRINT1("Failed to initialize accelerator implementation.\n");
       return(Status);
     }
 
   Status = InitGuiCheckImpl();
   if (!NT_SUCCESS(Status))
     {
-      DbgPrint("Failed to initialize GUI check implementation.\n");
+      DPRINT1("Failed to initialize GUI check implementation.\n");
       return(Status);
     }
 

reactos/subsys/win32k/ntuser
caret.c 1.14 -> 1.15
diff -u -r1.14 -r1.15
--- caret.c	4 Aug 2004 22:31:17 -0000	1.14
+++ caret.c	20 Nov 2004 16:46:06 -0000	1.15
@@ -1,4 +1,4 @@
-/* $Id: caret.c,v 1.14 2004/08/04 22:31:17 weiden Exp $
+/* $Id: caret.c,v 1.15 2004/11/20 16:46:06 weiden Exp $
  *
  * COPYRIGHT:        See COPYING in the top level directory
  * PROJECT:          ReactOS kernel
@@ -54,18 +54,7 @@
 IntSetCaretBlinkTime(UINT uMSeconds)
 {
   /* Don't save the new value to the registry! */
-  NTSTATUS Status;
-  PWINSTATION_OBJECT WinStaObject;
-  
-  Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
-	                                  KernelMode,
-				          0,
-				          &WinStaObject);
-  if(!NT_SUCCESS(Status))
-  {
-    SetLastNtError(Status);
-    return FALSE;
-  }
+  PWINSTATION_OBJECT WinStaObject = PsGetWin32Thread()->Desktop->WindowStation;
   
   /* windows doesn't do this check */
   if((uMSeconds < MIN_CARETBLINKRATE) || (uMSeconds > MAX_CARETBLINKRATE))
@@ -77,7 +66,6 @@
   
   WinStaObject->CaretBlinkRate = uMSeconds;
   
-  ObDereferenceObject(WinStaObject);
   return TRUE;
 }
 
@@ -151,19 +139,10 @@
 UINT FASTCALL
 IntGetCaretBlinkTime(VOID)
 {
-  NTSTATUS Status;
   PWINSTATION_OBJECT WinStaObject;
   UINT Ret;
   
-  Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
-	                                  KernelMode,
-				          0,
-				          &WinStaObject);
-  if(!NT_SUCCESS(Status))
-  {
-    SetLastNtError(Status);
-    return 0;
-  }
+  WinStaObject = PsGetWin32Thread()->Desktop->WindowStation;
   
   Ret = WinStaObject->CaretBlinkRate;
   if(!Ret)
@@ -178,7 +157,6 @@
     Ret = DEFAULT_CARETBLINKRATE;
   }
   
-  ObDereferenceObject(WinStaObject);
   return Ret;
 }
 

reactos/subsys/win32k/ntuser
class.c 1.59 -> 1.60
diff -u -r1.59 -r1.60
--- class.c	21 Jun 2004 20:56:53 -0000	1.59
+++ class.c	20 Nov 2004 16:46:06 -0000	1.60
@@ -16,7 +16,7 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/* $Id: class.c,v 1.59 2004/06/21 20:56:53 hbirr Exp $
+/* $Id: class.c,v 1.60 2004/11/20 16:46:06 weiden Exp $
  *
  * COPYRIGHT:        See COPYING in the top level directory
  * PROJECT:          ReactOS kernel
@@ -99,21 +99,10 @@
    BOOL Found;
    RTL_ATOM ClassAtom;
 
-   if (!ClassName)
+   if (!ClassName || !PsGetWin32Thread()->Desktop)
       return FALSE;
 
-   Status = IntValidateWindowStationHandle(
-      PROCESS_WINDOW_STATION(),
-      KernelMode,
-      0,
-      &WinStaObject);
-
-   if (!NT_SUCCESS(Status))
-   {
-      DPRINT("Validation of window station handle (0x%X) failed\n",
-	     PROCESS_WINDOW_STATION());
-      return FALSE;
-   }
+   WinStaObject = PsGetWin32Thread()->Desktop->WindowStation;
 
    Status = RtlLookupAtomInAtomTable(
       WinStaObject->AtomTable,
@@ -122,12 +111,10 @@
 
    if (!NT_SUCCESS(Status))
    {
-      ObDereferenceObject(WinStaObject);  
       return FALSE;
    }
 
    Found = ClassReferenceClassByAtom(Class, ClassAtom, hInstance);
-   ObDereferenceObject(WinStaObject);  
 
    return Found;
 }
@@ -205,14 +192,13 @@
    PWINSTATION_OBJECT WinStaObject;
    NTSTATUS Status;
 
-   Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
-      KernelMode, 0, &WinStaObject);
-   if (!NT_SUCCESS(Status))
+   if(!PsGetWin32Thread()->Desktop)
    {
-      DPRINT("Validation of window station handle (0x%X) failed\n",
-         PROCESS_WINDOW_STATION());
       return 0;
    }
+
+   WinStaObject = PsGetWin32Thread()->Desktop->WindowStation;
+   
    Length = 0;
    Status = RtlQueryAtomInAtomTable(WinStaObject->AtomTable,
       WindowObject->Class->Atom, NULL, NULL, NULL, &Length);
@@ -233,7 +219,6 @@
    /* FIXME: Check buffer size before doing this! */
    *(lpClassName + Length) = 0;
    ExFreePool(Name);
-   ObDereferenceObject(WinStaObject);
 
    return Length;
 }
@@ -426,18 +411,8 @@
       return (RTL_ATOM)0;
    }
   
-  DPRINT("About to open window station handle (0x%X)\n", 
-    PROCESS_WINDOW_STATION());
-  Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
-    KernelMode,
-    0,
-    &WinStaObject);
-  if (!NT_SUCCESS(Status))
-  {
-    DPRINT("Validation of window station handle (0x%X) failed\n",
-      PROCESS_WINDOW_STATION());
-    return((RTL_ATOM)0);
-  }
+  WinStaObject = PsGetWin32Thread()->Desktop->WindowStation;
+  
   if (ClassName->Length)
   {
     DPRINT("NtUserRegisterClassExWOW(%S)\n", ClassName->Buffer);
@@ -447,7 +422,6 @@
       &Atom);
     if (!NT_SUCCESS(Status))
     {
-      ObDereferenceObject(WinStaObject);
       DPRINT("Failed adding class name (%S) to atom table\n",
 	ClassName->Buffer);
       SetLastNtError(Status);      
@@ -465,14 +439,12 @@
     {
       RtlDeleteAtomFromAtomTable(WinStaObject->AtomTable, Atom);
     }
-    ObDereferenceObject(WinStaObject);
     DPRINT("Failed creating window class object\n");
     return((RTL_ATOM)0);
   }
   IntLockProcessClasses(PsGetWin32Process());
   InsertTailList(&PsGetWin32Process()->ClassListHead, &ClassObject->ListEntry);
   IntUnLockProcessClasses(PsGetWin32Process());
-  ObDereferenceObject(WinStaObject);
   return(Atom);
 }
 
@@ -668,32 +640,21 @@
 	 HINSTANCE hInstance,
 	 DWORD Unknown)
 {
-   NTSTATUS Status;
    PWNDCLASS_OBJECT Class;
    PWINSTATION_OBJECT WinStaObject;
   
    DPRINT("NtUserUnregisterClass(%S)\n", ClassNameOrAtom);
    
-   if (!ClassNameOrAtom)
+   if (!ClassNameOrAtom || !PsGetWin32Thread()->Desktop)
    {
       SetLastWin32Error(ERROR_INVALID_PARAMETER);
       return FALSE;
    }
   
-   Status = IntValidateWindowStationHandle(
-      PROCESS_WINDOW_STATION(),
-      KernelMode,
-      0,
-      &WinStaObject);
-   if (!NT_SUCCESS(Status))
-   {
-      SetLastWin32Error(ERROR_INVALID_HANDLE);
-      return FALSE;
-   }
+   WinStaObject = PsGetWin32Thread()->Desktop->WindowStation;
 
    if (!ClassReferenceClassByNameOrAtom(&Class, ClassNameOrAtom, hInstance))
    {
-      ObDereferenceObject(WinStaObject);
       SetLastWin32Error(ERROR_CLASS_DOES_NOT_EXIST);
       return FALSE;
    }
@@ -701,7 +662,6 @@
    if (Class->hInstance && Class->hInstance != hInstance)
    {
       ClassDereferenceObject(Class);
-      ObDereferenceObject(WinStaObject);
       SetLastWin32Error(ERROR_CLASS_DOES_NOT_EXIST);
       return FALSE;
    }
@@ -712,7 +672,6 @@
       IntUnLockClassWindows(Class);
       /* Dereference the ClassReferenceClassByNameOrAtom() call */
       ObmDereferenceObject(Class);
-      ObDereferenceObject(WinStaObject);
       SetLastWin32Error(ERROR_CLASS_HAS_WINDOWS);
       return FALSE;
    }
@@ -727,8 +686,6 @@
   
    /* Free the object */
    ClassDereferenceObject(Class);
-   
-   ObDereferenceObject(WinStaObject);
   
    return TRUE;
 }

reactos/subsys/win32k/ntuser
csr.c 1.2 -> 1.3
diff -u -r1.2 -r1.3
--- csr.c	12 Jul 2004 20:09:35 -0000	1.2
+++ csr.c	20 Nov 2004 16:46:06 -0000	1.3
@@ -1,4 +1,4 @@
-/* $Id: csr.c,v 1.2 2004/07/12 20:09:35 gvg Exp $
+/* $Id: csr.c,v 1.3 2004/11/20 16:46:06 weiden Exp $
  *
  * COPYRIGHT:        See COPYING in the top level directory
  * PROJECT:          ReactOS kernel
@@ -10,7 +10,7 @@
 #include <w32k.h>
 
 static HANDLE WindowsApiPort = NULL;
-static PEPROCESS CsrProcess = NULL;
+PEPROCESS CsrProcess = NULL;
 
 NTSTATUS FASTCALL
 CsrInit(void)
@@ -76,4 +76,60 @@
   return Status;
 }
 
+NTSTATUS STDCALL
+CsrInsertObject(PVOID Object,
+                PACCESS_STATE PassedAccessState,
+                ACCESS_MASK DesiredAccess,
+                ULONG AdditionalReferences,
+                PVOID* ReferencedObject,
+                PHANDLE Handle)
+{
+  NTSTATUS Status;
+  PEPROCESS OldProcess;
+
+  /* Switch to the process in which the handle is valid */
+  OldProcess = PsGetCurrentProcess();
+  if (CsrProcess != OldProcess)
+    {
+      KeAttachProcess(CsrProcess);
+    }
+
+  Status = ObInsertObject(Object,
+                          PassedAccessState,
+                          DesiredAccess,
+                          AdditionalReferences,
+                          ReferencedObject,
+                          Handle);
+
+  if (CsrProcess != OldProcess)
+    {
+      KeDetachProcess();
+    }
+
+  return Status;
+}
+
+NTSTATUS FASTCALL
+CsrCloseHandle(HANDLE Handle)
+{
+  NTSTATUS Status;
+  PEPROCESS OldProcess;
+  
+  /* Switch to the process in which the handle is valid */
+  OldProcess = PsGetCurrentProcess();
+  if (CsrProcess != OldProcess)
+    {
+      KeAttachProcess(CsrProcess);
+    }
+
+  Status = ZwClose(Handle);
+
+  if (CsrProcess != OldProcess)
+    {
+      KeDetachProcess();
+    }
+
+  return Status;
+}
+
 /* EOF */

reactos/subsys/win32k/ntuser
desktop.c 1.23 -> 1.24
diff -u -r1.23 -r1.24
--- desktop.c	6 Nov 2004 22:28:11 -0000	1.23
+++ desktop.c	20 Nov 2004 16:46:06 -0000	1.24
@@ -16,7 +16,7 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  *
- *  $Id: desktop.c,v 1.23 2004/11/06 22:28:11 gvg Exp $
+ *  $Id: desktop.c,v 1.24 2004/11/20 16:46:06 weiden Exp $
  *
  *  COPYRIGHT:        See COPYING in the top level directory
  *  PROJECT:          ReactOS kernel
@@ -66,6 +66,179 @@
 
 /* PRIVATE FUNCTIONS **********************************************************/
 
+NTSTATUS FASTCALL
+IntParseDesktopPath(PEPROCESS Process,
+                    PUNICODE_STRING DesktopPath,
+                    HWINSTA *hWinSta,
+                    HDESK *hDesktop)
+{
+  OBJECT_ATTRIBUTES ObjectAttributes;
+  UNICODE_STRING WinSta, Desktop, FullName;
+  BOOL DesktopPresent = FALSE;
+  BOOL WinStaPresent = FALSE;
+  NTSTATUS Status;
+  
+  ASSERT(hWinSta);
+  
+  *hWinSta = NULL;
+  
+  if(hDesktop != NULL)
+  {
+    *hDesktop = NULL;
+  }
+  
+  RtlInitUnicodeString(&WinSta, NULL);
+  RtlInitUnicodeString(&Desktop, NULL);
+  
+  if(DesktopPath != NULL && DesktopPath->Buffer != NULL && DesktopPath->Length > sizeof(WCHAR))
+  {
+    PWCHAR c = DesktopPath->Buffer;
+    USHORT wl = 0;
+    USHORT l = DesktopPath->Length;
+
+    /*
+     * Parse the desktop path string which can be in the form "WinSta\Desktop"
+     * or just "Desktop". In latter case WinSta0 will be used.
+     */
+
+    while(l > 0)
+    {
+      if(*c == L'\\')
+      {
+        wl = (ULONG_PTR)c - (ULONG_PTR)DesktopPath->Buffer;
+        break;
+      }
+      l -= sizeof(WCHAR);
+      c++;
+    }
+    
+    if(wl > 0)
+    {
+      WinSta.Length = wl;
+      WinSta.MaximumLength = wl + sizeof(WCHAR);
+      WinSta.Buffer = DesktopPath->Buffer;
+      
+      WinStaPresent = TRUE;
+      c++;
+    }
+    
+    Desktop.Length = DesktopPath->Length - wl;
+    if(wl > 0)
+    {
+      Desktop.Length -= sizeof(WCHAR);
+    }
+    if(Desktop.Length > 0)
+    {
+      Desktop.MaximumLength = Desktop.Length + sizeof(WCHAR);
+      Desktop.Buffer = ((wl > 0) ? c : DesktopPath->Buffer);
+      DesktopPresent = TRUE;
+    }
+  }
+  
+  if(!WinStaPresent)
+  {
+    /* search the process handle table for (inherited) window station
+       handles, use a more appropriate one than WinSta0 if possible. */
+    Status = ObFindHandleForObject(Process,
+                                   NULL,
+                                   ExWindowStationObjectType,
+                                   NULL,
+                                   (PHANDLE)hWinSta);
+    if(!NT_SUCCESS(Status))
+    {
+      /* we had no luck searching for opened handles, use WinSta0 now */
+      RtlInitUnicodeString(&WinSta, L"WinSta0");
+    }
+  }
+  
+  if(!DesktopPresent && hDesktop != NULL)
+  {
+    /* search the process handle table for (inherited) desktop
+       handles, use a more appropriate one than Default if possible. */
+    Status = ObFindHandleForObject(Process,
+                                   NULL,
+                                   ExDesktopObjectType,
+                                   NULL,
+                                   (PHANDLE)hDesktop);
+    if(!NT_SUCCESS(Status))
+    {
+      /* we had no luck searching for opened handles, use Desktop now */
+      RtlInitUnicodeString(&Desktop, L"Default");
+    }
+  }
+  
+  if(*hWinSta == NULL)
+  {
+    if(!IntGetFullWindowStationName(&FullName, &WinSta, NULL))
+    {
+      return STATUS_INSUFFICIENT_RESOURCES;
+    }
+
+    /* open the window station */
+    InitializeObjectAttributes(&ObjectAttributes,
+                               &FullName,
+                               OBJ_CASE_INSENSITIVE,
+                               NULL,
+                               NULL);
+
+    Status = ObOpenObjectByName(&ObjectAttributes,
+                                ExWindowStationObjectType,
+                                NULL,
+                                UserMode,
+                                0,
+                                NULL,
+                                (HANDLE*)hWinSta);
+
+    RtlFreeUnicodeString(&FullName);
+
+    if(!NT_SUCCESS(Status))
+    {
+      SetLastNtError(Status);
+      DPRINT("Failed to reference window station %wZ PID: %d!\n", &WinSta, PsGetCurrentProcessId());
+      return Status;
+    }
+  }
+  
+  if(hDesktop != NULL && *hDesktop == NULL)
+  {
+    if(!IntGetFullWindowStationName(&FullName, &WinSta, &Desktop))
+    {
+      NtClose(*hWinSta);
+      *hWinSta = NULL;
+      return STATUS_INSUFFICIENT_RESOURCES;
+    }
+    
+    /* open the desktop object */
+    InitializeObjectAttributes(&ObjectAttributes,
+                               &FullName,
+                               OBJ_CASE_INSENSITIVE,
+                               NULL,
+                               NULL);
+
+     Status = ObOpenObjectByName(&ObjectAttributes,
+                                 ExDesktopObjectType,
+                                 NULL,
+                                 UserMode,
+                                 0,
+                                 NULL,
+                                 (HANDLE*)hDesktop);
+
+    RtlFreeUnicodeString(&FullName);
+    
+    if(!NT_SUCCESS(Status))
+    {
+      *hDesktop = NULL;
+      NtClose(*hWinSta);
+      *hWinSta = NULL;
+      SetLastNtError(Status);
+      DPRINT("Failed to reference desktop %wZ PID: %d!\n", &Desktop, PsGetCurrentProcessId());
+      return Status;
+    }
+  }
+  
+  return STATUS_SUCCESS;
+}
+
 /*
  * IntValidateDesktopHandle
  *
@@ -322,17 +495,19 @@
   HDESK Desktop;
   CSRSS_API_REQUEST Request;
   CSRSS_API_REPLY Reply;
+  
+  DPRINT1("CreateDesktop: %wZ\n", lpszDesktopName);
 
   Status = IntValidateWindowStationHandle(
     hWindowStation,
     KernelMode,
-    0,
+    0, /* FIXME - WINSTA_CREATEDESKTOP */
     &WinStaObject);
 
   if (! NT_SUCCESS(Status))
     {
-      DPRINT1("Failed validation of window station handle (0x%X)\n", 
-        hWindowStation);
+      DPRINT1("Failed validation of window station handle (0x%X), cannot create desktop %wZ\n",
+        hWindowStation, lpszDesktopName);
       SetLastNtError(Status);
       return NULL;
     }
@@ -428,14 +603,28 @@
       return NULL;
     }
 
+  /*
+   * Create a handle for CSRSS and notify CSRSS
+   */
   Request.Type = CSRSS_CREATE_DESKTOP;
-  memcpy(Request.Data.CreateDesktopRequest.DesktopName, lpszDesktopName->Buffer,
-         lpszDesktopName->Length);
-  Request.Data.CreateDesktopRequest.DesktopName[lpszDesktopName->Length / sizeof(WCHAR)] = L'\0';
+  Status = CsrInsertObject((PVOID)DesktopObject,
+                           NULL,
+                           GENERIC_ALL,
+                           0,
+                           NULL,
+                           (HANDLE*)&Request.Data.CreateDesktopRequest.DesktopHandle);
+  if (! NT_SUCCESS(Status))
+  {
+    DPRINT1("Failed to create desktop handle for CSRSS\n");
+    ZwClose(Desktop);
+    SetLastNtError(Status);
+    return NULL;
+  }
 
   Status = CsrNotify(&Request, &Reply);
   if (! NT_SUCCESS(Status))
     {
+      CsrCloseHandle(Request.Data.CreateDesktopRequest.DesktopHandle);
       DPRINT1("Failed to notify CSRSS about new desktop\n");
       ZwClose(Desktop);
       SetLastNtError(Status);
@@ -485,15 +674,15 @@
     */
 
    Status = IntValidateWindowStationHandle(
-      PROCESS_WINDOW_STATION(),
+      PsGetCurrentProcess()->Win32WindowStation,
       KernelMode,
       0,
       &WinStaObject);
 
    if (!NT_SUCCESS(Status))
    {
-      DPRINT("Failed validation of window station handle (0x%X)\n",
-         PROCESS_WINDOW_STATION());
+      DPRINT1("Failed validation of window station handle (0x%X)\n",
+              PsGetCurrentProcess()->Win32WindowStation);
       SetLastNtError(Status);
       return 0;
    }
@@ -508,7 +697,7 @@
  
    ObDereferenceObject(WinStaObject);
 
-   DPRINT("Trying to open desktop station (%wZ)\n", &DesktopName);
+   DPRINT1("Trying to open desktop (%wZ)\n", &DesktopName);
 
    /* Initialize ObjectAttributes for the desktop object */
    InitializeObjectAttributes(
@@ -1026,13 +1215,6 @@
    }
 
    W32Thread = PsGetWin32Thread();
-   /* Check for setting the same desktop as before. */
-   if (DesktopObject == W32Thread->Desktop)
-   {
-      W32Thread->hDesktop = hDesktop;
-      ObDereferenceObject(DesktopObject);
-      return TRUE;
-   }
 
    /* FIXME: Should check here to see if the thread has any windows. */
 

reactos/subsys/win32k/ntuser
hook.c 1.9 -> 1.10
diff -u -r1.9 -r1.10
--- hook.c	28 Sep 2004 15:02:30 -0000	1.9
+++ hook.c	20 Nov 2004 16:46:06 -0000	1.10
@@ -16,7 +16,7 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/* $Id: hook.c,v 1.9 2004/09/28 15:02:30 weiden Exp $
+/* $Id: hook.c,v 1.10 2004/11/20 16:46:06 weiden Exp $
  *
  * COPYRIGHT:        See COPYING in the top level directory
  * PROJECT:          ReactOS kernel
@@ -289,7 +289,7 @@
   Result = IntCallHookProc(HookId, Code, wParam, lParam, Hook->Proc,
                            Hook->Ansi, &Hook->ModuleName);
 
-  Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
+  Status = IntValidateWindowStationHandle(PsGetCurrentProcess()->Win32WindowStation,
 				          KernelMode,
 				          0,
 				          &WinStaObj);
@@ -319,11 +319,11 @@
 
   if (NULL != GlobalHooks)
     {
-      Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
+      Status = IntValidateWindowStationHandle(PsGetCurrentProcess()->Win32WindowStation,
                                               KernelMode,
                                               0,
                                               &WinStaObj);
-  
+
       if(! NT_SUCCESS(Status))
         {
           DPRINT1("Invalid window station????\n");
@@ -367,11 +367,11 @@
   PWINSTATION_OBJECT WinStaObj;
   NTSTATUS Status;
 
-  Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
+  Status = IntValidateWindowStationHandle(PsGetCurrentProcess()->Win32WindowStation,
 				          KernelMode,
 				          0,
 				          &WinStaObj);
-  
+
   if(! NT_SUCCESS(Status))
     {
       SetLastNtError(Status);
@@ -509,7 +509,7 @@
       return NULL;
     }
   
-  Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
+  Status = IntValidateWindowStationHandle(PsGetCurrentProcess()->Win32WindowStation,
 				          KernelMode,
 				          0,
 				          &WinStaObj);
@@ -613,7 +613,7 @@
   PHOOK HookObj;
   NTSTATUS Status;
 
-  Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
+  Status = IntValidateWindowStationHandle(PsGetCurrentProcess()->Win32WindowStation,
 				          KernelMode,
 				          0,
 				          &WinStaObj);

reactos/subsys/win32k/ntuser
hotkey.c 1.10 -> 1.11
diff -u -r1.10 -r1.11
--- hotkey.c	25 May 2004 15:52:44 -0000	1.10
+++ hotkey.c	20 Nov 2004 16:46:06 -0000	1.11
@@ -16,7 +16,7 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/* $Id: hotkey.c,v 1.10 2004/05/25 15:52:44 navaraf Exp $
+/* $Id: hotkey.c,v 1.11 2004/11/20 16:46:06 weiden Exp $
  *
  * COPYRIGHT:        See COPYING in the top level directory
  * PROJECT:          ReactOS kernel
@@ -114,9 +114,8 @@
   PHOT_KEY_ITEM HotKeyItem;
   PWINSTATION_OBJECT WinStaObject = NULL;
   
-  if(Window->OwnerThread && Window->OwnerThread->ThreadsProcess &&
-     Window->OwnerThread->ThreadsProcess->Win32Process)
-    WinStaObject = Window->OwnerThread->ThreadsProcess->Win32Process->WindowStation;
+  if(Window->OwnerThread && Window->OwnerThread->ThreadsProcess)
+    WinStaObject = Window->OwnerThread->Tcb.Win32Thread->Desktop->WindowStation;
 
   if(!WinStaObject)
     return;
@@ -148,8 +147,8 @@
   PHOT_KEY_ITEM HotKeyItem;
   PWINSTATION_OBJECT WinStaObject = NULL;
   
-  if(Thread->ThreadsProcess && Thread->ThreadsProcess->Win32Process)
-    WinStaObject = Thread->ThreadsProcess->Win32Process->WindowStation;
+  if(Thread->Tcb.Win32Thread && Thread->Tcb.Win32Thread->Desktop)
+    WinStaObject = Thread->Tcb.Win32Thread->Desktop->WindowStation;
   
   if(!WinStaObject)
     return;
@@ -230,7 +229,7 @@
 
   
   if(HotKeyThread->ThreadsProcess && HotKeyThread->ThreadsProcess->Win32Process)
-    WinStaObject = HotKeyThread->ThreadsProcess->Win32Process->WindowStation;
+    WinStaObject = HotKeyThread->Tcb.Win32Thread->Desktop->WindowStation;
   
   if(!WinStaObject)
   {
@@ -285,7 +284,7 @@
   }
   
   if(Window->OwnerThread->ThreadsProcess && Window->OwnerThread->ThreadsProcess->Win32Process)
-    WinStaObject = Window->OwnerThread->ThreadsProcess->Win32Process->WindowStation;
+    WinStaObject = Window->OwnerThread->Tcb.Win32Thread->Desktop->WindowStation;
   
   if(!WinStaObject)
   {

reactos/subsys/win32k/ntuser
menu.c 1.55 -> 1.56
diff -u -r1.55 -r1.56
--- menu.c	13 May 2004 20:21:27 -0000	1.55
+++ menu.c	20 Nov 2004 16:46:06 -0000	1.56
@@ -16,7 +16,7 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/* $Id: menu.c,v 1.55 2004/05/13 20:21:27 navaraf Exp $
+/* $Id: menu.c,v 1.56 2004/11/20 16:46:06 weiden Exp $
  *
  * COPYRIGHT:        See COPYING in the top level directory
  * PROJECT:          ReactOS kernel
@@ -135,16 +135,15 @@
 IntGetMenuObject(HMENU hMenu)
 {
   PMENU_OBJECT MenuObject;
-  PW32PROCESS W32Process = PsGetWin32Process();
+  PW32THREAD W32Thread = PsGetWin32Thread();
   
-  if(!W32Process)
+  if(!W32Thread)
   {
     return NULL;
   }
   
-  NTSTATUS Status = ObmReferenceObjectByHandle(W32Process->
-                      WindowStation->HandleTable, hMenu, otMenu, 
-                      (PVOID*)&MenuObject);
+  NTSTATUS Status = ObmReferenceObjectByHandle(W32Thread->Desktop->WindowStation->HandleTable,
+                                               hMenu, otMenu, (PVOID*)&MenuObject);
   if (!NT_SUCCESS(Status))
   {
     return NULL;
@@ -225,6 +224,9 @@
 {
   if(MenuObject)
   {
+    PWINSTATION_OBJECT WindowStation;
+    NTSTATUS Status;
+    
     /* remove all menu items */
     IntLockMenuItems(MenuObject);
     IntDeleteMenuItems(MenuObject, bRecurse); /* do not destroy submenus */
@@ -232,14 +234,23 @@
     
     if(RemoveFromProcess)
     {
-      IntLockProcessMenus(MenuObject->W32Process);
+      IntLockProcessMenus(MenuObject->Process->Win32Process);
       RemoveEntryList(&MenuObject->ListEntry);
-      IntUnLockProcessMenus(MenuObject->W32Process);
+      IntUnLockProcessMenus(MenuObject->Process->Win32Process);
     }
     
-    ObmCloseHandle(MenuObject->W32Process->WindowStation->HandleTable, MenuObject->MenuInfo.Self);
-  
-    return TRUE;
+    Status = ObReferenceObjectByHandle(MenuObject->Process->Win32WindowStation,
+                                       0,
+                                       ExWindowStationObjectType,
+                                       KernelMode,
+                                       (PVOID*)&WindowStation,
+                                       NULL);
+    if(NT_SUCCESS(Status))
+    {
+      ObmCloseHandle(WindowStation->HandleTable, MenuObject->MenuInfo.Self);
+      ObDereferenceObject(WindowStation);
+      return TRUE;
+    }
   }
   return FALSE;
 }
@@ -248,10 +259,10 @@
 IntCreateMenu(PHANDLE Handle, BOOL IsMenuBar)
 {
   PMENU_OBJECT MenuObject;
-  PW32PROCESS Win32Process = PsGetWin32Process();
+  PW32THREAD Win32Thread = PsGetWin32Thread();
 
   MenuObject = (PMENU_OBJECT)ObmCreateObject(
-      Win32Process->WindowStation->HandleTable, Handle, 
+      Win32Thread->Desktop->WindowStation->HandleTable, Handle,
       otMenu, sizeof(MENU_OBJECT));
 
   if(!MenuObject)
@@ -260,7 +271,7 @@
     return NULL;
   }
   
-  MenuObject->W32Process = Win32Process;
+  MenuObject->Process = PsGetCurrentProcess();
   MenuObject->RtoL = FALSE; /* default */
   MenuObject->MenuInfo.cbSize = sizeof(MENUINFO); /* not used */
   MenuObject->MenuInfo.fMask = 0; /* not used */
@@ -284,9 +295,9 @@
   ExInitializeFastMutex(&MenuObject->MenuItemsLock);
 
   /* Insert menu item into process menu handle list */
-  IntLockProcessMenus(Win32Process);
-  InsertTailList(&Win32Process->MenuListHead, &MenuObject->ListEntry);
-  IntUnLockProcessMenus(Win32Process);
+  IntLockProcessMenus(PsGetWin32Process());
+  InsertTailList(&PsGetWin32Process()->MenuListHead, &MenuObject->ListEntry);
+  IntUnLockProcessMenus(PsGetWin32Process());
 
   return MenuObject;
 }
@@ -363,18 +374,17 @@
 {
   HANDLE Handle;
   PMENU_OBJECT MenuObject;
-  PW32PROCESS Process = PsGetWin32Process();
   
   if(!Source)
     return NULL;
     
   MenuObject = (PMENU_OBJECT)ObmCreateObject(
-    Process->WindowStation->HandleTable, &Handle, 
+    PsGetWin32Thread()->Desktop->WindowStation->HandleTable, &Handle,
     otMenu, sizeof(MENU_OBJECT));  
   if(!MenuObject)
     return NULL;
   
-  MenuObject->W32Process = Process;
+  MenuObject->Process = PsGetCurrentProcess();
   MenuObject->RtoL = Source->RtoL;
   MenuObject->MenuInfo.cbSize = sizeof(MENUINFO); /* not used */
   MenuObject->MenuInfo.fMask = Source->MenuInfo.fMask;
@@ -396,9 +406,9 @@
   ExInitializeFastMutex(&MenuObject->MenuItemsLock);
   
   /* Insert menu item into process menu handle list */
-  IntLockProcessMenus(Process);
-  InsertTailList(&Process->MenuListHead, &MenuObject->ListEntry);
-  IntUnLockProcessMenus(Process);
+  IntLockProcessMenus(PsGetWin32Process());
+  InsertTailList(&PsGetWin32Process()->MenuListHead, &MenuObject->ListEntry);
+  IntUnLockProcessMenus(PsGetWin32Process());
 
   IntCloneMenuItems(MenuObject, Source);
 
@@ -1328,7 +1338,7 @@
   PWINSTATION_OBJECT WinStaObject;
   HANDLE Handle;
 
-  NTSTATUS Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
+  NTSTATUS Status = IntValidateWindowStationHandle(PsGetCurrentProcess()->Win32WindowStation,
 				       KernelMode,
 				       0,
 				       &WinStaObject);
@@ -1387,7 +1397,7 @@
     SetLastWin32Error(ERROR_INVALID_MENU_HANDLE);
     return FALSE;
   }
-  if(MenuObject->W32Process != PsGetWin32Process())
+  if(MenuObject->Process != PsGetCurrentProcess())
   {
     IntReleaseMenuObject(MenuObject);
     SetLastWin32Error(ERROR_ACCESS_DENIED);

reactos/subsys/win32k/ntuser
message.c 1.74 -> 1.75
diff -u -r1.74 -r1.75
--- message.c	28 Sep 2004 15:02:30 -0000	1.74
+++ message.c	20 Nov 2004 16:46:06 -0000	1.75
@@ -16,7 +16,7 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/* $Id: message.c,v 1.74 2004/09/28 15:02:30 weiden Exp $
+/* $Id: message.c,v 1.75 2004/11/20 16:46:06 weiden Exp $
  *
  * COPYRIGHT:        See COPYING in the top level directory
  * PROJECT:          ReactOS kernel
@@ -1126,7 +1126,7 @@
           SetLastWin32Error(ERROR_INVALID_PARAMETER);
           return FALSE;
         }
-      CurInfo = IntGetSysCursorInfo(PsGetWin32Process()->WindowStation);
+      CurInfo = IntGetSysCursorInfo(PsGetWin32Thread()->Desktop->WindowStation);
       KernelModeMsg.pt.x = CurInfo->x;
       KernelModeMsg.pt.y = CurInfo->y;
       KeQueryTickCount(&LargeTickCount);

reactos/subsys/win32k/ntuser
metric.c 1.22 -> 1.23
diff -u -r1.22 -r1.23
--- metric.c	4 Oct 2004 19:23:31 -0000	1.22
+++ metric.c	20 Nov 2004 16:46:06 -0000	1.23
@@ -16,7 +16,7 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/* $Id: metric.c,v 1.22 2004/10/04 19:23:31 jc Exp $
+/* $Id: metric.c,v 1.23 2004/11/20 16:46:06 weiden Exp $
  *
  * COPYRIGHT:        See COPYING in the top level directory
  * PROJECT:          ReactOS kernel
@@ -68,7 +68,7 @@
     case SM_SWAPBUTTON:
     {
       PSYSTEM_CURSORINFO CurInfo;
-      Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
+      Status = IntValidateWindowStationHandle(PsGetCurrentProcess()->Win32WindowStation,
                                               KernelMode,
                                               0,
                                               &WinStaObject);

reactos/subsys/win32k/ntuser
misc.c 1.87 -> 1.88
diff -u -r1.87 -r1.88
--- misc.c	19 Oct 2004 08:25:25 -0000	1.87
+++ misc.c	20 Nov 2004 16:46:06 -0000	1.88
@@ -1,4 +1,4 @@
-/* $Id: misc.c,v 1.87 2004/10/19 08:25:25 gvg Exp $
+/* $Id: misc.c,v 1.88 2004/11/20 16:46:06 weiden Exp $
  *
  * COPYRIGHT:        See COPYING in the top level directory
  * PROJECT:          ReactOS kernel
@@ -215,7 +215,7 @@
       NTSTATUS Status;
       DWORD Result;
       
-      Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
+      Status = IntValidateWindowStationHandle(PsGetCurrentProcess()->Win32WindowStation,
                                               KernelMode,
                                               0,
                                               &WinStaObject);
@@ -265,7 +265,7 @@
       
       if(!Param)
         return (DWORD)FALSE;
-      Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
+      Status = IntValidateWindowStationHandle(PsGetCurrentProcess()->Win32WindowStation,
                                               KernelMode,
                                               0,
                                               &WinStaObject);
@@ -686,7 +686,7 @@
     {
       PSYSTEM_CURSORINFO CurInfo;
       
-      Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
+      Status = IntValidateWindowStationHandle(PsGetCurrentProcess()->Win32WindowStation,
                                               KernelMode,
                                               0,
                                               &WinStaObject);
@@ -1038,7 +1038,7 @@
   PWINSTATION_OBJECT WinStaObject;
   PSYSTEM_CURSORINFO CurInfo;
   
-  Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
+  Status = IntValidateWindowStationHandle(PsGetCurrentProcess()->Win32WindowStation,
                                           KernelMode,
                                           0,
                                           &WinStaObject);

reactos/subsys/win32k/ntuser
monitor.c 1.1 -> 1.2
diff -u -r1.1 -r1.2
--- monitor.c	16 Nov 2004 17:25:47 -0000	1.1
+++ monitor.c	20 Nov 2004 16:46:06 -0000	1.2
@@ -91,7 +91,7 @@
   HANDLE Handle;
   PMONITOR_OBJECT Monitor;
 
-  Monitor = ObmCreateObject(PsGetWin32Process()->WindowStation->HandleTable, &Handle, otMonitor, sizeof (MONITOR_OBJECT));
+  Monitor = ObmCreateObject(PsGetWin32Thread()->Desktop->WindowStation->HandleTable, &Handle, otMonitor, sizeof (MONITOR_OBJECT));
   if (Monitor == NULL)
     {
       return NULL;
@@ -141,7 +141,7 @@
   PMONITOR_OBJECT Monitor;
   NTSTATUS Status;
 
-  Status = ObmReferenceObjectByHandle(PsGetWin32Process()->WindowStation->HandleTable, hMonitor, otMonitor, (PVOID *)&Monitor);
+  Status = ObmReferenceObjectByHandle(PsGetWin32Thread()->Desktop->WindowStation->HandleTable, hMonitor, otMonitor, (PVOID *)&Monitor);
   if (!NT_SUCCESS(Status) || Monitor == NULL)
     {
       /* FIXME: SetLastNtError( status ); ? */

reactos/subsys/win32k/ntuser
msgqueue.c 1.107 -> 1.108
diff -u -r1.107 -r1.108
--- msgqueue.c	28 Sep 2004 15:02:30 -0000	1.107
+++ msgqueue.c	20 Nov 2004 16:46:06 -0000	1.108
@@ -16,7 +16,7 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/* $Id: msgqueue.c,v 1.107 2004/09/28 15:02:30 weiden Exp $
+/* $Id: msgqueue.c,v 1.108 2004/11/20 16:46:06 weiden Exp $
  *
  * COPYRIGHT:        See COPYING in the top level directory
  * PROJECT:          ReactOS kernel
@@ -175,18 +175,15 @@
 {
   PWINSTATION_OBJECT WinStaObject;
   PSYSTEM_CURSORINFO CurInfo;
-  NTSTATUS Status;
   LONG dX, dY;
   BOOL Res;
   
-  Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
-                                          KernelMode,
-                                          0,
-                                          &WinStaObject);
-  if (!NT_SUCCESS(Status))
+  if (PsGetWin32Thread()->Desktop == NULL)
   {
     return FALSE;
   }
+  WinStaObject = PsGetWin32Thread()->Desktop->WindowStation;
+  
   CurInfo = IntGetSysCursorInfo(WinStaObject);
   Res = (Msg->hwnd == (HWND)CurInfo->LastClkWnd) && 
         ((Msg->time - CurInfo->LastBtnDown) < CurInfo->DblClickSpeed);
@@ -220,7 +217,6 @@
     }
   }
   
-  ObDereferenceObject(WinStaObject);
   return Res;
 }
 
@@ -616,7 +612,7 @@
 {
   PWINDOW_OBJECT Window;
   PW32THREAD Win32Thread;
-  PW32PROCESS Win32Process;
+  PWINSTATION_OBJECT WinSta;
   MSG Mesg;
   NTSTATUS Status;
 
@@ -634,14 +630,8 @@
       return;
     }
 
-  Win32Process = ((PETHREAD)Thread)->ThreadsProcess->Win32Process;
-  if (Win32Process == NULL || Win32Process->WindowStation == NULL)
-    {
-      ObDereferenceObject ((PETHREAD)Thread);
-      return;
-    }
-
-  Status = ObmReferenceObjectByHandle(Win32Process->WindowStation->HandleTable,
+  WinSta = Win32Thread->Desktop->WindowStation;
+  Status = ObmReferenceObjectByHandle(WinSta->HandleTable,
                                       hWnd, otWindow, (PVOID*)&Window);
   if (!NT_SUCCESS(Status))
     {

reactos/subsys/win32k/ntuser
useratom.c 1.2 -> 1.3
diff -u -r1.2 -r1.3
--- useratom.c	10 May 2004 17:07:18 -0000	1.2
+++ useratom.c	20 Nov 2004 16:46:06 -0000	1.3
@@ -35,17 +35,15 @@
    PWINSTATION_OBJECT WinStaObject;
    NTSTATUS Status;
    RTL_ATOM Atom;
-
-   Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
-      KernelMode, 0, &WinStaObject);
-   if (!NT_SUCCESS(Status))
+   
+   if (PsGetWin32Thread()->Desktop == NULL)
    {
       SetLastNtError(Status);      
       return (RTL_ATOM)0;
    }
+   WinStaObject = PsGetWin32Thread()->Desktop->WindowStation;
    Status = RtlAddAtomToAtomTable(WinStaObject->AtomTable,
       AtomName, &Atom);
-   ObDereferenceObject(WinStaObject);
    if (!NT_SUCCESS(Status))
    {
       SetLastNtError(Status);      
@@ -61,18 +59,16 @@
    NTSTATUS Status;
    ULONG Size = nSize;
 
-   Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
-      KernelMode, 0, &WinStaObject);
-   if (!NT_SUCCESS(Status))
+   if (PsGetWin32Thread()->Desktop == NULL)
    {
       SetLastNtError(Status);      
       return 0;
    }
+   WinStaObject = PsGetWin32Thread()->Desktop->WindowStation;
    Status = RtlQueryAtomInAtomTable(WinStaObject->AtomTable,
       nAtom, NULL, NULL, lpBuffer, &Size);
    if (Size < nSize)
       *(lpBuffer + Size) = 0;
-   ObDereferenceObject(WinStaObject);
    if (!NT_SUCCESS(Status))
    {
       SetLastNtError(Status);      

reactos/subsys/win32k/ntuser
window.c 1.248 -> 1.249
diff -u -r1.248 -r1.249
--- window.c	15 Nov 2004 23:10:42 -0000	1.248
+++ window.c	20 Nov 2004 16:46:06 -0000	1.249
@@ -16,7 +16,7 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/* $Id: window.c,v 1.248 2004/11/15 23:10:42 gvg Exp $
+/* $Id: window.c,v 1.249 2004/11/20 16:46:06 weiden Exp $
  *
  * COPYRIGHT:        See COPYING in the top level directory
  * PROJECT:          ReactOS kernel
@@ -106,18 +106,21 @@
  */
 
 PWINDOW_OBJECT FASTCALL
-IntGetProcessWindowObject(PW32PROCESS ProcessData, HWND hWnd)
+IntGetProcessWindowObject(PW32THREAD Thread, HWND hWnd)
 {
    PWINDOW_OBJECT WindowObject;
    NTSTATUS Status;
-
-   Status = ObmReferenceObjectByHandle(ProcessData->WindowStation->HandleTable,
-      hWnd, otWindow, (PVOID*)&WindowObject);
-   if (!NT_SUCCESS(Status))
+   
+   if(Thread->Desktop != NULL)
    {
-      return NULL;
+     Status = ObmReferenceObjectByHandle(Thread->Desktop->WindowStation->HandleTable,
+                                         hWnd, otWindow, (PVOID*)&WindowObject);
+     if (NT_SUCCESS(Status))
+     {
+        return WindowObject;
+     }
    }
-   return WindowObject;
+   return NULL;
 }
 
 
@@ -328,13 +331,13 @@
     }
 
   /* reset shell window handles */
-  if(ProcessData->WindowStation)
+  if(ThreadData->Desktop)
   {
-    if (Window->Self == ProcessData->WindowStation->ShellWindow)
-      ProcessData->WindowStation->ShellWindow = NULL;
+    if (Window->Self == ThreadData->Desktop->WindowStation->ShellWindow)
+      ThreadData->Desktop->WindowStation->ShellWindow = NULL;
 
-    if (Window->Self == ProcessData->WindowStation->ShellListView)
-      ProcessData->WindowStation->ShellListView = NULL;
+    if (Window->Self == ThreadData->Desktop->WindowStation->ShellListView)
+      ThreadData->Desktop->WindowStation->ShellListView = NULL;
   }
   
   /* Unregister hot keys */
@@ -378,7 +381,7 @@
   IntUnlinkWindow(Window);
   
   IntReferenceWindowObject(Window);
-  ObmCloseHandle(ProcessData->WindowStation->HandleTable, Window->Self);
+  ObmCloseHandle(ThreadData->Desktop->WindowStation->HandleTable, Window->Self);
   
   IntDestroyScrollBars(Window);
   
@@ -613,15 +616,15 @@
 IntGetSystemMenu(PWINDOW_OBJECT WindowObject, BOOL bRevert, BOOL RetMenu)
 {
   PMENU_OBJECT MenuObject, NewMenuObject, SysMenuObject, ret = NULL;
-  PW32PROCESS W32Process;
+  PW32THREAD W32Thread;
   HMENU NewMenu, SysMenu;
   ROSMENUITEMINFO ItemInfo;
 
   if(bRevert)
   {
-    W32Process = PsGetWin32Process();
+    W32Thread = PsGetWin32Thread();
     
-    if(!W32Process->WindowStation)
+    if(!W32Thread->Desktop)
       return NULL;
       
     if(WindowObject->SystemMenu)
@@ -635,10 +638,10 @@
       }
     }
       
-    if(W32Process->WindowStation->SystemMenuTemplate)
+    if(W32Thread->Desktop->WindowStation->SystemMenuTemplate)
     {
       /* clone system menu */
-      MenuObject = IntGetMenuObject(W32Process->WindowStation->SystemMenuTemplate);
+      MenuObject = IntGetMenuObject(W32Thread->Desktop->WindowStation->SystemMenuTemplate);
       if(!MenuObject)
         return NULL;
 
@@ -1415,7 +1418,7 @@
   BOOL MenuChanged;
   BOOL ClassFound;
   PWSTR ClassNameString;
-  
+  DPRINT1("PsGetWin32Thread()->Desktop == 0x%x\n", PsGetWin32Thread()->Desktop);
   ParentWindowHandle = PsGetWin32Thread()->Desktop->DesktopWindow;
   OwnerWindowHandle = NULL;
 
@@ -1487,27 +1490,22 @@
   }
 
   /* Check the window station. */
-  DPRINT("IoGetCurrentProcess() %X\n", IoGetCurrentProcess());
-  DPRINT("PROCESS_WINDOW_STATION %X\n", PROCESS_WINDOW_STATION());
-  Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
-					  KernelMode,
-					  0,
-					  &WinStaObject);
-  if (!NT_SUCCESS(Status))
+  if (PsGetWin32Thread()->Desktop == NULL)
     {
       ClassDereferenceObject(ClassObject);
       if (NULL != ParentWindow)
         {
           IntReleaseWindowObject(ParentWindow);
         }
-      DPRINT("Validation of window station handle (0x%X) failed\n",
-	     PROCESS_WINDOW_STATION());
+      DPRINT("Thread is not attached to a desktop! Cannot create window!\n");
       return (HWND)0;
     }
+  WinStaObject = PsGetWin32Thread()->Desktop->WindowStation;
+  ObReferenceObjectByPointer(WinStaObject, KernelMode, ExWindowStationObjectType, 0);
 
   /* Create the window object. */
   WindowObject = (PWINDOW_OBJECT)
-    ObmCreateObject(PsGetWin32Process()->WindowStation->HandleTable, &Handle,
+    ObmCreateObject(PsGetWin32Thread()->Desktop->WindowStation->HandleTable, &Handle,
         otWindow, sizeof(WINDOW_OBJECT) + ClassObject->cbWndExtra
         );
 
@@ -2872,7 +2870,7 @@
   PWINSTATION_OBJECT WinStaObject;
   HWND Ret;
 
-  NTSTATUS Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
+  NTSTATUS Status = IntValidateWindowStationHandle(PsGetCurrentProcess()->Win32WindowStation,
 				       KernelMode,
 				       0,
 				       &WinStaObject);
@@ -2904,7 +2902,7 @@
 {
   PWINSTATION_OBJECT WinStaObject;
 
-  NTSTATUS Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
+  NTSTATUS Status = IntValidateWindowStationHandle(PsGetCurrentProcess()->Win32WindowStation,
 				       KernelMode,
 				       0,
 				       &WinStaObject);
@@ -3255,7 +3253,6 @@
 NtUserSetWindowLong(HWND hWnd, DWORD Index, LONG NewValue, BOOL Ansi)
 {
    PWINDOW_OBJECT WindowObject, Parent;
-   PW32PROCESS Process;
    PWINSTATION_OBJECT WindowStation;
    LONG OldValue;
    STYLESTRUCT Style;
@@ -3296,8 +3293,7 @@
             /*
              * Remove extended window style bit WS_EX_TOPMOST for shell windows.
              */
-            Process = WindowObject->OwnerThread->ThreadsProcess->Win32Process;
-            WindowStation = Process->WindowStation;
+            WindowStation = WindowObject->OwnerThread->Tcb.Win32Thread->Desktop->WindowStation;
             if(WindowStation)
             {
               if (hWnd == WindowStation->ShellWindow || hWnd == WindowStation->ShellListView)

reactos/subsys/win32k/ntuser
winpos.c 1.122 -> 1.123
diff -u -r1.122 -r1.123
--- winpos.c	15 Nov 2004 23:10:42 -0000	1.122
+++ winpos.c	20 Nov 2004 16:46:06 -0000	1.123
@@ -16,7 +16,7 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/* $Id: winpos.c,v 1.122 2004/11/15 23:10:42 gvg Exp $
+/* $Id: winpos.c,v 1.123 2004/11/20 16:46:06 weiden Exp $
  *
  * COPYRIGHT:        See COPYING in the top level directory
  * PROJECT:          ReactOS kernel
@@ -1193,7 +1193,7 @@
 //  HRGN VisibleRgn;
 
   Status = 
-    ObmReferenceObjectByHandle(PsGetWin32Process()->WindowStation->HandleTable,
+    ObmReferenceObjectByHandle(PsGetWin32Thread()->Desktop->WindowStation->HandleTable,
 			       Wnd,
 			       otWindow,
 			       (PVOID*)&Window);

reactos/subsys/win32k/ntuser
winsta.c 1.65 -> 1.66
diff -u -r1.65 -r1.66
--- winsta.c	20 Aug 2004 22:38:49 -0000	1.65
+++ winsta.c	20 Nov 2004 16:46:06 -0000	1.66
@@ -16,7 +16,7 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  *
- *  $Id: winsta.c,v 1.65 2004/08/20 22:38:49 gvg Exp $
+ *  $Id: winsta.c,v 1.66 2004/11/20 16:46:06 weiden Exp $
  *
  *  COPYRIGHT:        See COPYING in the top level directory
  *  PROJECT:          ReactOS kernel
@@ -85,7 +85,7 @@
 /*
  * IntGetFullWindowStationName
  *
- * Get a full desktop object name from a name specified in 
+ * Get a full window station object name from a name specified in
  * NtUserCreateWindowStation, NtUserOpenWindowStation, NtUserCreateDesktop
  * or NtUserOpenDesktop.
  *
@@ -478,7 +478,7 @@
 
    Status = ObOpenObjectByName(
       &ObjectAttributes,
-      ExDesktopObjectType,
+      ExWindowStationObjectType,
       NULL,
       UserMode,
       dwDesiredAccess,
@@ -783,7 +783,48 @@
 HWINSTA STDCALL
 NtUserGetProcessWindowStation(VOID)
 {
-   return PROCESS_WINDOW_STATION();
+   if(PsGetCurrentProcess() != CsrProcess)
+   {
+     return PsGetCurrentProcess()->Win32WindowStation;
+   }
+   else
+   {
+     /* FIXME - get the pointer to the window station by querying the parent of
+                the desktop of the calling thread (which is a window station),
+                then use ObFindHandleForObject() to find a suitable handle */
+     DPRINT1("CSRSS called NtUserGetProcessWindowStation()!!! returned NULL!\n");
+     return NULL;
+   }
+}
+
+PWINSTATION_OBJECT FASTCALL
+IntGetWinStaObj(VOID)
+{
+  PWINSTATION_OBJECT WinStaObj;
+  
+  /*
+   * just a temporary hack, this will be gone soon
+   */
+  
+  if(PsGetWin32Thread()->Desktop != NULL)
+  {
+    WinStaObj = PsGetWin32Thread()->Desktop->WindowStation;
+    ObReferenceObjectByPointer(WinStaObj, KernelMode, ExWindowStationObjectType, 0);
+  }
+  else if(PsGetCurrentProcess() != CsrProcess)
+  {
+    NTSTATUS Status = IntValidateWindowStationHandle(PsGetCurrentProcess()->Win32WindowStation,
+                                                     KernelMode,
+                                                     0,
+                                                     &WinStaObj);
+   if(!NT_SUCCESS(Status))
+   {
+     SetLastNtError(Status);
+     return NULL;
+   }
+  }
+  
+  return WinStaObj;
 }
 
 /*
@@ -805,42 +846,43 @@
 BOOL STDCALL
 NtUserSetProcessWindowStation(HWINSTA hWindowStation)
 {
-   PWINSTATION_OBJECT Object;
-   PW32PROCESS Win32Process;
+   HANDLE hOld;
+   PWINSTATION_OBJECT NewWinSta;
    NTSTATUS Status;
 
-   DPRINT("About to set process window station with handle (0x%X)\n", 
+   DPRINT("About to set process window station with handle (0x%X)\n",
       hWindowStation);
 
+   if(PsGetCurrentProcess() == CsrProcess)
+   {
+     DPRINT1("CSRSS is not allowed to change it's window station!!!\n");
+     SetLastWin32Error(ERROR_ACCESS_DENIED);
+     return FALSE;
+   }
+
    Status = IntValidateWindowStationHandle(
       hWindowStation,
       KernelMode,
       0,
-      &Object);
+      &NewWinSta);
 
    if (!NT_SUCCESS(Status)) 
    {
       DPRINT("Validation of window station handle (0x%X) failed\n", 
          hWindowStation);
+      SetLastNtError(Status);
       return FALSE;
    }
+   
+   /*
+    * FIXME - don't allow changing the window station if there are threads that are attached to desktops and own gui objects
+    */
 
-   Win32Process = PsGetWin32Process();
-   if (Win32Process == NULL)
-   {
-      ObDereferenceObject(Object);
-   }
-   else
-   {
-      if (Win32Process->WindowStation != NULL)
-         ObDereferenceObject(Win32Process->WindowStation);
-      Win32Process->WindowStation = Object;
-   }
+   /* FIXME - dereference the old window station, etc... */
+   hOld = InterlockedExchangePointer(&PsGetCurrentProcess()->Win32WindowStation, hWindowStation);
 
-   SET_PROCESS_WINDOW_STATION(hWindowStation);
-  
-   DPRINT("IoGetCurrentProcess()->Win32WindowStation 0x%X\n",
-      IoGetCurrentProcess()->Win32WindowStation);
+   DPRINT("PsGetCurrentProcess()->Win32WindowStation 0x%X\n",
+      PsGetCurrentProcess()->Win32WindowStation);
 
    return TRUE;
 }

reactos/subsys/win32k/objects
cursoricon.c 1.64 -> 1.65
diff -u -r1.64 -r1.65
--- cursoricon.c	7 Aug 2004 00:25:10 -0000	1.64
+++ cursoricon.c	20 Nov 2004 16:46:06 -0000	1.65
@@ -16,7 +16,7 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/* $Id: cursoricon.c,v 1.64 2004/08/07 00:25:10 navaraf Exp $ */
+/* $Id: cursoricon.c,v 1.65 2004/11/20 16:46:06 weiden Exp $ */
 #include <w32k.h>
 
 PCURICON_OBJECT FASTCALL
@@ -371,20 +371,20 @@
   PCURICON_OBJECT Current;
   PLIST_ENTRY CurrentEntry, NextEntry;
   
-  if(!(WinStaObject = Win32Process->WindowStation))
-    return;
-  
-  IntLockProcessCursorIcons(Win32Process);
-  CurrentEntry = Win32Process->CursorIconListHead.Flink;
-  while(CurrentEntry != &Win32Process->CursorIconListHead)
+  WinStaObject = IntGetWinStaObj();
+  if(WinStaObject != NULL)
   {
-    NextEntry = CurrentEntry->Flink;
-    Current = CONTAINING_RECORD(CurrentEntry, CURICON_OBJECT, ListEntry);
-    RemoveEntryList(&Current->ListEntry);
-    IntDestroyCurIconObject(WinStaObject, Current->Self, FALSE);
-    CurrentEntry = NextEntry;
+    CurrentEntry = Win32Process->CursorIconListHead.Flink;
+    while(CurrentEntry != &Win32Process->CursorIconListHead)
+    {
+      NextEntry = CurrentEntry->Flink;
+      Current = CONTAINING_RECORD(CurrentEntry, CURICON_OBJECT, ListEntry);
+      RemoveEntryList(&Current->ListEntry);
+      IntDestroyCurIconObject(WinStaObject, Current->Self, FALSE);
+      CurrentEntry = NextEntry;
+    }
+    ObDereferenceObject(WinStaObject);
   }
-  IntUnLockProcessCursorIcons(Win32Process);
 }
 
 /*
@@ -400,14 +400,9 @@
   NTSTATUS Status;
   HANDLE Ret;
   
-  Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
-				                               KernelMode,
-				                               0,
-				                               &WinStaObject);
-  
-  if(!NT_SUCCESS(Status))
+  WinStaObject = IntGetWinStaObj();
+  if(WinStaObject == NULL)
   {
-    SetLastNtError(Status);
     return (HANDLE)0;
   }
   
@@ -475,14 +470,9 @@
   NTSTATUS Status;
   BOOL Ret = FALSE;
   
-  Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
-				                               KernelMode,
-				                               0,
-				                               &WinStaObject);
-  
-  if(!NT_SUCCESS(Status))
+  WinStaObject = IntGetWinStaObj();
+  if(WinStaObject == NULL)
   {
-    SetLastNtError(Status);
     return FALSE;
   }
   
@@ -537,14 +527,9 @@
   BOOL Ret = FALSE;
   SIZE SafeSize;
   
-  Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
-				                               KernelMode,
-				                               0,
-				                               &WinStaObject);
-  
-  if(!NT_SUCCESS(Status))
+  WinStaObject = IntGetWinStaObj();
+  if(WinStaObject == NULL)
   {
-    SetLastNtError(Status);
     return FALSE;
   }
   
@@ -629,14 +614,9 @@
     return FALSE;
   }
   
-  Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
-				                               KernelMode,
-				                               0,
-				                               &WinStaObject);
-  
-  if(!NT_SUCCESS(Status))
+  WinStaObject = IntGetWinStaObj();
+  if(WinStaObject == NULL)
   {
-    SetLastNtError(Status);
     return FALSE;
   }
   
@@ -676,15 +656,9 @@
   RECT Rect;
   PWINDOW_OBJECT DesktopWindow = NULL;
 
-  NTSTATUS Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
-				       KernelMode,
-				       0,
-				       &WinStaObject);
-  if (!NT_SUCCESS(Status))
+  WinStaObject = IntGetWinStaObj();
+  if (WinStaObject == NULL)
   {
-    DPRINT1("Validation of window station handle (0x%X) failed\n",
-      PROCESS_WINDOW_STATION());
-    SetLastNtError(Status);
     return FALSE;
   }
 
@@ -739,16 +713,10 @@
   DWORD Unknown)
 {
   PWINSTATION_OBJECT WinStaObject;
-  NTSTATUS Status;
   
-  Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
-				                               KernelMode,
-				                               0,
-				                               &WinStaObject);
-  
-  if(!NT_SUCCESS(Status))
+  WinStaObject = IntGetWinStaObj();
+  if(WinStaObject == NULL)
   {
-    SetLastNtError(Status);
     return FALSE;
   }
   
@@ -777,17 +745,11 @@
 {
   PCURICON_OBJECT CurIconObject;
   PWINSTATION_OBJECT WinStaObject;
-  NTSTATUS Status;
   HANDLE Ret = (HANDLE)0;
   
-  Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
-				                               KernelMode,
-				                               0,
-				                               &WinStaObject);
-  
-  if(!NT_SUCCESS(Status))
+  WinStaObject = IntGetWinStaObj();
+  if(WinStaObject == NULL)
   {
-    SetLastNtError(Status);
     return Ret;
   }
   
@@ -824,11 +786,8 @@
   if(!lpRect)
     return FALSE;
 
-  Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
-	       KernelMode,
-	       0,
-	       &WinStaObject);
-  if (!NT_SUCCESS(Status))
+  WinStaObject = IntGetWinStaObj();
+  if (WinStaObject == NULL)
   {
     DPRINT("Validation of window station handle (0x%X) failed\n",
       PROCESS_WINDOW_STATION());
@@ -877,15 +836,10 @@
   PCURICON_OBJECT CurIconObject;
   HICON OldCursor = (HCURSOR)0;
   PWINSTATION_OBJECT WinStaObject;
-  NTSTATUS Status;
   
-  Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
-				                               KernelMode,
-				                               0,
-				                               &WinStaObject);
-  if(!NT_SUCCESS(Status))
+  WinStaObject = IntGetWinStaObj();
+  if(WinStaObject == NULL)
   {
-    SetLastNtError(Status);
     return (HCURSOR)0;
   }
   
@@ -918,14 +872,9 @@
   NTSTATUS Status;
   BOOL Ret = FALSE;
   
-  Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
-				                               KernelMode,
-				                               0,
-				                               &WinStaObject);
-  
-  if(!NT_SUCCESS(Status))
+  WinStaObject = IntGetWinStaObj();
+  if(WinStaObject == NULL)
   {
-    SetLastNtError(Status);
     return FALSE;
   }
   
@@ -992,14 +941,9 @@
   POINT SafeHotspot;
   BOOL Ret = FALSE;
   
-  Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
-				                               KernelMode,
-				                               0,
-				                               &WinStaObject);
-  
-  if(!NT_SUCCESS(Status))
+  WinStaObject = IntGetWinStaObj();
+  if(WinStaObject == NULL)
   {
-    SetLastNtError(Status);
     return FALSE;
   }
   
@@ -1091,7 +1035,6 @@
 {
   PCURICON_OBJECT CurIconObject;
   PWINSTATION_OBJECT WinStaObject;
-  NTSTATUS Status;
   HBITMAP hbmMask, hbmColor;
   BITMAP bmpMask, bmpColor;
   BOOL DoFlickerFree;
@@ -1105,14 +1048,9 @@
   INT nStretchMode;
   #endif
   
-  Status = IntValidateWindowStationHandle(PROCESS_WINDOW_STATION(),
-				                               KernelMode,
-				                               0,
-				                               &WinStaObject);
-  
-  if(!NT_SUCCESS(Status))
+  WinStaObject = IntGetWinStaObj();
+  if(WinStaObject == NULL)
   {
-    SetLastNtError(Status);
     return FALSE;
   }
   
CVSspam 0.2.8