Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.cpp	(revision 38331)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.cpp	(revision 38363)
@@ -1767,9 +1767,7 @@
         pSwapchain->pSwapChainIf->Release();
         Assert(pSwapchain->hWnd);
-        HRESULT hr = VBoxDispWndDestroy(pDevice->pAdapter, pSwapchain->hWnd);
-        Assert(hr == S_OK);
         pSwapchain->pSwapChainIf = NULL;
         pSwapchain->hWnd = NULL;
-        return hr;
+        return S_OK;
     }
 
@@ -2297,18 +2295,11 @@
     {
 //#define VBOXDISP_NEWWND_ON_SWAPCHAINUPDATE
-#ifndef VBOXDISP_NEWWND_ON_SWAPCHAINUPDATE
-        if (!hOldWnd)
-#endif
-        {
-            hr = VBoxDispWndCreate(pAdapter, Params.BackBufferWidth, Params.BackBufferHeight, &pSwapchain->hWnd);
-            Assert(hr == S_OK);
-        }
         if (hr == S_OK)
         {
             DWORD fFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
-            if (pDevice->fFlags.AllowMultithreading)
-                fFlags |= D3DCREATE_MULTITHREADED;
-
-            Params.hDeviceWindow = pSwapchain->hWnd;
+//            if (pDevice->fFlags.AllowMultithreading)
+//                fFlags |= D3DCREATE_MULTITHREADED;
+
+            Params.hDeviceWindow = NULL;
                         /* @todo: it seems there should be a way to detect this correctly since
                          * our vboxWddmDDevSetDisplayMode will be called in case we are using full-screen */
@@ -2321,8 +2312,10 @@
             if (!pDevice->pDevice9If)
             {
-                hr = pAdapter->pD3D9If->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, pSwapchain->hWnd, fFlags, &Params, &pDevice9If);
+                hr = pAdapter->pD3D9If->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, NULL, fFlags, &Params, &pDevice9If);
                 Assert(hr == S_OK);
                 if (hr == S_OK)
                 {
+                    Assert(Params.hDeviceWindow);
+                    pSwapchain->hWnd = Params.hDeviceWindow;
                     pDevice->pDevice9If = pDevice9If;
                     hr = pDevice9If->GetSwapChain(0, &pNewIf);
@@ -2410,4 +2403,6 @@
                 if (hr == S_OK)
                 {
+                    Assert(Params.hDeviceWindow);
+                    pSwapchain->hWnd = Params.hDeviceWindow;
                     Assert(pNewIf);
                 }
@@ -2496,8 +2491,4 @@
                         Assert(hOldWnd);
                         pOldIf->Release();
-                        if (hOldWnd != pSwapchain->hWnd)
-                        {
-                            VBoxDispWndDestroy(pAdapter, hOldWnd);
-                        }
                     }
                     else
@@ -2513,10 +2504,5 @@
 
         Assert(hr != S_OK);
-        if (hOldWnd != pSwapchain->hWnd)
-        {
-            HRESULT tmpHr = VBoxDispWndDestroy(pAdapter, pSwapchain->hWnd);
-            Assert(tmpHr == S_OK);
-            pSwapchain->hWnd = hOldWnd;
-        }
+        pSwapchain->hWnd = hOldWnd;
     }
 
