-refcount reworking (simplification & improvement)
-make functions deal with pointers, not handles
-misc formatting
Modified: trunk/reactos/subsys/win32k/eng/window.c
Modified: trunk/reactos/subsys/win32k/include/userfuncs.h
Modified: trunk/reactos/subsys/win32k/include/window.h
Modified: trunk/reactos/subsys/win32k/ntuser/class.c
Modified: trunk/reactos/subsys/win32k/ntuser/desktop.c
Modified: trunk/reactos/subsys/win32k/ntuser/focus.c
Modified: trunk/reactos/subsys/win32k/ntuser/message.c
Modified: trunk/reactos/subsys/win32k/ntuser/misc.c
Modified: trunk/reactos/subsys/win32k/ntuser/msgqueue.c
Modified: trunk/reactos/subsys/win32k/ntuser/object.c
Modified: trunk/reactos/subsys/win32k/ntuser/painting.c
Modified: trunk/reactos/subsys/win32k/ntuser/timer.c
Modified: trunk/reactos/subsys/win32k/ntuser/windc.c
Modified: trunk/reactos/subsys/win32k/ntuser/window.c
Modified: trunk/reactos/subsys/win32k/ntuser/winpos.c
Modified: trunk/reactos/subsys/win32k/objects/color.c

Modified: trunk/reactos/subsys/win32k/eng/window.c
--- trunk/reactos/subsys/win32k/eng/window.c	2005-09-11 14:38:57 UTC (rev 17801)
+++ trunk/reactos/subsys/win32k/eng/window.c	2005-09-11 14:48:32 UTC (rev 17802)
@@ -287,7 +287,7 @@
   }
 
   /* Get window object */
