[YAROTOWS] Reintegrate the branch. For a brighter future.
[reactos.git] / reactos / subsystems / win32 / win32k / ntuser / clipboard.c
index 6243cc4..4d79962 100644 (file)
@@ -7,7 +7,7 @@
  *                   Pablo Borobia <pborobia@gmail.com>
  */
 
-#include <w32k.h>
+#include <win32k.h>
 
 #define NDEBUG
 #include <debug.h>
 #define DATA_DELAYED_RENDER  0
 #define DATA_SYNTHESIZED_RENDER -1
 
-#define USE_WINSTA \
-    PWINSTATION_OBJECT WinStaObj; \
-    WinStaObj = PsGetCurrentThreadWin32Thread()->Desktop->WindowStation;
-
-#define WINSTA_ClipboardThread WinStaObj->Clipboard->ClipboardThread
-#define WINSTA_ClipboardOwnerThread WinStaObj->Clipboard->ClipboardOwnerThread
-#define WINSTA_ClipboardWindow WinStaObj->Clipboard->ClipboardWindow
-#define WINSTA_ClipboardViewerWindow WinStaObj->Clipboard->ClipboardViewerWindow
-#define WINSTA_ClipboardOwnerWindow WinStaObj->Clipboard->ClipboardOwnerWindow
-#define WINSTA_sendDrawClipboardMsg WinStaObj->Clipboard->sendDrawClipboardMsg
-#define WINSTA_recentlySetClipboard WinStaObj->Clipboard->recentlySetClipboard
-#define WINSTA_delayedRender WinStaObj->Clipboard->delayedRender
-#define WINSTA_lastEnumClipboardFormats WinStaObj->Clipboard->lastEnumClipboardFormats
-#define WINSTA_ClipboardSequenceNumber WinStaObj->Clipboard->ClipboardSequenceNumber
-#define WINSTA_WindowsChain WinStaObj->Clipboard->WindowsChain
-#define WINSTA_ClipboardData WinStaObj->Clipboard->ClipboardData
-#define WINSTA_synthesizedData WinStaObj->Clipboard->synthesizedData
-#define WINSTA_synthesizedDataSize WinStaObj->Clipboard->synthesizedDataSize
-
-PW32THREAD      ClipboardThread;
-PW32THREAD      ClipboardOwnerThread;
-PWINDOW_OBJECT  ClipboardWindow;
-PWINDOW_OBJECT  ClipboardViewerWindow;
-PWINDOW_OBJECT  ClipboardOwnerWindow;
+PTHREADINFO      ClipboardThread;
+PTHREADINFO      ClipboardOwnerThread;
+PWND  ClipboardWindow;
+PWND  ClipboardViewerWindow;
+PWND  ClipboardOwnerWindow;
 BOOL            sendDrawClipboardMsg;
 BOOL            recentlySetClipboard;
 BOOL            delayedRender;
@@ -57,7 +38,7 @@ DWORD synthesizedDataSize;
 /* return the pointer to the prev window of the finded window,
    if NULL does not exists in the chain */
 PCLIPBOARDCHAINELEMENT FASTCALL
-IntIsWindowInChain(PWINDOW_OBJECT window)
+IntIsWindowInChain(PWND window)
 {
     PCLIPBOARDCHAINELEMENT wce = WindowsChain;
 
@@ -73,20 +54,20 @@ IntIsWindowInChain(PWINDOW_OBJECT window)
     return wce;
 }
 
-VOID FASTCALL printChain()
+VOID FASTCALL printChain(VOID)
 {
     /*test*/
     PCLIPBOARDCHAINELEMENT wce2 = WindowsChain;
     while (wce2)
     {
-        DPRINT1("chain: %p\n", wce2->window->hSelf);
+        DPRINT1("chain: %p\n", wce2->window->head.h);
         wce2 = wce2->next;
     }
 }
 
 /* the new window always have to be the first in the chain */
 PCLIPBOARDCHAINELEMENT FASTCALL