@@ -5326,10 +5312,4 @@
     if (VBOXDISPMODE_IS_3D(pAdapter))
     {
-//        if (pRc->RcDesc.fFlags.RenderTarget)
-//        {
-//            Assert(pDevice->hWnd);
-//            Assert(pDevice->pDevice9If);
-//        }
-
         for (UINT i = 0; i < pRc->cAllocations; ++i)
         {
@@ -5498,5 +5478,4 @@
         }
 #endif
-#if 1
         PVBOXWDDMDISP_RESOURCE pRc = (PVBOXWDDMDISP_RESOURCE)pData->hSrcResource;
         Assert(pRc);
@@ -5505,77 +5484,7 @@
         hr = vboxWddmSwapchainPresent(pDevice, pAlloc);
         Assert(hr == S_OK);
-#else
-        PVBOXWDDMDISP_RESOURCE pRc = (PVBOXWDDMDISP_RESOURCE)pData->hSrcResource;
-        Assert(pRc);
-        PVBOXWDDMDISP_SCREEN pScreen = &pDevice->aScreens[pRc->RcDesc.VidPnSourceId];
-        Assert(pScreen->hWnd);
-        Assert(pScreen->pDevice9If);
-        Assert(pRc == pScreen->pRenderTargetRc);
-#if 1
-        VBOXVDBG_RTGT_STATECHECK(pDevice);
-
-        if (pRc->RcDesc.VidPnSourceId != pDevice->iPrimaryScreen)
-        {
-            PVBOXWDDMDISP_ALLOCATION pAlloc = &pRc->aAllocations[pData->SrcSubResourceIndex];
-            PVBOXWDDMDISP_SCREEN pPrimaryScreen = &pDevice->aScreens[pDevice->iPrimaryScreen];
-            Assert(pPrimaryScreen->pDevice9If);
-            IDirect3DSurface9 *pSecondaryRt;
-            Assert(pAlloc->enmD3DIfType == VBOXDISP_D3DIFTYPE_SURFACE);
-            IDirect3DSurface9 *pDataRt = (IDirect3DSurface9*)pAlloc->pSecondaryOpenedD3DIf;
-            Assert(pDataRt);
-            hr = pDevice->pAdapter->D3D.pfnVBoxWineExD3DDev9Flush((IDirect3DDevice9Ex*)pPrimaryScreen->pDevice9If);
-            Assert(hr == S_OK);
-            if (hr == S_OK)
-            {
-                hr = pScreen->pDevice9If->GetRenderTarget(0, &pSecondaryRt);
-                Assert(hr == S_OK);
-                if (hr == S_OK)
-                {
-                    hr = pScreen->pDevice9If->StretchRect(pDataRt,
-                                        NULL,
-                                        pSecondaryRt,
-                                        NULL,
-                                        D3DTEXF_NONE);
-                    pSecondaryRt->Release();
-                }
-            }
-        }
-
-        hr = pScreen->pDevice9If->Present(NULL, /* CONST RECT * pSourceRect */
-                NULL, /* CONST RECT * pDestRect */
-                NULL, /* HWND hDestWindowOverride */
-                NULL /*CONST RGNDATA * pDirtyRegion */
-                );
-        Assert(hr == S_OK);
-#endif
-#endif
-    }
-#if 0
-    else
-#endif
-    {
-//        if (pData->Flags.Flip)
-//        {
-//            Assert(pData->hSrcResource);
-//            PVBOXWDDMDISP_RESOURCE pRc = (PVBOXWDDMDISP_RESOURCE)pData->hSrcResource;
-//            PVBOXWDDMDISP_SCREEN pScreen = &pDevice->aScreens[pRc->RcDesc.VidPnSourceId];
-//            Assert(pScreen->hWnd);
-//            Assert(pScreen->pDevice9If);
-//            Assert(pScreen->pRenderTargetRc == pRc);
-//            Assert(pRc->cAllocations >= 2);
-//            Assert(pRc->aAllocations[0].enmD3DIfType == VBOXDISP_D3DIFTYPE_SURFACE);
-//            Assert(pRc->RcDesc.fFlags.RenderTarget);
-//            uint32_t iNewRTFB = (pScreen->iRenderTargetFrontBuf + 1) % pRc->cAllocations;
-//
-//            Assert(pScreen->iRenderTargetFrontBuf != iNewRTFB);
-//            Assert(pData->SrcSubResourceIndex == iNewRTFB);
-//
-//            vboxWddmRenderTargetUpdate(pDevice, pRc, iNewRTFB);
-//
-//            /* assign a new frontbuffer index */
-//            pScreen->iRenderTargetFrontBuf = iNewRTFB;
-//
-//            VBOXVDBG_RTGT_STATECHECK(pDevice);
-//        }
+    }
+
+    {
         D3DDDICB_PRESENT DdiPresent = {0};
         if (pData->hSrcResource)
@@ -5596,6 +5505,4 @@
         }
         DdiPresent.hContext = pDevice->DefaultContext.ContextInfo.hContext;
-//        DdiPresent.BroadcastContextCount;
-//        DdiPresent.BroadcastContext[D3DDDI_MAX_BROADCAST_CONTEXT];
 
         hr = pDevice->RtCallbacks.pfnPresentCb(pDevice->hDevice, &DdiPresent);
@@ -7215,6 +7122,4 @@
     {
         VBOXDISPCRHGSMI_SCOPE_SET_GLOBAL();
-        HRESULT hr = VBoxDispWorkerDestroy(&pAdapter->WndWorker);
-        Assert(hr == S_OK);
         pAdapter->pD3D9If->Release();
         VBoxDispD3DClose(&pAdapter->D3D);
@@ -7329,11 +7234,6 @@
                                 Assert(pAdapter->cMaxSimRTs);
                                 Assert(pAdapter->cMaxSimRTs < UINT32_MAX/2);
-                                hr = VBoxDispWorkerCreate(&pAdapter->WndWorker);
-                                Assert(hr == S_OK);
-                                if (hr == S_OK)
-                                {
-                                    vboxVDbgPrint((__FUNCTION__": SUCCESS 3D Enabled, pAdapter (0x%p)\n", pAdapter));
-                                    break;
-                                }
+                                vboxVDbgPrint((__FUNCTION__": SUCCESS 3D Enabled, pAdapter (0x%p)\n", pAdapter));
+                                break;
                             }
                             pAdapter->pD3D9If->Release();
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.h	(revision 38331)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.h	(revision 38363)
@@ -56,5 +56,4 @@
     UINT uRtVersion;
     VBOXDISPD3D D3D;