-  Window = IntGetWindowObject(WndObjInt->Hwnd);
+  Window = UserGetWindowObject(WndObjInt->Hwnd);
   if (Window == NULL)
     {
       DPRINT1("Warning: Couldnt get window object for WndObjInt->Hwnd!!!\n");
@@ -297,7 +297,6 @@
     {
       /* Remove object from window */
       RemoveEntryList(&WndObjInt->ListEntry);
-      IntReleaseWindowObject(Window);
     }
 
   if (!calledFromUser){

Modified: trunk/reactos/subsys/win32k/include/userfuncs.h
--- trunk/reactos/subsys/win32k/include/userfuncs.h	2005-09-11 14:38:57 UTC (rev 17801)
+++ trunk/reactos/subsys/win32k/include/userfuncs.h	2005-09-11 14:48:32 UTC (rev 17802)
@@ -2,15 +2,57 @@
 #define _WIN32K_USERFUNCS_H
 
 
-#define ASSERT_REFS_CO(obj) \
+
+
+
+PMENU_OBJECT FASTCALL UserGetMenuObject(HMENU hMenu);
+
+
+#if 0
+#define ObmDereferenceObject(_obj_) \
 { \
-   LONG ref = USER_BODY_TO_HEADER(obj)->RefCount;\
+   DPRINT1("obj 0x%x dereffed to %i refs\n",_obj_, USER_BODY_TO_HEADER(_obj_)->RefCount-1); \
+   ObmDereferenceObject2(_obj_); \
+}
+#endif
+
+
+
+
+
+
+
+#define ASSERT_REFS_CO(_obj_) \
+{ \
+   LONG ref = USER_BODY_TO_HEADER(_obj_)->RefCount;\
    if (!(ref >= 1)){ \
-      DPRINT1("obj 0x%x, refs %i\n", obj, ref); \
+      DPRINT1("ASSERT: obj 0x%x, refs %i\n", _obj_, ref); \
       ASSERT(FALSE); \
    } \
 }
 
+#if 0
+#define ASSERT_REFS_CO(_obj_) \
+{ \
+   PSINGLE_LIST_ENTRY e; \
+   BOOL gotit=FALSE; \
+   LONG ref = USER_BODY_TO_HEADER(_obj_)->RefCount;\
+   if (!(ref >= 1)){ \
+      DPRINT1("obj 0x%x, refs %i\n", _obj_, ref); \
+      ASSERT(FALSE); \
+   } \
+   \
+   e = PsGetWin32Thread()->ReferencesList.Next; \
+   while (e) \
+   { \
+      PUSER_REFERENCE_ENTRY ref = CONTAINING_RECORD(e, USER_REFERENCE_ENTRY, Entry); \
+      if (ref->obj == _obj_){ gotit=TRUE; break; } \
+      e = e->Next; \
+   } \
+   ASSERT(gotit); \
+}
+#endif
+
 #define DUMP_REFS(obj) DPRINT1("obj 0x%x, refs %i\n",obj, USER_BODY_TO_HEADER(obj)->RefCount)
 
 
@@ -19,11 +61,6 @@
 VOID FASTCALL ObmReferenceObject(PVOID obj);
 BOOL FASTCALL ObmDereferenceObject(PVOID obj);
 
-#define IntReferenceWindowObject(o) ObmReferenceObject(o)
-
-#define UserDerefObject(o) ObmReferenceObject(o)
-
-VOID FASTCALL IntReleaseWindowObject(PWINDOW_OBJECT Window);
 PWINDOW_OBJECT FASTCALL IntGetWindowObject(HWND hWnd);
 PVOID FASTCALL
 ObmCreateObject(PUSER_HANDLE_TABLE ht, HANDLE* h,USER_OBJECT_TYPE type , ULONG size);
@@ -31,8 +68,8 @@
 BOOL FASTCALL 
 ObmDeleteObject(HANDLE h, USER_OBJECT_TYPE type );
 
-//#define UserRefObjectCo(o) ObmReferenceObject(o)
-//#define UserDerefObjectCo(o) ObmDereferenceObject(o)
+#define UserRefObject(o) ObmReferenceObject(o)
+#define UserDerefObject(o) ObmDereferenceObject(o)
 BOOL FASTCALL ObmCreateHandleTable();
 
 

Modified: trunk/reactos/subsys/win32k/include/window.h
--- trunk/reactos/subsys/win32k/include/window.h	2005-09-11 14:38:57 UTC (rev 17801)
+++ trunk/reactos/subsys/win32k/include/window.h	2005-09-11 14:48:32 UTC (rev 17802)
@@ -157,7 +157,7 @@
 IntIsWindowVisible (PWINDOW_OBJECT Window);
 
 BOOL FASTCALL
-IntIsChildWindow (HWND Parent, HWND Child);
+IntIsChildWindow (PWINDOW_OBJECT Parent, PWINDOW_OBJECT Child);
 
 VOID FASTCALL
 IntUnlinkWindow(PWINDOW_OBJECT Wnd);
@@ -174,8 +174,6 @@
 PWINDOW_OBJECT FASTCALL
 IntGetOwner(PWINDOW_OBJECT Wnd);
 
-PWINDOW_OBJECT FASTCALL
-IntGetParentObject(PWINDOW_OBJECT Wnd);
 
 INT FASTCALL
 IntGetWindowRgn(PWINDOW_OBJECT Window, HRGN hRgn);

Modified: trunk/reactos/subsys/win32k/ntuser/class.c
--- trunk/reactos/subsys/win32k/ntuser/class.c	2005-09-11 14:38:57 UTC (rev 17801)
+++ trunk/reactos/subsys/win32k/ntuser/class.c	2005-09-11 14:48:32 UTC (rev 17802)
@@ -559,8 +559,6 @@
 void FASTCALL
 co_IntSetClassLong(PWINDOW_OBJECT Window, ULONG Offset, LONG dwNewLong, BOOL Ansi)
 {
-   PWINDOW_OBJECT Parent, Owner;
-
    ASSERT_REFS_CO(Window);
 
    if ((int)Offset >= 0)
@@ -591,25 +589,11 @@
          break;
       case GCL_HICON:
          Window->Class->hIcon = (HICON)dwNewLong;
-         Owner = IntGetOwner(Window);
-         Parent = IntGetParent(Window);
 
-         if ((!Owner) && (!Parent))
+         if (!IntGetOwner(Window) && !IntGetParent(Window))
          {
             co_IntShellHookNotify(HSHELL_REDRAW, (LPARAM) Window->hSelf);
          }
-
-         if (Parent)
-         {
-            IntReleaseWindowObject(Parent);
-         }
-
-         if (Owner)
-         {
-            IntReleaseWindowObject(Owner);
-         }
-
-
          break;
       case GCL_HICONSM:
          Window->Class->hIconSm = (HICON)dwNewLong;

Modified: trunk/reactos/subsys/win32k/ntuser/desktop.c
--- trunk/reactos/subsys/win32k/ntuser/desktop.c	2005-09-11 14:38:57 UTC (rev 17801)
+++ trunk/reactos/subsys/win32k/ntuser/desktop.c	2005-09-11 14:48:32 UTC (rev 17802)
@@ -463,7 +463,6 @@
 PWINDOW_OBJECT FASTCALL UserGetDesktopWindow(VOID)
 {
    PDESKTOP_OBJECT pdo = IntGetActiveDesktop();
-   PWINDOW_OBJECT DeskWnd;
 
    if (!pdo)
    {
@@ -471,11 +470,7 @@
       return NULL;
    }
 
-   //temp hack
-   DeskWnd = IntGetWindowObject(pdo->DesktopWindow);
-   if (DeskWnd)
-      IntReleaseWindowObject(DeskWnd);
-   return DeskWnd;
+   return UserGetWindowObject(pdo->DesktopWindow);
 }
 
 
@@ -1190,15 +1185,12 @@
    IntGdiGetClipBox(hDC, &Rect);
 
    hWndDesktop = IntGetDesktopWindow();
-   if (!(WndDesktop = IntGetWindowObject(hWndDesktop)))
+   if (!(WndDesktop = UserGetWindowObject(hWndDesktop)))
       return FALSE;
 
    DesktopBrush = (HBRUSH)IntGetClassLong(WndDesktop, GCL_HBRBACKGROUND, FALSE); //fixme: verify retval
 
-   //temp hack
-   IntReleaseWindowObject(WndDesktop);
 
-
    /*
     * Paint desktop background
     */
@@ -1207,7 +1199,7 @@
    {
       PWINDOW_OBJECT DeskWin;
 
-      if((DeskWin = IntGetWindowObject(hWndDesktop)))
+      if((DeskWin = UserGetWindowObject(hWndDesktop)))
       {
          SIZE sz;
          int x, y;
@@ -1215,8 +1207,8 @@
 
          sz.cx = DeskWin->WindowRect.right - DeskWin->WindowRect.left;
          sz.cy = DeskWin->WindowRect.bottom - DeskWin->WindowRect.top;
-         IntReleaseWindowObject(DeskWin);
 
+
          x = (sz.cx / 2) - (WinSta->cxWallpaper / 2);
          y = (sz.cy / 2) - (WinSta->cyWallpaper / 2);
 

Modified: trunk/reactos/subsys/win32k/ntuser/focus.c
--- trunk/reactos/subsys/win32k/ntuser/focus.c	2005-09-11 14:38:57 UTC (rev 17801)
+++ trunk/reactos/subsys/win32k/ntuser/focus.c	2005-09-11 14:48:32 UTC (rev 17802)
@@ -61,10 +61,13 @@
 VOID FASTCALL
 co_IntSendActivateMessages(HWND hWndPrev, HWND hWnd, BOOL MouseActivate)
 {
-   PWINDOW_OBJECT Window, Owner, Parent;
-
-   if (hWnd && (Window = IntGetWindowObject(hWnd)))
+   PWINDOW_OBJECT Window;
+   
+   if ((Window = UserGetWindowObject(hWnd)))
    {
+      
+      UserRefObjectCo(Window);
+      
       /* Send palette messages */
       if (co_IntPostOrSendMessage(hWnd, WM_QUERYNEWPALETTE, 0, 0))
       {
@@ -76,22 +79,12 @@
          co_WinPosSetWindowPos(Window, HWND_TOP, 0, 0, 0, 0,
                                SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOSENDCHANGING);
 
-
-      Owner = IntGetOwner(Window);
-      if (!Owner)
+      if (!IntGetOwner(Window) && !IntGetParent(Window))
       {
-         Parent = IntGetParent(Window);
-         if (!Parent)
-            co_IntShellHookNotify(HSHELL_WINDOWACTIVATED, (LPARAM) hWnd);
-         else
-            IntReleaseWindowObject(Parent);
+         co_IntShellHookNotify(HSHELL_WINDOWACTIVATED, (LPARAM) hWnd);
       }
-      else
-      {
-         IntReleaseWindowObject(Owner);
-      }
 
-      IntReleaseWindowObject(Window);
+      UserDerefObjectCo(Window);
 
       /* FIXME: IntIsWindow */
 
@@ -130,17 +123,15 @@
 
    for(Child = Root->FirstChild; Child; Child = Child->NextSibling)
    {
-      OwnerWnd = IntGetWindowObject(Child->hOwner);
+      OwnerWnd = UserGetWindowObject(Child->hOwner);
       if(!OwnerWnd)
          continue;
 
       if(OwnerWnd == Owner)
       {
          Ret = Child->hSelf;
-         IntReleaseWindowObject(OwnerWnd);
          return Ret;
       }
-      IntReleaseWindowObject(OwnerWnd);
    }
 
    return NULL;
@@ -215,9 +206,9 @@
 }
 
 BOOL FASTCALL
-co_IntSetForegroundWindow(PWINDOW_OBJECT Window)
+co_IntSetForegroundWindow(PWINDOW_OBJECT Window)//FIXME: can Window be NULL??
 {
-   ASSERT_REFS_CO(Window);
+   /*if (Window)*/ ASSERT_REFS_CO(Window);
 
    return co_IntSetForegroundAndFocusWindow(Window, Window, FALSE);
 }
@@ -234,18 +225,18 @@
    {
       BOOL Ret;
       PWINDOW_OBJECT TopWnd;
-      PWINDOW_OBJECT DesktopWindow = IntGetWindowObject(IntGetDesktopWindow());
+      PWINDOW_OBJECT DesktopWindow = UserGetWindowObject(IntGetDesktopWindow());
       if(DesktopWindow)
       {
          Top = IntFindChildWindowToOwner(DesktopWindow, Window);
-         if((TopWnd = IntGetWindowObject(Top)))
+         if((TopWnd = UserGetWindowObject(Top)))
          {
+            UserRefObjectCo(TopWnd);
             Ret = co_IntMouseActivateWindow(TopWnd);
-            IntReleaseWindowObject(TopWnd);
-            IntReleaseWindowObject(DesktopWindow);
+            UserDerefObjectCo(TopWnd);
+            
             return Ret;
          }
-         IntReleaseWindowObject(DesktopWindow);
       }
       return FALSE;
    }
@@ -253,23 +244,6 @@
 
    TopWindow = UserGetAncestor(Window, GA_ROOT);
    if (!TopWindow) return FALSE;
-//   if (TopWindow != Window)
-//   {
-      
-//   Top = UserGetAncestor(Window, GA_ROOT);
-//   if (Top != Window->hSelf)
-//   {
-//      TopWindow = IntGetWindowObject(Top);
-//      if (TopWindow == NULL)
-//      {
-//         SetLastWin32Error(ERROR_INVALID_WINDOW_HANDLE);
-//         return FALSE;
-//      }
-//   }
-//   else
-//   {
-//      TopWindow = Window;
-//   }
 
    /* TMN: Check return valud from this function? */
    UserRefObjectCo(TopWindow);
@@ -278,10 +252,6 @@
    
    UserDerefObjectCo(TopWindow);
 
-//   if (TopWindow != Window)
-//   {
-//      IntReleaseWindowObject(TopWindow);
-//   }
    return TRUE;
 }
 

Modified: trunk/reactos/subsys/win32k/ntuser/message.c
--- trunk/reactos/subsys/win32k/ntuser/message.c	2005-09-11 14:38:57 UTC (rev 17801)
+++ trunk/reactos/subsys/win32k/ntuser/message.c	2005-09-11 14:48:32 UTC (rev 17802)
@@ -318,7 +318,6 @@
 {
    NTSTATUS Status;
    NTUSERDISPATCHMESSAGEINFO MsgInfo;
-   PWINDOW_OBJECT WindowObject;
    LRESULT Result = TRUE;
    DECLARE_RETURN(LRESULT);
 
@@ -352,19 +351,19 @@
    }
    else
    {
+      PWINDOW_OBJECT Window;
+      
       /* Get the window object. */
-      WindowObject = IntGetWindowObject(MsgInfo.Msg.hwnd);
-      if (NULL == WindowObject)
+      Window = UserGetWindowObject(MsgInfo.Msg.hwnd);
+      if (NULL == Window)
       {
-         SetLastWin32Error(ERROR_INVALID_WINDOW_HANDLE);
          MsgInfo.HandledByKernel = TRUE;
          Result = 0;
       }
       else
       {
-         if (WindowObject->OwnerThread != PsGetCurrentThread())
+         if (Window->OwnerThread != PsGetCurrentThread())
          {
-            IntReleaseWindowObject(WindowObject);
             DPRINT1("Window doesn't belong to the calling thread!\n");
             MsgInfo.HandledByKernel = TRUE;
             Result = 0;
@@ -375,30 +374,29 @@
 
             MsgInfo.HandledByKernel = FALSE;
             Result = 0;
-            if (0xFFFF0000 != ((DWORD) WindowObject->WndProcW & 0xFFFF0000))
+            if (0xFFFF0000 != ((DWORD) Window->WndProcW & 0xFFFF0000))
             {
-               if (0xFFFF0000 != ((DWORD) WindowObject->WndProcA & 0xFFFF0000))
+               if (0xFFFF0000 != ((DWORD) Window->WndProcA & 0xFFFF0000))
                {
                   /* Both Unicode and Ansi winprocs are real, use whatever
                      usermode prefers */
-                  MsgInfo.Proc = (MsgInfo.Ansi ? WindowObject->WndProcA
-                                  : WindowObject->WndProcW);
+                  MsgInfo.Proc = (MsgInfo.Ansi ? Window->WndProcA
+                                  : Window->WndProcW);
                }
                else
                {
                   /* Real Unicode winproc */
                   MsgInfo.Ansi = FALSE;
-                  MsgInfo.Proc = WindowObject->WndProcW;
+                  MsgInfo.Proc = Window->WndProcW;
                }
             }
             else
             {
                /* Must have real Ansi winproc */
                MsgInfo.Ansi = TRUE;
-               MsgInfo.Proc = WindowObject->WndProcA;
+               MsgInfo.Proc = Window->WndProcA;
             }
          }
-         IntReleaseWindowObject(WindowObject);
       }
    }
    Status = MmCopyToCaller(UnsafeMsgInfo, &MsgInfo, sizeof(NTUSERDISPATCHMESSAGEINFO));
@@ -511,13 +509,15 @@
    ULONG Result;
    PWINDOW_OBJECT Parent;
 
+   ASSERT_REFS_CO(MsgWindow);
+
    if(*HitTest == (USHORT)HTTRANSPARENT)
    {
       /* eat the message, search again! */
       return TRUE;
    }
 
-   Parent = IntGetParent(MsgWindow);
+   Parent = IntGetParent(MsgWindow);//fixme: deref retval?
    /* fixme: abort if no parent ? */
    Result = co_IntSendMessage(MsgWindow->hSelf,
                               WM_MOUSEACTIVATE,
@@ -548,12 +548,14 @@
 {
    PWINDOW_OBJECT Window;
 
-   if(!(Window = IntGetWindowObject(Msg->hwnd)))
+   if(!(Window = UserGetWindowObject(Msg->hwnd)))
    {
       /* let's just eat the message?! */
       return TRUE;
    }
 
+   UserRefObjectCo(Window);
+
    if(ThreadQueue == Window->MessageQueue &&
          ThreadQueue->CaptureWindow != Window->hSelf)
    {
@@ -566,10 +568,12 @@
          PWINDOW_OBJECT DesktopWindow;
          HWND hDesktop = IntGetDesktopWindow();
 
-         if((DesktopWindow = IntGetWindowObject(hDesktop)))
+         if((DesktopWindow = UserGetWindowObject(hDesktop)))
          {
             PWINDOW_OBJECT Wnd;
-
+            
+            UserRefObjectCo(DesktopWindow);
+            
             co_WinPosWindowFromPoint(DesktopWindow, Window->MessageQueue, &Msg->pt, &Wnd);
             if(Wnd)
             {
@@ -585,15 +589,15 @@
                   }
 
                   /* eat the message */
-                  IntReleaseWindowObject(Wnd);
-                  IntReleaseWindowObject(Window);
-                  IntReleaseWindowObject(DesktopWindow);
+                  UserDerefObject(Wnd);
+                  UserDerefObjectCo(DesktopWindow);
+                  UserDerefObjectCo(Window);
                   return TRUE;
                }
-               IntReleaseWindowObject(Wnd);
+               UserDerefObject(Wnd);
             }
 
-            IntReleaseWindowObject(DesktopWindow);
+            UserDerefObjectCo(DesktopWindow);
          }
       }
    }
@@ -641,7 +645,7 @@
       }
    }
 
-   IntReleaseWindowObject(Window);
+   UserDerefObjectCo(Window);
    return FALSE;
 }
 
@@ -651,7 +655,7 @@
  */
 BOOL FASTCALL
 co_IntPeekMessage(PUSER_MESSAGE Msg,
-                  HWND Wnd,
+                  HWND hWnd,
                   UINT MsgFilterMin,
                   UINT MsgFilterMax,
                   UINT RemoveMsg)
@@ -703,7 +707,7 @@
    Present = co_MsqFindMessage(ThreadQueue,
                                FALSE,
                                RemoveMessages,
-                               Wnd,
+                               hWnd,
                                MsgFilterMin,
                                MsgFilterMax,
                                &Message);
@@ -721,7 +725,7 @@
    Present = co_MsqFindMessage(ThreadQueue,
                                TRUE,
                                RemoveMessages,
-                               Wnd,
+                               hWnd,
                                MsgFilterMin,
                                MsgFilterMax,
                                &Message);
@@ -740,14 +744,14 @@
       ;
 
    /* Check for paint messages. */
-   if (IntGetPaintMessage(Wnd, MsgFilterMin, MsgFilterMax, PsGetWin32Thread(), &Msg->Msg, RemoveMessages))
+   if (IntGetPaintMessage(hWnd, MsgFilterMin, MsgFilterMax, PsGetWin32Thread(), &Msg->Msg, RemoveMessages))
    {
       Msg->FreeLParam = FALSE;
       return TRUE;
    }
 
    /* Check for WM_(SYS)TIMER messages */
-   Present = MsqGetTimerMessage(ThreadQueue, Wnd, MsgFilterMin, MsgFilterMax,
+   Present = MsqGetTimerMessage(ThreadQueue, hWnd, MsgFilterMin, MsgFilterMax,
                                 &Msg->Msg, RemoveMessages);
    if (Present)
    {
@@ -763,19 +767,22 @@
       {
          PWINDOW_OBJECT MsgWindow = NULL;
 
-         if(Msg->Msg.hwnd && (MsgWindow = IntGetWindowObject(Msg->Msg.hwnd)) &&
+         if(Msg->Msg.hwnd && (MsgWindow = UserGetWindowObject(Msg->Msg.hwnd)) &&
                Msg->Msg.message >= WM_MOUSEFIRST && Msg->Msg.message <= WM_MOUSELAST)
          {
             USHORT HitTest;
 
+            UserRefObjectCo(MsgWindow);
+            
             if(co_IntTranslateMouseMessage(ThreadQueue, &Msg->Msg, &HitTest, TRUE))
                /* FIXME - check message filter again, if the message doesn't match anymore,
                           search again */
             {
-               IntReleaseWindowObject(MsgWindow);
+               UserDerefObjectCo(MsgWindow);
                /* eat the message, search again */
                goto CheckMessages;
             }
+            
             if(ThreadQueue->CaptureWindow == NULL)
             {
                co_IntSendHitTestMessages(ThreadQueue, &Msg->Msg);
@@ -783,21 +790,23 @@
                      IS_BTN_MESSAGE(Msg->Msg.message, DOWN) &&
                      co_IntActivateWindowMouse(ThreadQueue, &Msg->Msg, MsgWindow, &HitTest))
                {
-                  IntReleaseWindowObject(MsgWindow);
+                  UserDerefObjectCo(MsgWindow);
                   /* eat the message, search again */
                   goto CheckMessages;
                }
             }
+            
+            UserDerefObjectCo(MsgWindow);
          }
          else
          {
             co_IntSendHitTestMessages(ThreadQueue, &Msg->Msg);
          }
 
-         if(MsgWindow)
-         {
-            IntReleaseWindowObject(MsgWindow);
-         }
+//         if(MsgWindow)
+//         {
+//            UserDerefObject(MsgWindow);
+//         }
 
          return TRUE;
       }
@@ -820,7 +829,7 @@
 
 BOOL STDCALL
 NtUserPeekMessage(PNTUSERGETMESSAGEINFO UnsafeInfo,
-                  HWND Wnd,
+                  HWND hWnd,
                   UINT MsgFilterMin,
                   UINT MsgFilterMax,
                   UINT RemoveMsg)
@@ -839,17 +848,12 @@
    UserEnterExclusive();
 
    /* Validate input */
-   if (NULL != Wnd)
+   if (hWnd && hWnd != INVALID_HANDLE_VALUE)
    {
-      Window = IntGetWindowObject(Wnd);
-      if (NULL == Window)
+      if (!(Window = UserGetWindowObject(hWnd)))
       {
-         Wnd = NULL;
+         RETURN(-1);
       }
-      else
-      {
-         IntReleaseWindowObject(Window);
-      }
    }
 
    if (MsgFilterMax < MsgFilterMin)
@@ -858,7 +862,7 @@
       MsgFilterMax = 0;
    }
 
-   Present = co_IntPeekMessage(&Msg, Wnd, MsgFilterMin, MsgFilterMax, RemoveMsg);
+   Present = co_IntPeekMessage(&Msg, hWnd, MsgFilterMin, MsgFilterMax, RemoveMsg);
    if (Present)
    {
       Info.Msg = Msg.Msg;
@@ -945,7 +949,7 @@
 
 BOOL STDCALL
 NtUserGetMessage(PNTUSERGETMESSAGEINFO UnsafeInfo,
-                 HWND Wnd,
+                 HWND hWnd,
                  UINT MsgFilterMin,
                  UINT MsgFilterMax)
 /*
@@ -962,7 +966,7 @@
    BOOL GotMessage;
    NTUSERGETMESSAGEINFO Info;
    NTSTATUS Status;
-   PWINDOW_OBJECT Window;
+   PWINDOW_OBJECT Window = NULL;
    PMSGMEMORY MsgMemoryEntry;
    PVOID UserMem;
    UINT Size;
@@ -973,14 +977,13 @@
    UserEnterExclusive();
 
    /* Validate input */
-   if (NULL != Wnd)
+   if (hWnd && !(Window = UserGetWindowObject(hWnd)))
    {
-      Window = IntGetWindowObject(Wnd);
-      if(!Window)
-         Wnd = NULL;
-      else
-         IntReleaseWindowObject(Window);
+      RETURN(-1);
    }
+   
+//   if (Window) UserRefObjectCo(Window);
+   
    if (MsgFilterMax < MsgFilterMin)
    {
       MsgFilterMin = 0;
@@ -989,7 +992,7 @@
 
    do
    {
-      GotMessage = co_IntPeekMessage(&Msg, Wnd, MsgFilterMin, MsgFilterMax, PM_REMOVE);
+      GotMessage = co_IntPeekMessage(&Msg, hWnd, MsgFilterMin, MsgFilterMax, PM_REMOVE);
       if (GotMessage)
       {
          Info.Msg = Msg.Msg;
@@ -1038,7 +1041,7 @@
             RETURN( (BOOL) -1);
          }
       }
-      else if (! co_IntWaitMessage(Wnd, MsgFilterMin, MsgFilterMax))
+      else if (! co_IntWaitMessage(hWnd, MsgFilterMin, MsgFilterMax))
       {
          RETURN( (BOOL) -1);
       }
@@ -1048,6 +1051,8 @@
    RETURN( WM_QUIT != Info.Msg.message);
 
 CLEANUP:
+//   if (Window) UserDerefObjectCo(Window);
+
    DPRINT("Leave NtUserGetMessage\n");
    UserLeave();
    END_CLEANUP;
@@ -1169,7 +1174,6 @@
                 WPARAM wParam,
                 LPARAM lParam)
 {
-   PWINDOW_OBJECT Window;
    MSG UserModeMsg, KernelModeMsg;
    LARGE_INTEGER LargeTickCount;
    NTSTATUS Status;
@@ -1185,9 +1189,9 @@
       PWINDOW_OBJECT DesktopWindow;
       ULONG i;
 
-      DesktopWindow = IntGetWindowObject(IntGetDesktopWindow());
+      DesktopWindow = UserGetWindowObject(IntGetDesktopWindow());
       List = IntWinListChildren(DesktopWindow);
-      IntReleaseWindowObject(DesktopWindow);
+      
       if (List != NULL)
       {
          for (i = 0; List[i]; i++)
@@ -1197,15 +1201,15 @@
    }
    else
    {
-      Window = IntGetWindowObject(Wnd);
+      PWINDOW_OBJECT Window;
+      
+      Window = UserGetWindowObject(Wnd);
       if (NULL == Window)
       {
-         SetLastWin32Error(ERROR_INVALID_WINDOW_HANDLE);
          return FALSE;
       }
       if(Window->Status & WINDOWSTATUS_DESTROYING)
       {
-         IntReleaseWindowObject(Window);
          DPRINT1("Attempted to post message to window 0x%x that is being destroyed!\n", Wnd);
          /* FIXME - last error code? */
          return FALSE;
@@ -1229,7 +1233,6 @@
       MsqPostMessage(Window->MessageQueue, &KernelModeMsg,
                      NULL != MsgMemoryEntry && 0 != KernelModeMsg.lParam,
                      QS_POSTMESSAGE);
-      IntReleaseWindowObject(Window);
    }
 
    return TRUE;
@@ -1336,7 +1339,8 @@
    return 0;
 }
 
-static LRESULT FASTCALL
+static 
+LRESULT FASTCALL
 co_IntSendMessageTimeoutSingle(HWND hWnd,
                                UINT Msg,
                                WPARAM wParam,
@@ -1347,19 +1351,20 @@
 {
    ULONG_PTR Result;
    NTSTATUS Status;
-   PWINDOW_OBJECT Window;
+   PWINDOW_OBJECT Window = NULL;
    PMSGMEMORY MsgMemoryEntry;
    INT lParamBufferSize;
    LPARAM lParamPacked;
    PW32THREAD Win32Thread;
+   DECLARE_RETURN(LRESULT);
 
    /* FIXME: Call hooks. */
-   Window = IntGetWindowObject(hWnd);
-   if (!Window)
+   if (!(Window = UserGetWindowObject(hWnd)))
    {
-      SetLastWin32Error(ERROR_INVALID_WINDOW_HANDLE);
-      return FALSE;
+       RETURN( FALSE);
    }
+   
+   UserRefObjectCo(Window);
 
    Win32Thread = PsGetWin32Thread();
 
@@ -1369,8 +1374,7 @@
       if (Win32Thread->IsExiting)
       {
          /* Never send messages to exiting threads */
-         IntReleaseWindowObject(Window);
-         return FALSE;
+          RETURN( FALSE);
       }
 
       /* See if this message type is present in the table */
@@ -1386,9 +1390,8 @@
 
       if (! NT_SUCCESS(PackParam(&lParamPacked, Msg, wParam, lParam)))
       {
-         IntReleaseWindowObject(Window);
          DPRINT1("Failed to pack message parameters\n");
-         return FALSE;
+          RETURN( FALSE);
       }
       if (0xFFFF0000 != ((DWORD) Window->WndProcW & 0xFFFF0000))
       {
@@ -1408,46 +1411,47 @@
 
       if (! NT_SUCCESS(UnpackParam(lParamPacked, Msg, wParam, lParam)))
       {
-         IntReleaseWindowObject(Window);
          DPRINT1("Failed to unpack message parameters\n");
-         return TRUE;
+          RETURN( TRUE);
       }
 
-      IntReleaseWindowObject(Window);
-      return TRUE;
+       RETURN( TRUE);
    }
 
    if(uFlags & SMTO_ABORTIFHUNG && MsqIsHung(Window->MessageQueue))
    {
-      IntReleaseWindowObject(Window);
       /* FIXME - Set a LastError? */
-      return FALSE;
+       RETURN( FALSE);
    }
 
    if(Window->Status & WINDOWSTATUS_DESTROYING)
    {
-      IntReleaseWindowObject(Window);
       /* FIXME - last error? */
       DPRINT1("Attempted to send message to window 0x%x that is being destroyed!\n", hWnd);
-      return FALSE;
+       RETURN( FALSE);
    }
 
    Status = co_MsqSendMessage(Window->MessageQueue, hWnd, Msg, wParam, lParam,
                               uTimeout, (uFlags & SMTO_BLOCK), FALSE, uResult);
-   IntReleaseWindowObject(Window);
+
+
    if (STATUS_TIMEOUT == Status)
    {
       /* MSDN says GetLastError() should return 0 after timeout */
       SetLastWin32Error(0);
-      return FALSE;
+       RETURN( FALSE);
    }
    else if (! NT_SUCCESS(Status))
    {
       SetLastNtError(Status);
-      return FALSE;
+       RETURN( FALSE);
    }
 
-   return TRUE;
+   RETURN( TRUE);
+   
+CLEANUP:
+   if (Window) UserDerefObjectCo(Window);
+   END_CLEANUP;
 }
 
 LRESULT FASTCALL
@@ -1468,14 +1472,14 @@
       return co_IntSendMessageTimeoutSingle(hWnd, Msg, wParam, lParam, uFlags, uTimeout, uResult);
    }
 
-   DesktopWindow = IntGetWindowObject(IntGetDesktopWindow());
+   DesktopWindow = UserGetWindowObject(IntGetDesktopWindow());
    if (NULL == DesktopWindow)
    {
       SetLastWin32Error(ERROR_INTERNAL_ERROR);
       return 0;
    }
+
    Children = IntWinListChildren(DesktopWindow);
-   IntReleaseWindowObject(DesktopWindow);
    if (NULL == Children)
    {
       return 0;
@@ -1509,10 +1513,8 @@
       return 0;
    }
 
-   Window = IntGetWindowObject(hWnd);
-   if(!Window)
+   if(!(Window = UserGetWindowObject(hWnd)))
    {
-      SetLastWin32Error(ERROR_INVALID_WINDOW_HANDLE);
       return 0;
    }
 
@@ -1528,13 +1530,11 @@
       }
    }
 
-   IntReleaseWindowObject(Window);
-
    return (LRESULT)Result;
 }
 
 LRESULT FASTCALL
-co_IntDoSendMessage(HWND Wnd,
+co_IntDoSendMessage(HWND hWnd,
                     UINT Msg,
                     WPARAM wParam,
                     LPARAM lParam,
@@ -1552,15 +1552,14 @@
    RtlZeroMemory(&Info, sizeof(NTUSERSENDMESSAGEINFO));
 
    /* FIXME: Call hooks. */
-   if (HWND_BROADCAST != Wnd)
+   if (HWND_BROADCAST != hWnd)
    {
-      Window = IntGetWindowObject(Wnd);
+      Window = UserGetWindowObject(hWnd);
       if (NULL == Window)
       {
          /* Tell usermode to not touch this one */
          Info.HandledByKernel = TRUE;
          MmCopyToCaller(UnsafeInfo, &Info, sizeof(NTUSERSENDMESSAGEINFO));
-         SetLastWin32Error(ERROR_INVALID_WINDOW_HANDLE);
          return 0;
       }
    }
@@ -1568,7 +1567,7 @@
    /* FIXME: Check for an exiting window. */
 
    /* See if the current thread can handle the message */
-   if (HWND_BROADCAST != Wnd && NULL != PsGetWin32Thread() &&
+   if (HWND_BROADCAST != hWnd && NULL != PsGetWin32Thread() &&
          Window->MessageQueue == PsGetWin32Thread()->MessageQueue)
    {
       /* Gather the information usermode needs to call the window proc directly */
@@ -1599,17 +1598,16 @@
          Info.Ansi = TRUE;
          Info.Proc = Window->WndProcA;
       }
-      IntReleaseWindowObject(Window);
    }
    else
    {
       /* Must be handled by other thread */
-      if (HWND_BROADCAST != Wnd)
-      {
-         IntReleaseWindowObject(Window);
-      }
+//      if (HWND_BROADCAST != hWnd)
+//      {
+//         UserDerefObject(Window);
+//      }
       Info.HandledByKernel = TRUE;
-      UserModeMsg.hwnd = Wnd;
+      UserModeMsg.hwnd = hWnd;
       UserModeMsg.message = Msg;
       UserModeMsg.wParam = wParam;
       UserModeMsg.lParam = lParam;

Modified: trunk/reactos/subsys/win32k/ntuser/misc.c
--- trunk/reactos/subsys/win32k/ntuser/misc.c	2005-09-11 14:38:57 UTC (rev 17801)
+++ trunk/reactos/subsys/win32k/ntuser/misc.c	2005-09-11 14:48:32 UTC (rev 17802)
@@ -182,35 +182,30 @@
    {
       case ONEPARAM_ROUTINE_GETMENU:
          {
-            PWINDOW_OBJECT WindowObject;
+            PWINDOW_OBJECT Window;
             DWORD Result;
 
-            WindowObject = IntGetWindowObject((HWND)Param);
-            if(!WindowObject)
+            if(!(Window = UserGetWindowObject((HWND)Param)))
             {
-               SetLastWin32Error(ERROR_INVALID_WINDOW_HANDLE);
                RETURN( FALSE);
             }
[truncated at 1000 lines; 2536 more skipped]