-IntAddWindowToChain(PWINDOW_OBJECT window)
+IntAddWindowToChain(PWND window)
 {
     PCLIPBOARDCHAINELEMENT wce = NULL;
 
@@ -94,7 +75,7 @@ IntAddWindowToChain(PWINDOW_OBJECT window)
     {
         wce = WindowsChain;
 
-        wce = ExAllocatePool(PagedPool, sizeof(CLIPBOARDCHAINELEMENT));
+        wce = ExAllocatePoolWithTag(PagedPool, sizeof(CLIPBOARDCHAINELEMENT), USERTAG_CLIPBOARD);
         if (wce == NULL)
         {
             SetLastWin32Error(ERROR_NOT_ENOUGH_MEMORY);
@@ -115,7 +96,7 @@ exit_addChain:
 }
 
 PCLIPBOARDCHAINELEMENT FASTCALL
-IntRemoveWindowFromChain(PWINDOW_OBJECT window)
+IntRemoveWindowFromChain(PWND window)
 {
     PCLIPBOARDCHAINELEMENT wce = WindowsChain;
        PCLIPBOARDCHAINELEMENT *link = &WindowsChain;
@@ -148,7 +129,7 @@ IntRemoveWindowFromChain(PWINDOW_OBJECT window)
 /*==============================================================*/
 /* if format exists, returns a non zero value (pointing to format object) */
 PCLIPBOARDELEMENT FASTCALL
-intIsFormatAvailable(format)
+intIsFormatAvailable(UINT format)
 {
     PCLIPBOARDELEMENT ret = NULL;
     PCLIPBOARDELEMENT ce = ClipboardData;
@@ -167,7 +148,7 @@ intIsFormatAvailable(format)
 
 /* counts how many distinct format were are in the clipboard */
 DWORD FASTCALL
-IntCountClipboardFormats()
+IntCountClipboardFormats(VOID)
 {
     DWORD ret = 0;
     PCLIPBOARDELEMENT ce = ClipboardData;
@@ -186,7 +167,7 @@ intAddFormatedData(UINT format, HANDLE hData, DWORD size)
 {
     PCLIPBOARDELEMENT ce = NULL;
 
-    ce = ExAllocatePool(PagedPool, sizeof(CLIPBOARDELEMENT));
+    ce = ExAllocatePoolWithTag(PagedPool, sizeof(CLIPBOARDELEMENT), USERTAG_CLIPBOARD);
     if (ce == NULL)
     {
         SetLastWin32Error(ERROR_NOT_ENOUGH_MEMORY);
@@ -240,7 +221,7 @@ intRemoveFormatedData(UINT format)
 }
 
 VOID FASTCALL
-IntEmptyClipboardData()
+IntEmptyClipboardData(VOID)
 {
     PCLIPBOARDELEMENT ce = ClipboardData;
     PCLIPBOARDELEMENT tmp;
@@ -248,7 +229,10 @@ IntEmptyClipboardData()
     while(ce)
     {
         tmp = ce->next;
-        ExFreePool(ce->hData);
+               if (ce->hData)
+               {
+            ExFreePool(ce->hData);
+        }
            ExFreePool(ce);
            ce = tmp;
     }
@@ -259,34 +243,62 @@ IntEmptyClipboardData()
 /*==============================================================*/
 
 HANDLE FASTCALL
-renderBITMAPfromDIB(LPBYTE hDIB)
+renderBITMAPfromDIB(LPBYTE pDIB)
 {
     HDC hdc;
     HBITMAP hbitmap;
-    unsigned int offset;
-    BITMAPINFOHEADER *ih;
+    PBITMAPINFO pBmi, pConvertedBmi = NULL;
+    NTSTATUS Status ;
+       UINT offset = 0; /* Stupid compiler */
+
+       pBmi = (BITMAPINFO*)pDIB;
 
     //hdc = UserGetDCEx(NULL, NULL, DCX_USESTYLE);
     hdc = UserGetDCEx(ClipboardWindow, NULL, DCX_USESTYLE);
 
-    ih = (BITMAPINFOHEADER *)hDIB;
-
-    offset = sizeof(BITMAPINFOHEADER) + ((ih->biBitCount <= 8) ? (sizeof(RGBQUAD) * (1 << ih->biBitCount)) : 0);
-
-    hbitmap = NtGdiCreateDIBitmapInternal(hdc,
-                                          ih->biWidth,
-                                          ih->biHeight,
-                                          CBM_INIT,
-                                          (LPBYTE)ih+offset,
-                                          (LPBITMAPINFO)ih,
-                                          DIB_RGB_COLORS,
-                                          ih->biBitCount,
-                                          ih->biSizeImage,
-                                          0,
-                                          0);
+    /* Probe it */
+    _SEH2_TRY
+    {
+        ProbeForRead(&pBmi->bmiHeader.biSize, sizeof(DWORD), 1);
+               ProbeForRead(pBmi, pBmi->bmiHeader.biSize, 1);
+               ProbeForRead(pBmi, DIB_BitmapInfoSize(pBmi, DIB_RGB_COLORS), 1);
+               pConvertedBmi = DIB_ConvertBitmapInfo(pBmi, DIB_RGB_COLORS);
+               if(!pConvertedBmi)
+               {
+                       Status = STATUS_INVALID_PARAMETER;
+               }
+               else
+               {
+                       offset = DIB_BitmapInfoSize((BITMAPINFO*)pBmi, DIB_RGB_COLORS);
+                       ProbeForRead(pDIB + offset, pConvertedBmi->bmiHeader.biSizeImage, 1);
+               }
+    }
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+    {
+        Status = _SEH2_GetExceptionCode();
+    }
+    _SEH2_END
+
+    if(!NT_SUCCESS(Status))
+    {
+        UserReleaseDC(ClipboardWindow, hdc, FALSE);
+        return NULL;
+    }
+
+    hbitmap = GreCreateDIBitmapInternal(hdc,
+                                        pConvertedBmi->bmiHeader.biWidth,
+                                        pConvertedBmi->bmiHeader.biHeight,
+                                        CBM_INIT,
+                                        pDIB+offset,
+                                        pConvertedBmi,
+                                        DIB_RGB_COLORS,
+                                        0,
+                                        0);
     //UserReleaseDC(NULL, hdc, FALSE);
     UserReleaseDC(ClipboardWindow, hdc, FALSE);
 
+       DIB_FreeConvertedBitmapInfo(pConvertedBmi, pBmi);
+
     return hbitmap;
 }
 
@@ -338,7 +350,7 @@ synthesizeData(UINT format)
 }
 
 VOID FASTCALL
-freeSynthesizedData()
+freeSynthesizedData(VOID)
 {
     ExFreePool(synthesizedData);
 }
@@ -346,7 +358,7 @@ freeSynthesizedData()
 /*==============================================================*/
 
 BOOL FASTCALL
-intIsClipboardOpenByMe()
+intIsClipboardOpenByMe(VOID)
 {
     /* check if we open the clipboard */
     if (ClipboardThread && ClipboardThread == PsGetCurrentThreadWin32Thread())
@@ -361,7 +373,7 @@ intIsClipboardOpenByMe()
 
 /* IntClipboardFreeWindow it's called when a window was destroyed */
 VOID FASTCALL
-IntClipboardFreeWindow(PWINDOW_OBJECT window)
+IntClipboardFreeWindow(PWND window)
 {
     /* called from co_UserFreeWindow in window.c */
     /* check if clipboard is not locked by this window, if yes, unlock it */
@@ -389,11 +401,11 @@ IntClipboardFreeWindow(PWINDOW_OBJECT window)
     }
 }
 
-BOOL STDCALL
+BOOL APIENTRY
 NtUserOpenClipboard(HWND hWnd, DWORD Unknown1)
 {
 
-    PWINDOW_OBJECT Window;
+    PWND Window;
     BOOL ret = FALSE;
 
     UserEnterExclusive();
@@ -408,7 +420,7 @@ NtUserOpenClipboard(HWND hWnd, DWORD Unknown1)
         {
             if  (ClipboardOwnerWindow)
             {
-                if (ClipboardOwnerWindow->hSelf == hWnd)
+                if (ClipboardOwnerWindow->head.h == hWnd)
                 {
                     ret = TRUE;
                 }
@@ -456,7 +468,7 @@ NtUserOpenClipboard(HWND hWnd, DWORD Unknown1)
     return ret;
 }
 
-BOOL STDCALL
+BOOL APIENTRY
 NtUserCloseClipboard(VOID)
 {
     BOOL ret = FALSE;
@@ -492,7 +504,7 @@ NtUserCloseClipboard(VOID)
     return ret;
 }
 
-HWND STDCALL
+HWND APIENTRY
 NtUserGetOpenClipboardWindow(VOID)
 {
     HWND ret = NULL;
@@ -501,7 +513,7 @@ NtUserGetOpenClipboardWindow(VOID)
 
     if (ClipboardWindow)
     {
-        ret = ClipboardWindow->hSelf;
+        ret = ClipboardWindow->head.h;
     }
 
     UserLeave();
@@ -509,12 +521,12 @@ NtUserGetOpenClipboardWindow(VOID)
     return ret;
 }
 
-BOOL STDCALL
+BOOL APIENTRY
 NtUserChangeClipboardChain(HWND hWndRemove, HWND hWndNewNext)
 {
     BOOL ret = FALSE;
     PCLIPBOARDCHAINELEMENT w = NULL;
-    PWINDOW_OBJECT removeWindow;
+    PWND removeWindow;
     UserEnterExclusive();
 
     removeWindow = UserGetWindowObject(hWndRemove);
@@ -537,9 +549,9 @@ NtUserChangeClipboardChain(HWND hWndRemove, HWND hWndNewNext)
         // then they do the chain
 
         /* WindowsChain->window may be NULL */
-        LPARAM lparam = WindowsChain->window == NULL ? 0 : (LPARAM)WindowsChain->window->hSelf;
-        DPRINT1("Message: WM_CHANGECBCHAIN to %p", WindowsChain->window->hSelf);
-        co_IntSendMessage(WindowsChain->window->hSelf, WM_CHANGECBCHAIN, (WPARAM)hWndRemove, lparam);
+        LPARAM lparam = WindowsChain->window == NULL ? 0 : (LPARAM)WindowsChain->window->head.h;
+        DPRINT1("Message: WM_CHANGECBCHAIN to %p", WindowsChain->window->head.h);
+        co_IntSendMessage(WindowsChain->window->head.h, WM_CHANGECBCHAIN, (WPARAM)hWndRemove, lparam);
     }
 
     UserLeave();
@@ -547,7 +559,7 @@ NtUserChangeClipboardChain(HWND hWndRemove, HWND hWndNewNext)
     return ret;
 }
 
-DWORD STDCALL
+DWORD APIENTRY
 NtUserCountClipboardFormats(VOID)
 {
     DWORD ret = 0;
@@ -560,7 +572,7 @@ NtUserCountClipboardFormats(VOID)
     return ret;
 }
 
-DWORD STDCALL
+DWORD APIENTRY
 NtUserEmptyClipboard(VOID)
 {
     BOOL ret = FALSE;
@@ -588,8 +600,8 @@ NtUserEmptyClipboard(VOID)
 
     if (ret && ClipboardOwnerWindow)
     {
-        DPRINT("Clipboard: WM_DESTROYCLIPBOARD to %p", ClipboardOwnerWindow->hSelf);
-        co_IntSendMessage( ClipboardOwnerWindow->hSelf, WM_DESTROYCLIPBOARD, 0, 0);
+        DPRINT("Clipboard: WM_DESTROYCLIPBOARD to %p", ClipboardOwnerWindow->head.h);
+        co_IntSendMessage( ClipboardOwnerWindow->head.h, WM_DESTROYCLIPBOARD, 0, 0);
     }
 
     UserLeave();
@@ -597,18 +609,17 @@ NtUserEmptyClipboard(VOID)
     return ret;
 }
 
-HANDLE STDCALL
-NtUserGetClipboardData(UINT uFormat, DWORD Unknown1)
+HANDLE APIENTRY
+NtUserGetClipboardData(UINT uFormat, PVOID pBuffer)
 {
     HANDLE ret = NULL;
-    PCHAR buffer;
 
     UserEnterShared();
 
     if (intIsClipboardOpenByMe())
     {
         /* when Unknown1 is zero, we returns to user32 the data size */
-        if (Unknown1 == 0)
+        if (!pBuffer)
         {
             PCLIPBOARDELEMENT data = intIsFormatAvailable(uFormat);
 
@@ -620,11 +631,11 @@ NtUserGetClipboardData(UINT uFormat, DWORD Unknown1)
                     /* tell owner what data needs to be rendered */
                     if (ClipboardOwnerWindow)
                     {
-                        ASSERT(ClipboardOwnerWindow->hSelf);
-                        co_IntSendMessage(ClipboardOwnerWindow->hSelf, WM_RENDERFORMAT, (WPARAM)uFormat, 0);
+                        ASSERT(ClipboardOwnerWindow->head.h);
+                        co_IntSendMessage(ClipboardOwnerWindow->head.h, WM_RENDERFORMAT, (WPARAM)uFormat, 0);
                         data = intIsFormatAvailable(uFormat);
                         ASSERT(data->size);
-                        ret = (HANDLE)data->size;
+                        ret = (HANDLE)(ULONG_PTR)data->size;
                     }
                 }
                 else
@@ -635,7 +646,7 @@ NtUserGetClipboardData(UINT uFormat, DWORD Unknown1)
                     }
 
                 }
-                ret = (HANDLE)data->size;
+                ret = (HANDLE)(ULONG_PTR)data->size;
             }
             else
             {
@@ -668,20 +679,36 @@ NtUserGetClipboardData(UINT uFormat, DWORD Unknown1)
                         }
                         else
                         {
-                            buffer = (PCHAR)Unknown1;
-                            memcpy(buffer, (PCHAR)synthesizedData, synthesizedDataSize);
+                            ret = (HANDLE)pBuffer;
 
-                            freeSynthesizedData();
+                            _SEH2_TRY
+                            {
+                                ProbeForWrite(pBuffer, synthesizedDataSize, 1);
+                                memcpy(pBuffer, (PCHAR)synthesizedData, synthesizedDataSize);
+                            }
+                            _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+                            {
+                                ret = NULL;
+                            }
+                            _SEH2_END
 
-                            ret = (HANDLE)Unknown1;
+                            freeSynthesizedData();
                         }
                     }
                     else
                     {
-                        buffer = (PCHAR)Unknown1;
-                        memcpy(buffer, (PCHAR)data->hData, data->size);
+                        ret = (HANDLE)pBuffer;
 
-                        ret = (HANDLE)Unknown1;
+                        _SEH2_TRY
+                        {
+                            ProbeForWrite(pBuffer, data->size, 1);
+                            memcpy(pBuffer, (PCHAR)data->hData, data->size);
+                        }
+                        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+                        {
+                            ret = NULL;
+                        }
+                        _SEH2_END
                     }
                 }
 
@@ -699,7 +726,7 @@ NtUserGetClipboardData(UINT uFormat, DWORD Unknown1)
     return ret;
 }
 
-INT STDCALL
+INT APIENTRY
 NtUserGetClipboardFormatName(UINT format, PUNICODE_STRING FormatName,
                              INT cchMaxCount)
 {
@@ -719,7 +746,7 @@ NtUserGetClipboardFormatName(UINT format, PUNICODE_STRING FormatName,
         return 0;
     }
 
-    _SEH_TRY
+    _SEH2_TRY
     {
         ProbeForWriteUnicodeString(FormatName);
         sFormatName = *(volatile UNICODE_STRING *)FormatName;
@@ -737,58 +764,16 @@ NtUserGetClipboardFormatName(UINT format, PUNICODE_STRING FormatName,
             ret = 0;
         }
     }
-    _SEH_HANDLE
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
     {
-        SetLastNtError(_SEH_GetExceptionCode());
+        SetLastNtError(_SEH2_GetExceptionCode());
     }
-    _SEH_END;
+    _SEH2_END;
 
     return ret;
 }
 
-UINT STDCALL
-NtUserRegisterClipboardFormat(PUNICODE_STRING FormatName)
-{
-    UINT ret = 0;
-    UNICODE_STRING cFormatName = {0};
-
-    if (FormatName == NULL)
-    {
-        SetLastWin32Error(ERROR_INVALID_PARAMETER);
-        return ret;
-    }
-
-    UserEnterExclusive();
-
-    _SEH_TRY
-    {
-        cFormatName = ProbeForReadUnicodeString(FormatName);
-
-        if (cFormatName.Length > 0)
-        {
-            ret = (UINT)IntAddAtom(cFormatName.Buffer);
-            //RtlFreeUnicodeString(&cFormatName);
-        }
-        else
-        {
-            SetLastWin32Error(ERROR_INVALID_NAME);
-            _SEH_LEAVE;
-        }
-
-    }
-    _SEH_HANDLE
-    {
-        SetLastNtError(_SEH_GetExceptionCode());
-    }
-    _SEH_END;
-
-    UserLeave();
-
-    return ret;
-}
-
-
-HWND STDCALL
+HWND APIENTRY
 NtUserGetClipboardOwner(VOID)
 {
     HWND ret = NULL;
@@ -797,7 +782,7 @@ NtUserGetClipboardOwner(VOID)
 
     if (ClipboardOwnerWindow)
     {
-        ret = ClipboardOwnerWindow->hSelf;
+        ret = ClipboardOwnerWindow->head.h;
     }
 
     UserLeave();
@@ -805,7 +790,7 @@ NtUserGetClipboardOwner(VOID)
     return ret;
 }
 
-HWND STDCALL
+HWND APIENTRY
 NtUserGetClipboardViewer(VOID)
 {
     HWND ret = NULL;
@@ -814,7 +799,7 @@ NtUserGetClipboardViewer(VOID)
 
     if (WindowsChain)
     {
-        ret = WindowsChain->window->hSelf;
+        ret = WindowsChain->window->head.h;
     }
 
     UserLeave();
@@ -822,7 +807,7 @@ NtUserGetClipboardViewer(VOID)
     return ret;
 }
 
-INT STDCALL
+INT APIENTRY
 NtUserGetPriorityClipboardFormat(UINT *paFormatPriorityList, INT cFormats)
 {
     INT i;
@@ -831,7 +816,7 @@ NtUserGetPriorityClipboardFormat(UINT *paFormatPriorityList, INT cFormats)
 
     UserEnterExclusive();
 
-    _SEH_TRY
+    _SEH2_TRY
     {
         if (IntCountClipboardFormats() == 0)
         {
@@ -856,11 +841,11 @@ NtUserGetPriorityClipboardFormat(UINT *paFormatPriorityList, INT cFormats)
 
         }
     }
-    _SEH_HANDLE
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
     {
-        SetLastNtError(_SEH_GetExceptionCode());
+        SetLastNtError(_SEH2_GetExceptionCode());
     }
-    _SEH_END;
+    _SEH2_END;
 
     UserLeave();
 
@@ -868,7 +853,7 @@ NtUserGetPriorityClipboardFormat(UINT *paFormatPriorityList, INT cFormats)
 
 }
 
-BOOL STDCALL
+BOOL APIENTRY
 NtUserIsClipboardFormatAvailable(UINT format)
 {
     BOOL ret = FALSE;
@@ -884,7 +869,7 @@ NtUserIsClipboardFormatAvailable(UINT format)
 
 
 
-HANDLE STDCALL
+HANDLE APIENTRY
 NtUserSetClipboardData(UINT uFormat, HANDLE hMem, DWORD size)
 {
     HANDLE hCBData = NULL;
@@ -914,16 +899,16 @@ NtUserSetClipboardData(UINT uFormat, HANDLE hMem, DWORD size)
 
         if (hMem)
         {
-            _SEH_TRY
+            _SEH2_TRY
             {
                 ProbeForRead(hMem, size, 1);
             }
-            _SEH_HANDLE
+            _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
             {
-                SetLastNtError(_SEH_GetExceptionCode());
-                _SEH_YIELD(goto exit_setCB);
+                SetLastNtError(_SEH2_GetExceptionCode());
+                _SEH2_YIELD(goto exit_setCB);
             }
-            _SEH_END;
+            _SEH2_END;
 
             if (intIsClipboardOpenByMe())
             {
@@ -932,7 +917,7 @@ NtUserSetClipboardData(UINT uFormat, HANDLE hMem, DWORD size)
 
             if (!canSinthesize(uFormat))
             {
-                hCBData = ExAllocatePool(PagedPool, size);
+                hCBData = ExAllocatePoolWithTag(PagedPool, size, USERTAG_CLIPBOARD);
                 memcpy(hCBData, hMem, size);
                 intAddFormatedData(uFormat, hCBData, size);
                 DPRINT1("Data stored\n");
@@ -949,7 +934,9 @@ NtUserSetClipboardData(UINT uFormat, HANDLE hMem, DWORD size)
                     {
                         //TODO : sinthesize CF_UNICODETEXT & CF_OEMTEXT
                         // CF_TEXT -> CF_UNICODETEXT
-                        RtlAnsiStringToUnicodeString(&unicodeString, hCBData, TRUE);
+                        ansiString.Buffer = hCBData;
+                        ansiString.Length = size;
+                        RtlAnsiStringToUnicodeString(&unicodeString, &ansiString, TRUE);
                         intAddFormatedData(CF_UNICODETEXT, unicodeString.Buffer, unicodeString.Length * sizeof(WCHAR));
                         // CF_TEXT -> CF_OEMTEXT
                         RtlUnicodeStringToOemString(&oemString, &unicodeString, TRUE);
@@ -990,19 +977,18 @@ NtUserSetClipboardData(UINT uFormat, HANDLE hMem, DWORD size)
                     // because pallette information may change
 
                     HDC hdc;
-                    INT ret;
                     BITMAP bm;
                     BITMAPINFO bi;
-                    BITMAPOBJ *BitmapObj;
+                    SURFACE *psurf;
 
                     hdc = UserGetDCEx(NULL, NULL, DCX_USESTYLE);
 
 
-                    BitmapObj = BITMAPOBJ_LockBitmap(hMem);
-                    BITMAP_GetObject(BitmapObj, sizeof(BITMAP), (LPSTR)&bm);
-                    if(BitmapObj)
+                    psurf = SURFACE_LockSurface(hMem);
+                    BITMAP_GetObject(psurf, sizeof(BITMAP), (PVOID)&bm);
+                    if(psurf)
                     {
-                        BITMAPOBJ_UnlockBitmap(BitmapObj);
+                        SURFACE_UnlockSurface(psurf);
                     }
 
                     bi.bmiHeader.biSize        = sizeof(BITMAPINFOHEADER);
@@ -1016,14 +1002,14 @@ NtUserSetClipboardData(UINT uFormat, HANDLE hMem, DWORD size)
                     bi.bmiHeader.biYPelsPerMeter = 0;
                     bi.bmiHeader.biClrUsed = 0;
 
-                    ret = NtGdiGetDIBitsInternal(hdc, hMem, 0, bm.bmHeight,  NULL, &bi, DIB_RGB_COLORS, 0, 0);
+                    NtGdiGetDIBitsInternal(hdc, hMem, 0, bm.bmHeight,  NULL, &bi, DIB_RGB_COLORS, 0, 0);
 
                     size = bi.bmiHeader.biSizeImage + sizeof(BITMAPINFOHEADER);
 
-                    hCBData = ExAllocatePool(PagedPool, size);
+                    hCBData = ExAllocatePoolWithTag(PagedPool, size, USERTAG_CLIPBOARD);
                     memcpy(hCBData, &bi, sizeof(BITMAPINFOHEADER));
 
-                    ret = NtGdiGetDIBitsInternal(hdc, hMem, 0, bm.bmHeight, (LPBYTE)hCBData + sizeof(BITMAPINFOHEADER), &bi, DIB_RGB_COLORS, 0, 0);
+                    NtGdiGetDIBitsInternal(hdc, hMem, 0, bm.bmHeight, (LPBYTE)hCBData + sizeof(BITMAPINFOHEADER), &bi, DIB_RGB_COLORS, 0, 0);
 
                     UserReleaseDC(NULL, hdc, FALSE);
 
@@ -1074,12 +1060,12 @@ exit_setCB:
     return hMem;
 }
 
-HWND STDCALL
+HWND APIENTRY
 NtUserSetClipboardViewer(HWND hWndNewViewer)
 {
     HWND ret = NULL;
     PCLIPBOARDCHAINELEMENT newWC = NULL;
-    PWINDOW_OBJECT window;
+    PWND window;
 
     UserEnterExclusive();
 
@@ -1095,7 +1081,7 @@ NtUserSetClipboardViewer(HWND hWndNewViewer)
                 if (newWC->next)
                 {
                     // return the next HWND available window in the chain
-                    ret = newWC->next->window->hSelf;
+                    ret = newWC->next->window->head.h;
                 }
             }
         }
@@ -1106,13 +1092,11 @@ NtUserSetClipboardViewer(HWND hWndNewViewer)
     return ret;
 }
 
-UINT STDCALL
-NtUserEnumClipboardFormats(UINT uFormat)
+UINT APIENTRY
+IntEnumClipboardFormats(UINT uFormat)
 {
     UINT ret = 0;
 
-    UserEnterShared();
-
     if (intIsClipboardOpenByMe())
     {
             if (uFormat == 0)
@@ -1162,8 +1146,6 @@ NtUserEnumClipboardFormats(UINT uFormat)
         SetLastWin32Error(ERROR_CLIPBOARD_NOT_OPEN);
     }
 
-    UserLeave();
-
     return ret;
 }
 
@@ -1174,14 +1156,16 @@ NtUserEnumClipboardFormats(UINT uFormat)
 VOID FASTCALL
 IntIncrementSequenceNumber(VOID)
 {
+    PTHREADINFO pti;
+    PWINSTATION_OBJECT WinStaObj;
 
-    USE_WINSTA
-
-    WINSTA_ClipboardSequenceNumber++;
+    pti = PsGetCurrentThreadWin32Thread();
+    WinStaObj = pti->rpdesk->rpwinstaParent;
 
+    WinStaObj->Clipboard->ClipboardSequenceNumber++;
 }
 
-DWORD STDCALL
+DWORD APIENTRY
 NtUserGetClipboardSequenceNumber(VOID)
 {
     //windowstation sequence number
@@ -1195,7 +1179,7 @@ NtUserGetClipboardSequenceNumber(VOID)
 
     WinSta = UserGetProcessWindowStation();
 
-    Status = IntValidateWindowStationHandle(WinSta, UserMode, WINSTA_ACCESSCLIPBOARD, &WinStaObj);
+    Status = IntValidateWindowStationHandle(WinSta, KernelMode, WINSTA_ACCESSCLIPBOARD, &WinStaObj);
 
     if (!NT_SUCCESS(Status))
     {
@@ -1217,7 +1201,7 @@ NtUserGetClipboardSequenceNumber(VOID)
 
 /**************** VISTA FUNCTIONS******************/
 
-BOOL STDCALL NtUserAddClipboardFormatListener(
+BOOL APIENTRY NtUserAddClipboardFormatListener(
     HWND hwnd
 )
 {
@@ -1225,7 +1209,7 @@ BOOL STDCALL NtUserAddClipboardFormatListener(
     return FALSE;
 }
 
-BOOL STDCALL NtUserRemoveClipboardFormatListener(
+BOOL APIENTRY NtUserRemoveClipboardFormatListener(
     HWND hwnd
 )
 {
@@ -1233,7 +1217,7 @@ BOOL STDCALL NtUserRemoveClipboardFormatListener(
     return FALSE;
 }
 
-BOOL STDCALL NtUserGetUpdatedClipboardFormats(
+BOOL APIENTRY NtUserGetUpdatedClipboardFormats(
     PUINT lpuiFormats,
     UINT cFormats,
     PUINT pcFormatsOut