-    VBOXDISPWORKER WndWorker;
     IDirect3D9Ex * pD3D9If;
     D3DDDI_ADAPTERCALLBACKS RtCallbacks;
@@ -148,4 +147,8 @@
 #endif
     IDirect3DSwapChain9 *pSwapChainIf;
+    /* a read-only hWnd we receive from wine
+     * we use it for visible region notifications only,
+     * it MUST NOT be destroyed on swapchain destruction,
+     * wine will handle that for us */
     HWND hWnd;
     VBOXDISP_KMHANDLE hSwapchainKm;
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DIf.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DIf.cpp	(revision 38331)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DIf.cpp	(revision 38363)
@@ -81,309 +81,2 @@
     return E_FAIL;
 }
-
-#define WM_VBOXDISP_CALLPROC (WM_APP+1)
-
-typedef struct VBOXDISP_CALLPROC
-{
-    PFNVBOXDISPWORKERCB pfnCb;
-    void *pvCb;
-} VBOXDISP_CALLPROC;
-
-static DWORD WINAPI vboxDispWorkerThread(void *pvUser)
-{
-    VBOXDISPWORKER *pWorker = (VBOXDISPWORKER*)pvUser;
-    MSG Msg;
-
-    PeekMessage(&Msg,
-        NULL /* HWND hWnd */,
-        WM_USER /* UINT wMsgFilterMin */,
-        WM_USER /* UINT wMsgFilterMax */,
-        PM_NOREMOVE);
-    RTSemEventSignal(pWorker->hEvent);
-
-    do
-    {
-        BOOL bResult = GetMessage(&Msg,
-            0 /*HWND hWnd*/,
-            0 /*UINT wMsgFilterMin*/,
-            0 /*UINT wMsgFilterMax*/
-            );
-
-        if(!bResult) /* WM_QUIT was posted */
-            break;
-
-        Assert(bResult != -1);
-        if(bResult == -1) /* error occurred */
-            break;
-
-        switch (Msg.message)
-        {
-            case WM_VBOXDISP_CALLPROC:
-            {
-                VBOXDISP_CALLPROC* pData = (VBOXDISP_CALLPROC*)Msg.lParam;
-                pData->pfnCb(pData->pvCb);
-                RTSemEventSignal(pWorker->hEvent);
-                break;
-            }
-            default:
-                TranslateMessage(&Msg);
-                DispatchMessage(&Msg);
-        }
-    } while (1);
-    return 0;
-}
-
-static int vboxDispWorkerSubmit(VBOXDISPWORKER *pWorker, UINT Msg, LPARAM lParam)
-{
-    /* need to serialize since vboxDispWorkerThread is using one pWorker->hEvent
-     * to signal job completion */
-    int rc = RTCritSectEnter(&pWorker->CritSect);
-    AssertRC(rc);
-    if (RT_SUCCESS(rc))
-    {
-        BOOL bResult = PostThreadMessage(pWorker->idThread, Msg, 0, lParam);
-        Assert(bResult);
-        if (bResult)
-        {
-            rc = RTSemEventWait(pWorker->hEvent, RT_INDEFINITE_WAIT);
-            AssertRC(rc);
-        }
-        else
-            rc = VERR_GENERAL_FAILURE;
-
-        int tmpRc = RTCritSectLeave(&pWorker->CritSect);
-        AssertRC(tmpRc);
-    }
-    return rc;
-}
-
-HRESULT VBoxDispWorkerSubmitProc(VBOXDISPWORKER *pWorker, PFNVBOXDISPWORKERCB pfnCb, void *pvCb)
-{
-    VBOXDISP_CALLPROC Ctx;
-    Ctx.pfnCb = pfnCb;
-    Ctx.pvCb = pvCb;
-    int rc =  vboxDispWorkerSubmit(pWorker, WM_VBOXDISP_CALLPROC, (LPARAM)&Ctx);
-    AssertRC(rc);
-    return RT_SUCCESS(rc) ? S_OK : E_FAIL;
-}
-
-HRESULT VBoxDispWorkerCreate(VBOXDISPWORKER *pWorker)
-{
-    int rc = RTCritSectInit(&pWorker->CritSect);
-    AssertRC(rc);
-    if (RT_SUCCESS(rc))
-    {
-        rc = RTSemEventCreate(&pWorker->hEvent);
-        AssertRC(rc);
-        if (RT_SUCCESS(rc))
-        {
-            pWorker->hThread = CreateThread(
-                                  NULL /* LPSECURITY_ATTRIBUTES lpThreadAttributes */,
-                                  0 /* SIZE_T dwStackSize */,
-                                  vboxDispWorkerThread,
-                                  pWorker,
-                                  0 /* DWORD dwCreationFlags */,
-                                  &pWorker->idThread);
-            Assert(pWorker->hThread);
-            if (pWorker->hThread)
-            {
-                rc = RTSemEventWait(pWorker->hEvent, RT_INDEFINITE_WAIT);
-                AssertRC(rc);
-                if (RT_SUCCESS(rc))
-                    return S_OK;
-                /* destroy thread ? */
-            }
-            else
-            {
-                DWORD winErr = GetLastError();
-                vboxVDbgPrintR((__FUNCTION__": CreateThread failed, winErr = (%d)", winErr));
-                rc = VERR_GENERAL_FAILURE;
-            }
-            int tmpRc = RTSemEventDestroy(pWorker->hEvent);
-            AssertRC(tmpRc);
-        }
-        int tmpRc = RTCritSectDelete(&pWorker->CritSect);
-        AssertRC(tmpRc);
-    }
-    return E_FAIL;
-}
-
-HRESULT VBoxDispWorkerDestroy(VBOXDISPWORKER *pWorker)
-{
-    int rc = VINF_SUCCESS;
-    BOOL bResult = PostThreadMessage(pWorker->idThread, WM_QUIT, 0, 0);
-    Assert(bResult);
-    if (bResult)
-    {
-        DWORD dwErr = WaitForSingleObject(pWorker->hThread, INFINITE);
-        Assert(dwErr == WAIT_OBJECT_0);
-        if (dwErr == WAIT_OBJECT_0)
-        {
-            rc = RTSemEventDestroy(pWorker->hEvent);
-            AssertRC(rc);
-            if (RT_SUCCESS(rc))
-            {
-                rc = RTCritSectDelete(&pWorker->CritSect);
-                AssertRC(rc);
-            }
-        }
-        else
-            rc = VERR_GENERAL_FAILURE;
-    }
-    else
-        rc = VERR_GENERAL_FAILURE;
-
-    return RT_SUCCESS(rc) ? S_OK : E_FAIL;
-}
-
-static LRESULT CALLBACK WindowProc(HWND hwnd,
-    UINT uMsg,
-    WPARAM wParam,
-    LPARAM lParam
-)
-{
-    switch(uMsg)
-    {
-        case WM_CLOSE:
-            vboxVDbgPrint((__FUNCTION__": got WM_CLOSE for hwnd(0x%x)", hwnd));
-            return 0;
-        case WM_DESTROY:
-            vboxVDbgPrint((__FUNCTION__": got WM_DESTROY for hwnd(0x%x)", hwnd));
-            return 0;
-        case WM_NCHITTEST:
-            vboxVDbgPrint((__FUNCTION__": got WM_NCHITTEST for hwnd(0x%x)\n", hwnd));
-            return HTNOWHERE;
-    }
-
-    return DefWindowProc(hwnd, uMsg, wParam, lParam);
-}
-
-#define VBOXDISPWND_NAME L"VboxDispD3DWindow"
-
-HRESULT vboxDispWndDoCreate(DWORD w, DWORD h, HWND *phWnd)
-{
-    HRESULT hr = S_OK;
-    HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
-    /* Register the Window Class. */
-    WNDCLASS wc;
-    if (!GetClassInfo(hInstance, VBOXDISPWND_NAME, &wc))
-    {
-        wc.style = 0;//CS_OWNDC;
-        wc.lpfnWndProc = WindowProc;
-        wc.cbClsExtra = 0;
-        wc.cbWndExtra = 0;
-        wc.hInstance = hInstance;
-        wc.hIcon = NULL;
-        wc.hCursor = NULL;
-        wc.hbrBackground = NULL;
-        wc.lpszMenuName = NULL;
-        wc.lpszClassName = VBOXDISPWND_NAME;
-        if (!RegisterClass(&wc))
-        {
-            DWORD winErr = GetLastError();
-            vboxVDbgPrint((__FUNCTION__": RegisterClass failed, winErr(%d)\n", winErr));
-            hr = E_FAIL;
-        }
-    }
-
-    if (hr == S_OK)
-    {
-        HWND hWnd = CreateWindowEx (WS_EX_TOOLWINDOW,
-                                        VBOXDISPWND_NAME, VBOXDISPWND_NAME,
-                                        WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED,
-                                        0, 0,
-                                        w, h,
-                                        NULL, //GetDesktopWindow() /* hWndParent */,
-                                        NULL /* hMenu */,
-                                        hInstance,
-                                        NULL /* lpParam */);
-        Assert(hWnd);
-        if (hWnd)
-        {
-            *phWnd = hWnd;
-        }
-        else
-        {
-            DWORD winErr = GetLastError();
-            vboxVDbgPrint((__FUNCTION__": CreateWindowEx failed, winErr(%d)\n", winErr));
-            hr = E_FAIL;
-        }
-    }
-
-    return hr;
-}
-
-static HRESULT vboxDispWndDoDestroy(HWND hWnd)
-{
-    BOOL bResult = DestroyWindow(hWnd);
-    Assert(bResult);
-    if (bResult)
-        return S_OK;
-
-    DWORD winErr = GetLastError();
-    vboxVDbgPrint((__FUNCTION__": DestroyWindow failed, winErr(%d) for hWnd(0x%x)\n", winErr, hWnd));
-
-    return E_FAIL;
-}
-
-typedef struct VBOXDISPWND_CREATE_INFO
-{
-    int hr;
-    HWND hWnd;
-    DWORD width;
-    DWORD height;
-} VBOXDISPWND_CREATE_INFO;
-
-typedef struct VBOXDISPWND_DESTROY_INFO
-{
-    int hr;
-    HWND hWnd;
-} VBOXDISPWND_DESTROY_INFO;
-
-DECLCALLBACK(void) vboxDispWndDestroyWorker(void *pvUser)
-{
-    VBOXDISPWND_DESTROY_INFO *pInfo = (VBOXDISPWND_DESTROY_INFO*)pvUser;
-    pInfo->hr = vboxDispWndDoDestroy(pInfo->hWnd);
-    Assert(pInfo->hr == S_OK);
-}
-
-DECLCALLBACK(void) vboxDispWndCreateWorker(void *pvUser)
-{
-    VBOXDISPWND_CREATE_INFO *pInfo = (VBOXDISPWND_CREATE_INFO*)pvUser;
-    pInfo->hr = vboxDispWndDoCreate(pInfo->width, pInfo->height, &pInfo->hWnd);
-    Assert(pInfo->hr == S_OK);
-}
-
-
-HRESULT VBoxDispWndDestroy(PVBOXWDDMDISP_ADAPTER pAdapter, HWND hWnd)
-{
-    VBOXDISPWND_DESTROY_INFO Info;
-    Info.hr = E_FAIL;
-    Info.hWnd = hWnd;
-    HRESULT hr = VBoxDispWorkerSubmitProc(&pAdapter->WndWorker, vboxDispWndDestroyWorker, &Info);
-    Assert(hr == S_OK);
-    if (hr == S_OK)
-    {
-        Assert(Info.hr == S_OK);
-        return Info.hr;
-    }
-    return hr;
-}
-
-HRESULT VBoxDispWndCreate(PVBOXWDDMDISP_ADAPTER pAdapter, DWORD width, DWORD height, HWND *phWnd)
-{
-    VBOXDISPWND_CREATE_INFO Info;
-    Info.hr = E_FAIL;
-    Info.width = width;
-    Info.height = height;
-    HRESULT hr = VBoxDispWorkerSubmitProc(&pAdapter->WndWorker, vboxDispWndCreateWorker, &Info);
-    Assert(hr == S_OK);
-    if (hr == S_OK)
-    {
-        Assert(Info.hr == S_OK);
-        if (Info.hr == S_OK)
-            *phWnd = Info.hWnd;
-        return Info.hr;
-    }
-    return hr;
-}
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DIf.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DIf.h	(revision 38331)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DIf.h	(revision 38363)
@@ -53,27 +53,3 @@
 void VBoxDispD3DClose(VBOXDISPD3D *pD3D);
 
-
-typedef struct VBOXDISPWORKER
-{
-    RTCRITSECT CritSect;
-
-    RTSEMEVENT hEvent;
-
-    HANDLE hThread;
-    DWORD  idThread;
-} VBOXDISPWORKER;
-
-HRESULT VBoxDispWorkerCreate(VBOXDISPWORKER *pWorker);
-HRESULT VBoxDispWorkerDestroy(VBOXDISPWORKER *pWorker);
-
-typedef DECLCALLBACK(void) FNVBOXDISPWORKERCB(void *pvUser);
-typedef FNVBOXDISPWORKERCB *PFNVBOXDISPWORKERCB;
-
-HRESULT VBoxDispWorkerSubmitProc(VBOXDISPWORKER *pWorker, PFNVBOXDISPWORKERCB pfnCb, void *pvCb);
-
-typedef struct VBOXWDDMDISP_ADAPTER *PVBOXWDDMDISP_ADAPTER;
-
-HRESULT VBoxDispWndDestroy(PVBOXWDDMDISP_ADAPTER pAdapter, HWND hWnd);
-HRESULT VBoxDispWndCreate(PVBOXWDDMDISP_ADAPTER pAdapter, DWORD width, DWORD height, HWND *phWnd);
-
 #endif /* ifndef ___VBoxDispD3DIf_h___ */
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d9/device.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d9/device.c	(revision 38331)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d9/device.c	(revision 38363)
@@ -281,5 +281,7 @@
 
       IWineD3DDevice_Uninit3D(This->WineD3DDevice, D3D9CB_DestroySwapChain);
+#ifndef VBOX_WITH_WDDM
       IWineD3DDevice_ReleaseFocusWindow(This->WineD3DDevice);
+#endif
       IWineD3DDevice_Release(This->WineD3DDevice);
       wined3d_mutex_unlock();
@@ -3044,4 +3046,5 @@
     }
 
+#ifndef VBOX_WITH_WDDM
     if (!parameters->Windowed)
     {
@@ -3055,4 +3058,5 @@
         }
     }
+#endif
 
     if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
@@ -3099,5 +3103,7 @@
     {
         WARN("Failed to initialize 3D, hr %#x.\n", hr);
+#ifndef VBOX_WITH_WDDM
         IWineD3DDevice_ReleaseFocusWindow(device->WineD3DDevice);
+#endif
         HeapFree(GetProcessHeap(), 0, wined3d_parameters);
         IWineD3DDevice_Release(device->WineD3DDevice);
@@ -3136,5 +3142,7 @@
         wined3d_mutex_lock();
         IWineD3DDevice_Uninit3D(device->WineD3DDevice, D3D9CB_DestroySwapChain);
+#ifndef VBOX_WITH_WDDM
         IWineD3DDevice_ReleaseFocusWindow(device->WineD3DDevice);
+#endif
         IWineD3DDevice_Release(device->WineD3DDevice);
         wined3d_mutex_unlock();
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/device.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/device.c	(revision 38331)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/device.c	(revision 38363)
@@ -1548,4 +1548,5 @@
 static HRESULT WINAPI IWineD3DDeviceImpl_AcquireFocusWindow(IWineD3DDevice *iface, HWND window)
 {
+#ifndef VBOX_WITH_WDDM
     IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)iface;
 
@@ -1558,5 +1559,7 @@
     device->focus_window = window;
     SetForegroundWindow(window);
-
+#else
+    ERR("should not be here");
+#endif
     return WINED3D_OK;
 }
@@ -1564,8 +1567,12 @@
 static void WINAPI IWineD3DDeviceImpl_ReleaseFocusWindow(IWineD3DDevice *iface)
 {
+#ifndef VBOX_WITH_WDDM
     IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)iface;
 
     if (device->focus_window) wined3d_unregister_window(device->focus_window);
     device->focus_window = NULL;
+#else
+    ERR("should not be here");
+#endif
 }
 
@@ -7262,4 +7269,5 @@
 }
 
+#ifndef VBOX_WITH_WDDM
 LRESULT device_process_message(IWineD3DDeviceImpl *device, HWND window,
         UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
@@ -7283,2 +7291,3 @@
     return CallWindowProcW(proc, window, message, wparam, lparam);
 }
+#endif
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/swapchain.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/swapchain.c	(revision 38331)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/swapchain.c	(revision 38363)
@@ -89,5 +89,5 @@
     }
 #else
-    IWineD3DDevice_RemoveSwapChain(This->device, This);
+    IWineD3DDevice_RemoveSwapChain((IWineD3DDevice*)This->device, (IWineD3DSwapChain*)This);
     if (!This->device->NumberOfSwapChains)
 #endif
@@ -109,5 +109,9 @@
     if(This->device_window) {
         /* see VBoxExtGet/ReleaseDC for comments */
-        VBoxExtReleaseDC(This->device_window, This->hDC);
+        VBoxExtWndDestroy(This->device_window, This->hDC);
+    }
+    else
+    {
+        ERR("null device window");
     }
 #else
@@ -692,5 +696,8 @@
     WINED3DDISPLAYMODE mode;
     RECT client_rect;
-    HWND window;
+    HWND window = NULL;
+#ifdef VBOX_WITH_WDDM
+    HDC hDC = NULL;
+#endif
     HRESULT hr;
     UINT i;
@@ -724,5 +731,22 @@
     }
 
+#ifdef VBOX_WITH_WDDM
+    if (present_parameters->hDeviceWindow)
+    {
+        ERR("non-null device window");
+        return E_FAIL;
+    }
+    hr = VBoxExtWndCreate(present_parameters->BackBufferWidth, present_parameters->BackBufferHeight, &window, &hDC);
+    if (FAILED(hr))
+    {
+        ERR("VBoxExtWndCreate failed, hr 0x%x", hr);
+        return hr;
+    }
+    Assert(window);
+    Assert(hDC);
+    present_parameters->hDeviceWindow = window;
+#else
     window = present_parameters->hDeviceWindow ? present_parameters->hDeviceWindow : device->createParms.hFocusWindow;
+#endif
 
     swapchain->device = device;
@@ -733,13 +757,5 @@
 #ifdef VBOX_WITH_WDDM
     Assert(window);
-    /* see VBoxExtGet/ReleaseDC for comments */
-    swapchain->hDC = VBoxExtGetDC(window);
-    if (!swapchain->hDC)
-    {
-        DWORD winEr = GetLastError();
-        WARN("Failed to get a window DC, winEr %d.\n", winEr);
-        Assert(0);
-        goto err;
-    }
+    swapchain->hDC = hDC;
 #endif
 
@@ -848,5 +864,7 @@
     if (surface_type == SURFACE_OPENGL)
     {
+#ifdef VBOX_WITH_WDDM
         struct wined3d_context * swapchainContext;
+#endif
         const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
 
@@ -999,4 +1017,11 @@
     if (swapchain->frontBuffer) IWineD3DSurface_Release(swapchain->frontBuffer);
 
+#ifdef VBOX_WITH_WDDM
+    if (swapchain->device_window)
+    {
+        VBoxExtWndDestroy(swapchain->device_window, swapchain->hDC);
+    }
+#endif
+
     return hr;
 }
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/vboxext.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/vboxext.c	(revision 38331)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/vboxext.c	(revision 38363)
@@ -26,4 +26,9 @@
 
 /*******************************/
+#if defined(VBOX_WDDM_WOW64)
+# define VBOXEXT_WINE_MODULE_NAME "wined3dwddm-x86.dll"
+#else
+# define VBOXEXT_WINE_MODULE_NAME "wined3dwddm.dll"
+#endif
 
 typedef struct VBOXEXT_WORKER
@@ -35,4 +40,8 @@
     HANDLE hThread;
     DWORD  idThread;
+    /* wine does not seem to guarantie the dll is not unloaded in case FreeLibrary is used
+     * while d3d object is not terminated, keep an extra reference to ensure we're not unloaded
+     * while we are active */
+    HMODULE hSelf;
 } VBOXEXT_WORKER, *PVBOXEXT_WORKER;
 
@@ -45,5 +54,4 @@
 
 /*******************************/
-
 typedef struct VBOXEXT_GLOBAL
 {
@@ -107,4 +115,11 @@
 HRESULT VBoxExtWorkerCreate(PVBOXEXT_WORKER pWorker)
 {
+    if(!GetModuleHandleEx(0, VBOXEXT_WINE_MODULE_NAME, &pWorker->hSelf))
+    {
+        DWORD dwEr = GetLastError();
+        ERR("GetModuleHandleEx failed, %d", dwEr);
+        return E_FAIL;
+    }
+
     InitializeCriticalSection(&pWorker->CritSect);
     pWorker->hEvent = CreateEvent(NULL, /* LPSECURITY_ATTRIBUTES lpEventAttributes */
@@ -143,4 +158,6 @@
     }
 
+    FreeLibrary(pWorker->hSelf);
+
     return E_FAIL;
 }
@@ -166,4 +183,6 @@
     CloseHandle(pWorker->hEvent);
     DeleteCriticalSection(&pWorker->CritSect);
+
+    FreeLibrary(pWorker->hSelf);
 
     return S_OK;
@@ -284,5 +303,5 @@
     pData->ret = ReleaseDC(pData->hWnd, pData->hDC);
 }
-
+#if 0
 HDC VBoxExtGetDC(HWND hWnd)
 {
@@ -327,4 +346,5 @@
 #endif
 }
+#endif
 
 /* window creation API */
@@ -353,5 +373,5 @@
 #define VBOXEXTWND_NAME "VboxDispD3DWineWnd"
 
-HRESULT vboxExtWndDoCreate(DWORD w, DWORD h, HWND *phWnd)
+HRESULT vboxExtWndDoCreate(DWORD w, DWORD h, HWND *phWnd, HDC *phDC)
 {
     HRESULT hr = S_OK;
@@ -394,4 +414,5 @@
         {
             *phWnd = hWnd;
+            *phDC = GetDC(hWnd);
         }
         else
@@ -406,8 +427,10 @@
 }
 
-static HRESULT vboxExtWndDoDestroy(HWND hWnd)
-{
-    BOOL bResult = DestroyWindow(hWnd);
+static HRESULT vboxExtWndDoDestroy(HWND hWnd, HDC hDC)
+{
+    BOOL bResult;
     DWORD winErr;
+    ReleaseDC(hWnd, hDC);
+    bResult = DestroyWindow(hWnd);
     Assert(bResult);
     if (bResult)
@@ -424,4 +447,5 @@
     int hr;
     HWND hWnd;
+    HDC hDC;
     DWORD width;
     DWORD height;
@@ -432,4 +456,5 @@
     int hr;
     HWND hWnd;
+    HDC hDC;
 } VBOXEXTWND_DESTROY_INFO;
 
@@ -437,5 +462,5 @@
 {
     VBOXEXTWND_DESTROY_INFO *pInfo = (VBOXEXTWND_DESTROY_INFO*)pvUser;
-    pInfo->hr = vboxExtWndDoDestroy(pInfo->hWnd);
+    pInfo->hr = vboxExtWndDoDestroy(pInfo->hWnd, pInfo->hDC);
     Assert(pInfo->hr == S_OK);
 }
@@ -444,9 +469,9 @@
 {
     VBOXEXTWND_CREATE_INFO *pInfo = (VBOXEXTWND_CREATE_INFO*)pvUser;
-    pInfo->hr = vboxExtWndDoCreate(pInfo->width, pInfo->height, &pInfo->hWnd);
+    pInfo->hr = vboxExtWndDoCreate(pInfo->width, pInfo->height, &pInfo->hWnd, &pInfo->hDC);
     Assert(pInfo->hr == S_OK);
 }
 
-HRESULT VBoxExtWndDestroy(HWND hWnd)
+HRESULT VBoxExtWndDestroy(HWND hWnd, HDC hDC)
 {
     HRESULT hr;
@@ -454,4 +479,5 @@
     Info.hr = E_FAIL;
     Info.hWnd = hWnd;
+    Info.hDC = hDC;
     hr = VBoxExtDwSubmitProc(vboxExtWndDestroyWorker, &Info);
     Assert(hr == S_OK);
@@ -464,5 +490,5 @@
 }
 
-HRESULT VBoxExtWndCreate(DWORD width, DWORD height, HWND *phWnd)
+HRESULT VBoxExtWndCreate(DWORD width, DWORD height, HWND *phWnd, HDC *phDC)
 {
     HRESULT hr;
@@ -477,5 +503,8 @@
         Assert(Info.hr == S_OK);
         if (Info.hr == S_OK)
+        {
             *phWnd = Info.hWnd;
+            *phDC = Info.hDC;
+        }
         return Info.hr;
     }
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/vboxext.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/vboxext.h	(revision 38331)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/vboxext.h	(revision 38363)
@@ -23,4 +23,5 @@
 HRESULT VBoxExtCheckInit();
 HRESULT VBoxExtCheckTerm();
+#if 0
 /* Windows destroys HDC created by a given thread when the thread is terminated
  * this leads to a mess-up in Wine & Chromium code in some situations, e.g.
@@ -29,8 +30,9 @@
 HDC VBoxExtGetDC(HWND hWnd);
 int VBoxExtReleaseDC(HWND hWnd, HDC hDC);
+#endif
 
 /* API for creating & destroying windows */
-HRESULT VBoxExtWndDestroy(HWND hWnd);
-HRESULT VBoxExtWndCreate(DWORD width, DWORD height, HWND *phWnd);
+HRESULT VBoxExtWndDestroy(HWND hWnd, HDC hDC);
+HRESULT VBoxExtWndCreate(DWORD width, DWORD height, HWND *phWnd, HDC *phDC);
 
 #endif /* #ifndef ___VBOXEXT_H__*/
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_main.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_main.c	(revision 38331)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_main.c	(revision 38363)
@@ -403,4 +403,5 @@
 }
 
+#ifndef VBOX_WITH_WDDM
 static struct wined3d_wndproc *wined3d_find_wndproc(HWND window)
 {
@@ -500,5 +501,5 @@
     ERR("Window %p is not registered with wined3d.\n", window);
 }
-
+#endif
 /* At process attach */
 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_private.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_private.h	(revision 38331)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_private.h	(revision 38363)
@@ -1639,6 +1639,8 @@
 
 HRESULT wined3d_init(IWineD3DImpl *wined3d, UINT version, IUnknown *parent) DECLSPEC_HIDDEN;
+#ifndef VBOX_WITH_WDDM
 BOOL wined3d_register_window(HWND window, struct IWineD3DDeviceImpl *device) DECLSPEC_HIDDEN;
 void wined3d_unregister_window(HWND window) DECLSPEC_HIDDEN;
+#endif
 
 /*****************************************************************************
@@ -1715,5 +1717,7 @@
     WINED3DDEVICE_CREATION_PARAMETERS createParms;
     WINED3DDEVTYPE                  devType;
+#ifndef VBOX_WITH_WDDM
     HWND focus_window;
+#endif
 
     IWineD3DSwapChain     **swapchains;
@@ -1790,6 +1794,8 @@
         IUnknown *parent, IWineD3DDeviceParent *device_parent) DECLSPEC_HIDDEN;
 void device_preload_textures(IWineD3DDeviceImpl *device) DECLSPEC_HIDDEN;
+#ifndef VBOX_WITH_WDDM
 LRESULT device_process_message(IWineD3DDeviceImpl *device, HWND window,
         UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc) DECLSPEC_HIDDEN;
+#endif
 void device_resource_add(IWineD3DDeviceImpl *This, IWineD3DResource *resource) DECLSPEC_HIDDEN;
 void device_resource_released(IWineD3DDeviceImpl *This, IWineD3DResource *resource) DECLSPEC_HIDDEN;
