Index: /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxHelpers.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxHelpers.cpp	(revision 35862)
+++ /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxHelpers.cpp	(revision 35863)
@@ -4,5 +4,5 @@
 
 /*
- * Copyright (C) 2006-2010 Oracle Corporation
+ * Copyright (C) 2006-2011 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -19,4 +19,5 @@
 
 #include <iprt/string.h>
+#include <VBox/Log.h>
 #include <VBox/VBoxGuestLib.h>
 
@@ -27,4 +28,13 @@
 
 
+int hlpReportStatus(VBoxGuestStatusCurrent statusCurrent)
+{
+    int rc = VbglR3ReportAdditionsStatus(VBoxGuestStatusFacility_VBoxTray,
+                                         statusCurrent,
+                                         0 /* Flags */);
+    if (RT_FAILURE(rc))
+        Log(("VBoxTray: Could not report VBoxTray status \"%ld\", rc=%Rrc\n", statusCurrent, rc));
+    return rc;
+}
 
 /**
Index: /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxHelpers.h
===================================================================
--- /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxHelpers.h	(revision 35862)
+++ /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxHelpers.h	(revision 35863)
@@ -27,4 +27,5 @@
 #endif /* !DEBUG_DISPLAY_CHANGE */
 
+extern int hlpReportStatus(VBoxGuestStatusCurrent statusCurrent);
 extern void hlpReloadCursor(void);
 extern void hlpResizeRect(RECTL *paRects, unsigned nRects, unsigned uPrimary, unsigned uResized, int iNewWidth, int iNewHeight);
Index: /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxHostVersion.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxHostVersion.cpp	(revision 35862)
+++ /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxHostVersion.cpp	(revision 35863)
@@ -51,5 +51,5 @@
                                                 "install option from the Devices menu.", pszGuestVersion, pszHostVersion);
 
-                rc = hlpShowBalloonTip(gInstance, gToolWindow, ID_TRAYICON,
+                rc = hlpShowBalloonTip(ghInstance, ghwndToolWindow, ID_TRAYICON,
                                        szMsg, szTitle,
                                        5000 /* Time to display in msec */, NIIF_INFO);
Index: /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxIPC.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxIPC.cpp	(revision 35862)
+++ /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxIPC.cpp	(revision 35863)
@@ -130,5 +130,5 @@
     if (RT_SUCCESS(rc))
     {
-        hlpShowBalloonTip(gInstance, gToolWindow, ID_TRAYICON,
+        hlpShowBalloonTip(ghInstance, ghwndToolWindow, ID_TRAYICON,
                           msg.szContent, msg.szTitle,
                           msg.ulShowMS, msg.ulType);
Index: /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxRestore.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxRestore.cpp	(revision 35862)
+++ /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxRestore.cpp	(revision 35863)
@@ -130,8 +130,8 @@
             /* did we get the right event? */
             if (waitEvent.u32EventFlagsOut & VMMDEV_EVENT_RESTORED)
-                PostMessage(gToolWindow, WM_VBOXTRAY_VM_RESTORED, 0, 0);
+                PostMessage(ghwndToolWindow, WM_VBOXTRAY_VM_RESTORED, 0, 0);
             else
                 /** @todo Don't poll, but wait for connect/disconnect events */
-                PostMessage(gToolWindow, WM_VBOXTRAY_VRDP_CHECK, 0, 0);
+                PostMessage(ghwndToolWindow, WM_VBOXTRAY_VRDP_CHECK, 0, 0);
         }
         else
Index: /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxSeamless.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxSeamless.cpp	(revision 35862)
+++ /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxSeamless.cpp	(revision 35863)
@@ -341,5 +341,5 @@
                                     Log(("VBoxTray: SystemParametersInfo SPI_SETSCREENSAVEACTIVE failed with %d\n", GetLastError()));
                             }
-                            PostMessage(gToolWindow, WM_VBOX_REMOVE_SEAMLESS_HOOK, 0, 0);
+                            PostMessage(ghwndToolWindow, WM_VBOX_REMOVE_SEAMLESS_HOOK, 0, 0);
                             break;
 
@@ -355,5 +355,5 @@
                             if (!ret)
                                 Log(("VBoxTray: SystemParametersInfo SPI_SETSCREENSAVEACTIVE failed with %d\n", GetLastError()));
-                            PostMessage(gToolWindow, WM_VBOX_INSTALL_SEAMLESS_HOOK, 0, 0);
+                            PostMessage(ghwndToolWindow, WM_VBOX_INSTALL_SEAMLESS_HOOK, 0, 0);
                             break;
 
Index: /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxTray.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxTray.cpp	(revision 35862)
+++ /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxTray.cpp	(revision 35863)
@@ -4,5 +4,5 @@
 
 /*
- * Copyright (C) 2006-2010 Oracle Corporation
+ * Copyright (C) 2006-2011 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -43,11 +43,11 @@
 *   Global Variables                                                           *
 *******************************************************************************/
-HANDLE                gVBoxDriver;
-HANDLE                gStopSem;
+HANDLE                ghVBoxDriver;
+HANDLE                ghStopSem;
 HANDLE                ghSeamlessNotifyEvent = 0;
 SERVICE_STATUS        gVBoxServiceStatus;
 SERVICE_STATUS_HANDLE gVBoxServiceStatusHandle;
-HINSTANCE             gInstance;
-HWND                  gToolWindow;
+HINSTANCE             ghInstance;
+HWND                  ghwndToolWindow;
 NOTIFYICONDATA        gNotifyIconData;
 DWORD                 gMajorVersion;
@@ -141,5 +141,5 @@
 static int vboxTrayCreateTrayIcon(void)
 {
-    HICON hIcon = LoadIcon(gInstance, MAKEINTRESOURCE(IDI_VIRTUALBOX));
+    HICON hIcon = LoadIcon(ghInstance, MAKEINTRESOURCE(IDI_VIRTUALBOX));
     if (hIcon == NULL)
     {
@@ -152,5 +152,5 @@
     RT_ZERO(gNotifyIconData);
     gNotifyIconData.cbSize           = NOTIFYICONDATA_V1_SIZE; // sizeof(NOTIFYICONDATA);
-    gNotifyIconData.hWnd             = gToolWindow;
+    gNotifyIconData.hWnd             = ghwndToolWindow;
     gNotifyIconData.uID              = ID_TRAYICON;
     gNotifyIconData.uFlags           = NIF_ICON | NIF_MESSAGE | NIF_TIP;
@@ -289,5 +289,5 @@
 }
 
-int vboxTrayRegisterGlobalMessages(PVBOXGLOBALMESSAGE pTable)
+static int vboxTrayRegisterGlobalMessages(PVBOXGLOBALMESSAGE pTable)
 {
     int rc = VINF_SUCCESS;
@@ -312,10 +312,10 @@
 }
 
-bool vboxTrayHandleGlobalMessages(PVBOXGLOBALMESSAGE pTable, UINT uMsg,
-                                  WPARAM wParam, LPARAM lParam)
+static bool vboxTrayHandleGlobalMessages(PVBOXGLOBALMESSAGE pTable, UINT uMsg,
+                                         WPARAM wParam, LPARAM lParam)
 {
     if (pTable == NULL)
         return false;
-    while (pTable->pszName)
+    while (pTable && pTable->pszName)
     {
         if (pTable->uMsgID == uMsg)
@@ -332,13 +332,9 @@
 }
 
-void WINAPI VBoxServiceStart(void)
-{
-    Log(("VBoxTray: Entering service main function\n"));
-
-    VBOXSERVICEENV svcEnv;
-    DWORD dwErr = NO_ERROR;
-
+static int vboxTrayOpenBaseDriver()
+{
     /* Open VBox guest driver. */
-    gVBoxDriver = CreateFile(VBOXGUEST_DEVICE_NAME,
+    DWORD dwErr = ERROR_SUCCESS;
+    ghVBoxDriver = CreateFile(VBOXGUEST_DEVICE_NAME,
                              GENERIC_READ | GENERIC_WRITE,
                              FILE_SHARE_READ | FILE_SHARE_WRITE,
@@ -347,37 +343,65 @@
                              FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
                              NULL);
-    if (gVBoxDriver == INVALID_HANDLE_VALUE)
+    if (ghVBoxDriver == INVALID_HANDLE_VALUE)
     {
         dwErr = GetLastError();
         LogRel(("VBoxTray: Could not open VirtualBox Guest Additions driver! Please install / start it first! Error = %08X\n", dwErr));
     }
-
-    if (dwErr == NO_ERROR)
-    {
-        /* Create a custom window class. */
-        WNDCLASS windowClass = {0};
-        windowClass.style         = CS_NOCLOSE;
-        windowClass.lpfnWndProc   = (WNDPROC)VBoxToolWndProc;
-        windowClass.hInstance     = gInstance;
-        windowClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
-        windowClass.lpszClassName = "VBoxTrayToolWndClass";
-        if (!RegisterClass(&windowClass))
-        {
-            dwErr = GetLastError();
-            Log(("VBoxTray: Registering invisible tool window failed, error = %08X\n", dwErr));
-        }
-    }
-
-    if (dwErr == NO_ERROR)
-    {
-        /* Create our (invisible) tool window. */
-        /* Note: The window name ("VBoxTrayToolWnd") and class ("VBoxTrayToolWndClass") is
+    return RTErrConvertFromWin32(dwErr);
+}
+
+static void vboxTrayCloseBaseDriver()
+{
+    if (ghVBoxDriver)
+    {
+        CloseHandle(ghVBoxDriver);
+        ghVBoxDriver = NULL;
+    }
+}
+
+static void vboxTrayDestroyToolWindow()
+{
+    if (ghwndToolWindow)
+    {
+        Log(("VBoxTray: Destroying tool window ...\n"));
+
+        /* Destroy the tool window. */
+        DestroyWindow(ghwndToolWindow);
+        ghwndToolWindow = NULL;
+
+        UnregisterClass("VBoxTrayToolWndClass", ghInstance);
+    }
+}
+
+static int vboxTrayCreateToolWindow()
+{
+    DWORD dwErr = ERROR_SUCCESS;
+
+    /* Create a custom window class. */
+    WNDCLASS windowClass = {0};
+    windowClass.style         = CS_NOCLOSE;
+    windowClass.lpfnWndProc   = (WNDPROC)VBoxToolWndProc;
+    windowClass.hInstance     = ghInstance;
+    windowClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
+    windowClass.lpszClassName = "VBoxTrayToolWndClass";
+    if (!RegisterClass(&windowClass))
+    {
+        dwErr = GetLastError();
+        Log(("VBoxTray: Registering invisible tool window failed, error = %08X\n", dwErr));
+    }
+    else
+    {
+        /*
+         * Create our (invisible) tool window.
+         * Note: The window name ("VBoxTrayToolWnd") and class ("VBoxTrayToolWndClass") is
          * needed for posting globally registered messages to VBoxTray and must not be
-         * changed! Otherwise things get broken! */
-        gToolWindow = CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
-                                     "VBoxTrayToolWndClass", "VBoxTrayToolWnd",
-                                     WS_POPUPWINDOW,
-                                     -200, -200, 100, 100, NULL, NULL, gInstance, NULL);
-        if (!gToolWindow)
+         * changed! Otherwise things get broken!
+         *
+         */
+        ghwndToolWindow = CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
+                                         "VBoxTrayToolWndClass", "VBoxTrayToolWnd",
+                                         WS_POPUPWINDOW,
+                                         -200, -200, 100, 100, NULL, NULL, ghInstance, NULL);
+        if (!ghwndToolWindow)
         {
             dwErr = GetLastError();
@@ -386,12 +410,20 @@
         else
         {
+            /* Reload the cursor(s). */
             hlpReloadCursor();
-        }
-    }
-
-    Log(("VBoxTray: Window Handle = %p, Status = %p\n", gToolWindow, dwErr));
-
+
+            Log(("VBoxTray: Invisible tool window handle = %p\n", ghwndToolWindow));
+        }
+    }
+
+    if (dwErr != ERROR_SUCCESS)
+         vboxTrayDestroyToolWindow();
+    return RTErrConvertFromWin32(dwErr);
+}
+
+static int vboxTraySetupSeamless()
+{
     OSVERSIONINFO info;
-    gMajorVersion = 5; /* default XP */
+    gMajorVersion = 5; /* Default to Windows XP. */
     info.dwOSVersionInfoSize = sizeof(info);
     if (GetVersionEx(&info))
@@ -401,35 +433,34 @@
     }
 
-    if (dwErr == NO_ERROR)
-    {
-        gStopSem = CreateEvent(NULL, TRUE, FALSE, NULL);
-        if (gStopSem == NULL)
-        {
-            Log(("VBoxTray: CreateEvent for stopping VBoxTray failed, error = %08X\n", GetLastError()));
-            return;
-        }
-
-        /* We need to setup a security descriptor to allow other processes modify access to the seamless notification event semaphore */
-        SECURITY_ATTRIBUTES     SecAttr;
-        char                    secDesc[SECURITY_DESCRIPTOR_MIN_LENGTH];
-        BOOL                    ret;
-
-        SecAttr.nLength              = sizeof(SecAttr);
-        SecAttr.bInheritHandle       = FALSE;
-        SecAttr.lpSecurityDescriptor = &secDesc;
-        InitializeSecurityDescriptor(SecAttr.lpSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
-        ret = SetSecurityDescriptorDacl(SecAttr.lpSecurityDescriptor, TRUE, 0, FALSE);
-        if (!ret)
-            Log(("VBoxTray: SetSecurityDescriptorDacl failed with error = %08X\n", GetLastError()));
-
-        /* For Vista and up we need to change the integrity of the security descriptor too */
+    /* We need to setup a security descriptor to allow other processes modify access to the seamless notification event semaphore. */
+    SECURITY_ATTRIBUTES     SecAttr;
+    DWORD                   dwErr = ERROR_SUCCESS;
+    char                    secDesc[SECURITY_DESCRIPTOR_MIN_LENGTH];
+    BOOL                    fRC;
+
+    SecAttr.nLength              = sizeof(SecAttr);
+    SecAttr.bInheritHandle       = FALSE;
+    SecAttr.lpSecurityDescriptor = &secDesc;
+    InitializeSecurityDescriptor(SecAttr.lpSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
+    fRC = SetSecurityDescriptorDacl(SecAttr.lpSecurityDescriptor, TRUE, 0, FALSE);
+    if (!fRC)
+    {
+        dwErr = GetLastError();
+        Log(("VBoxTray: SetSecurityDescriptorDacl failed with last error = %08X\n", dwErr));
+    }
+    else
+    {
+        /* For Vista and up we need to change the integrity of the security descriptor, too. */
         if (gMajorVersion >= 6)
         {
-            HMODULE hModule;
-
             BOOL (WINAPI * pfnConvertStringSecurityDescriptorToSecurityDescriptorA)(LPCSTR StringSecurityDescriptor, DWORD StringSDRevision, PSECURITY_DESCRIPTOR  *SecurityDescriptor, PULONG  SecurityDescriptorSize);
 
-            hModule = LoadLibrary("ADVAPI32.DLL");
-            if (hModule)
+            HMODULE hModule = LoadLibrary("ADVAPI32.DLL");
+            if (!hModule)
+            {
+                dwErr = GetLastError();
+                Log(("VBoxTray: Loading module ADVAPI32.DLL failed with last error = %08X\n", dwErr));
+            }
+            else
             {
                 PSECURITY_DESCRIPTOR    pSD;
@@ -443,157 +474,188 @@
                 if (pfnConvertStringSecurityDescriptorToSecurityDescriptorA)
                 {
-                    ret = pfnConvertStringSecurityDescriptorToSecurityDescriptorA("S:(ML;;NW;;;LW)", /* this means "low integrity" */
+                    fRC = pfnConvertStringSecurityDescriptorToSecurityDescriptorA("S:(ML;;NW;;;LW)", /* this means "low integrity" */
                                                                                   SDDL_REVISION_1, &pSD, NULL);
-                    if (!ret)
-                        Log(("VBoxTray: ConvertStringSecurityDescriptorToSecurityDescriptorA failed with error = %08X\n", GetLastError()));
-
-                    ret = GetSecurityDescriptorSacl(pSD, &fSaclPresent, &pSacl, &fSaclDefaulted);
-                    if (!ret)
-                        Log(("VBoxTray: GetSecurityDescriptorSacl failed with error = %08X\n", GetLastError()));
-
-                    ret = SetSecurityDescriptorSacl(SecAttr.lpSecurityDescriptor, TRUE, pSacl, FALSE);
-                    if (!ret)
-                        Log(("VBoxTray: SetSecurityDescriptorSacl failed with error = %08X\n", GetLastError()));
+                    if (!fRC)
+                    {
+                        dwErr = GetLastError();
+                        Log(("VBoxTray: ConvertStringSecurityDescriptorToSecurityDescriptorA failed with last error = %08X\n", dwErr));
+                    }
+                    else
+                    {
+                        fRC = GetSecurityDescriptorSacl(pSD, &fSaclPresent, &pSacl, &fSaclDefaulted);
+                        if (!fRC)
+                        {
+                            dwErr = GetLastError();
+                            Log(("VBoxTray: GetSecurityDescriptorSacl failed with last error = %08X\n", dwErr));
+                        }
+                        else
+                        {
+                            fRC = SetSecurityDescriptorSacl(SecAttr.lpSecurityDescriptor, TRUE, pSacl, FALSE);
+                            if (!fRC)
+                            {
+                                dwErr = GetLastError();
+                                Log(("VBoxTray: SetSecurityDescriptorSacl failed with last error = %08X\n", dwErr));
+                            }
+                        }
+                    }
                 }
             }
         }
 
-        if (gMajorVersion >= 5)        /* Only for W2K and up ... */
+        if (   dwErr == ERROR_SUCCESS
+            && gMajorVersion >= 5) /* Only for W2K and up ... */
         {
             ghSeamlessNotifyEvent = CreateEvent(&SecAttr, FALSE, FALSE, VBOXHOOK_GLOBAL_EVENT_NAME);
             if (ghSeamlessNotifyEvent == NULL)
             {
-                Log(("VBoxTray: CreateEvent for Seamless failed, error = %08X\n", GetLastError()));
-                return;
-            }
-        }
-    }
-
-    /*
-     * Start services listed in the vboxServiceTable.
-     */
-    svcEnv.hInstance  = gInstance;
-    svcEnv.hDriver    = gVBoxDriver;
-
-    /* initializes disp-if to default (XPDM) mode */
-    dwErr = VBoxDispIfInit(&svcEnv.dispIf);
-#ifdef VBOX_WITH_WDDM
-    /*
-     * For now the display mode will be adjusted to WDDM mode if needed
-     * on display service initialization when it detects the display driver type.
-     */
-#endif
-
-    if (dwErr == NO_ERROR)
-    {
-        int rc = vboxTrayStartServices(&svcEnv, vboxServiceTable);
-        if (RT_FAILURE (rc))
-        {
-            dwErr = ERROR_GEN_FAILURE;
-        }
-    }
-
-    /* terminate service if something went wrong */
-    if (dwErr != NO_ERROR)
-    {
-        vboxTrayStopServices(&svcEnv, vboxServiceTable);
-        return;
-    }
-
-    int rc = vboxTrayCreateTrayIcon();
-    if (   RT_SUCCESS(rc)
-        && gMajorVersion >= 5) /* Only for W2K and up ... */
-    {
-        /* We're ready to create the tooltip balloon. */
-        /* Check in 10 seconds (@todo make seconds configurable) ... */
-        SetTimer(gToolWindow,
-                 TIMERID_VBOXTRAY_CHECK_HOSTVERSION,
-                 10 * 1000, /* 10 seconds */
-                 NULL       /* No timerproc */);
-    }
-
-    /* Do the Shared Folders auto-mounting stuff. */
-    VBoxSharedFoldersAutoMount();
-
-    /* Boost thread priority to make sure we wake up early for seamless window notifications (not sure if it actually makes any difference though) */
-    SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
-
-    /*
-     * Main execution loop
-     * Wait for the stop semaphore to be posted or a window event to arrive
-     */
-
-    DWORD dwEventCount = 2;
-    HANDLE hWaitEvent[2] = {gStopSem, ghSeamlessNotifyEvent};
-
-    if (0 == ghSeamlessNotifyEvent)         /* If seamless mode is not active / supported, reduce event array count */
-        dwEventCount = 1;
-
-    Log(("VBoxTray: Number of events to wait in main loop: %ld\n", dwEventCount));
-    while (true)
-    {
-        DWORD waitResult = MsgWaitForMultipleObjectsEx(dwEventCount, hWaitEvent, 500, QS_ALLINPUT, 0);
-        waitResult = waitResult - WAIT_OBJECT_0;
-
-        /* Only enable for message debugging, lots of traffic! */
-        //Log(("VBoxTray: Wait result  = %ld\n", waitResult));
-
-        if (waitResult == 0)
-        {
-            Log(("VBoxTray: Event 'Exit' triggered\n"));
-            /* exit */
-            break;
-        }
-        else if (   (waitResult == 1)
-                 && (ghSeamlessNotifyEvent!=0)) /* Only jump in, if seamless is active! */
-        {
-            Log(("VBoxTray: Event 'Seamless' triggered\n"));
-
-            /* seamless window notification */
-            VBoxSeamlessCheckWindows();
+                dwErr = GetLastError();
+                Log(("VBoxTray: CreateEvent for Seamless failed, last error = %08X\n", dwErr));
+            }
+        }
+    }
+    return RTErrConvertFromWin32(dwErr);
+}
+
+static void vboxTrayShutdownSeamless(void)
+{
+	if (ghSeamlessNotifyEvent)
+	{
+    	CloseHandle(ghSeamlessNotifyEvent);
+	    ghSeamlessNotifyEvent = NULL;
+	}
+}
+
+static int vboxTrayServiceMain(void)
+{
+    int rc = VINF_SUCCESS;
+    Log(("VBoxTray: Entering vboxTrayServiceMain\n"));
+
+    ghStopSem = CreateEvent(NULL, TRUE, FALSE, NULL);
+    if (ghStopSem == NULL)
+    {
+        rc = RTErrConvertFromWin32(GetLastError());
+        Log(("VBoxTray: CreateEvent for stopping VBoxTray failed, rc=%Rrc\n", rc));
+    }
+    else
+    {
+        /*
+         * Start services listed in the vboxServiceTable.
+         */
+        VBOXSERVICEENV svcEnv;
+        svcEnv.hInstance = ghInstance;
+        svcEnv.hDriver   = ghVBoxDriver;
+
+        /* Initializes disp-if to default (XPDM) mode. */
+        VBoxDispIfInit(&svcEnv.dispIf); /* Cannot fail atm. */
+    #ifdef VBOX_WITH_WDDM
+        /*
+         * For now the display mode will be adjusted to WDDM mode if needed
+         * on display service initialization when it detects the display driver type.
+         */
+    #endif
+
+        /* Finally start all the built-in services! */
+        rc = vboxTrayStartServices(&svcEnv, vboxServiceTable);
+        if (RT_FAILURE(rc))
+        {
+            /* Terminate service if something went wrong. */
+            vboxTrayStopServices(&svcEnv, vboxServiceTable);
         }
         else
         {
-            /* timeout or a window message, handle it */
-            MSG msg;
-            while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
-            {
-                Log(("VBoxTray: msg %p\n", msg.message));
-                if (msg.message == WM_QUIT)
+            rc = vboxTrayCreateTrayIcon();
+            if (   RT_SUCCESS(rc)
+                && gMajorVersion >= 5) /* Only for W2K and up ... */
+            {
+                /* We're ready to create the tooltip balloon.
+                   Check in 10 seconds (@todo make seconds configurable) ... */
+                SetTimer(ghwndToolWindow,
+                         TIMERID_VBOXTRAY_CHECK_HOSTVERSION,
+                         10 * 1000, /* 10 seconds */
+                         NULL       /* No timerproc */);
+            }
+
+            if (RT_SUCCESS(rc))
+            {
+                /* Do the Shared Folders auto-mounting stuff. */
+                rc = VBoxSharedFoldersAutoMount();
+                if (RT_SUCCESS(rc))
                 {
-                    Log(("VBoxTray: WM_QUIT!\n"));
-                    SetEvent(gStopSem);
-                    continue;
+                    /* Report the host that we're up and running! */
+                    rc = hlpReportStatus(VBoxGuestStatusCurrent_Active);
                 }
-                TranslateMessage(&msg);
-                DispatchMessage(&msg);
-            }
-        }
-    }
-
-    Log(("VBoxTray: Returned from main loop, exiting ...\n"));
+            }
+
+            if (RT_SUCCESS(rc))
+            {
+                /* Boost thread priority to make sure we wake up early for seamless window notifications
+                 * (not sure if it actually makes any difference though). */
+                SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
+
+                /*
+                 * Main execution loop
+                 * Wait for the stop semaphore to be posted or a window event to arrive
+                 */
+
+                DWORD dwEventCount = 2;
+                HANDLE hWaitEvent[2] = { ghStopSem, ghSeamlessNotifyEvent };
+
+                if (0 == ghSeamlessNotifyEvent) /* If seamless mode is not active / supported, reduce event array count. */
+                    dwEventCount = 1;
+
+                Log(("VBoxTray: Number of events to wait in main loop: %ld\n", dwEventCount));
+                while (true)
+                {
+                    DWORD waitResult = MsgWaitForMultipleObjectsEx(dwEventCount, hWaitEvent, 500, QS_ALLINPUT, 0);
+                    waitResult = waitResult - WAIT_OBJECT_0;
+
+                    /* Only enable for message debugging, lots of traffic! */
+                    //Log(("VBoxTray: Wait result  = %ld\n", waitResult));
+
+                    if (waitResult == 0)
+                    {
+                        Log(("VBoxTray: Event 'Exit' triggered\n"));
+                        /* exit */
+                        break;
+                    }
+                    else if (   waitResult == 1
+                             && ghSeamlessNotifyEvent != 0) /* Only jump in, if seamless is active! */
+                    {
+                        Log(("VBoxTray: Event 'Seamless' triggered\n"));
+
+                        /* seamless window notification */
+                        VBoxSeamlessCheckWindows();
+                    }
+                    else
+                    {
+                        /* timeout or a window message, handle it */
+                        MSG msg;
+                        while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
+                        {
+                            Log(("VBoxTray: msg %p\n", msg.message));
+                            if (msg.message == WM_QUIT)
+                            {
+                                Log(("VBoxTray: WM_QUIT!\n"));
+                                SetEvent(ghStopSem);
+                                continue;
+                            }
+                            TranslateMessage(&msg);
+                            DispatchMessage(&msg);
+                        }
+                    }
+                }
+                Log(("VBoxTray: Returned from main loop, exiting ...\n"));
+            }
+            Log(("VBoxTray: Waiting for services to stop ...\n"));
+            vboxTrayStopServices(&svcEnv, vboxServiceTable);
+        } /* Services started */
+        CloseHandle(ghStopSem);
+    } /* Stop event created */
 
     vboxTrayRemoveTrayIcon();
 
-    Log(("VBoxTray: Waiting for display change thread ...\n"));
-
-    vboxTrayStopServices(&svcEnv, vboxServiceTable);
-
-    Log(("VBoxTray: Destroying tool window ...\n"));
-
-    /* Destroy the tool window. */
-    DestroyWindow(gToolWindow);
-
-    UnregisterClass("VBoxTrayToolWndClass", gInstance);
-
-    CloseHandle(gVBoxDriver);
-    CloseHandle(gStopSem);
-    CloseHandle(ghSeamlessNotifyEvent);
-
-    Log(("VBoxTray: Leaving service main function\n"));
-
-    return;
-}
-
+    Log(("VBoxTray: Leaving vboxTrayServiceMain with rc=%Rrc\n", rc));
+    return rc;
+}
 
 /**
@@ -613,17 +675,49 @@
     }
 
+    LogRel(("VBoxTray: %s r%s\n", RTBldCfgVersion(), RTBldCfgRevisionStr()));
+
     int rc = RTR3Init();
+    if (RT_SUCCESS(rc))
+    {
+        rc = VbglR3Init();
+        if (RT_SUCCESS(rc))
+            rc = vboxTrayOpenBaseDriver();
+    }
+
     if (RT_FAILURE(rc))
-        return rc;
-
-    rc = VbglR3Init();
-    if (RT_FAILURE(rc))
-        return rc;
-
-    LogRel(("VBoxTray: %s r%s started\n", RTBldCfgVersion(), RTBldCfgRevisionStr()));
-
-    gInstance = hInstance;
-    VBoxServiceStart();
-
+    {
+        CloseHandle (hMutexAppRunning);
+        hMutexAppRunning = NULL;
+        return 1;
+    }
+
+    if (RT_SUCCESS(rc))
+    {
+        /* Save instance handle. */
+        ghInstance = hInstance;
+
+        rc = hlpReportStatus(VBoxGuestStatusCurrent_Init);
+        if (RT_SUCCESS(rc))
+        {
+            rc = vboxTrayCreateToolWindow();
+            if (RT_SUCCESS(rc))
+            {
+                rc = vboxTraySetupSeamless();
+                if (RT_SUCCESS(rc))
+                {
+                    Log(("VBoxTray: Init successful\n"));
+                    rc = vboxTrayServiceMain();
+                    hlpReportStatus(VBoxGuestStatusCurrent_Terminating);
+                    vboxTrayShutdownSeamless();
+                }
+                vboxTrayDestroyToolWindow();
+            }
+            rc = hlpReportStatus(VBoxGuestStatusCurrent_Inactive);
+        }
+    }
+    else
+        Log(("VBoxTray: Could not report VBoxTray status \"Init\", rc=%Rrc\n", rc));
+	if (RT_FAILURE(rc))
+    	LogRel(("VBoxTray: Error while starting, rc=%Rrc\n", rc));
     LogRel(("VBoxTray: Ended\n"));
 
@@ -661,5 +755,5 @@
         case WM_DESTROY:
             Log(("VBoxTray: Tool window destroyed\n"));
-            KillTimer(gToolWindow, TIMERID_VBOXTRAY_CHECK_HOSTVERSION);
+            KillTimer(ghwndToolWindow, TIMERID_VBOXTRAY_CHECK_HOSTVERSION);
             return 0;
 
@@ -671,5 +765,5 @@
                     {
                         /* After successful run we don't need to check again. */
-                        KillTimer(gToolWindow, TIMERID_VBOXTRAY_CHECK_HOSTVERSION);
+                        KillTimer(ghwndToolWindow, TIMERID_VBOXTRAY_CHECK_HOSTVERSION);
                     }
                     return 0;
Index: /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxTray.h
===================================================================
--- /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxTray.h	(revision 35862)
+++ /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxTray.h	(revision 35863)
@@ -102,6 +102,6 @@
 } VBOXGLOBALMESSAGE, *PVBOXGLOBALMESSAGE;
 
-extern HWND         gToolWindow;
-extern HINSTANCE    gInstance;
+extern HWND         ghwndToolWindow;
+extern HINSTANCE    ghInstance;
 
 #endif /* !___VBOXTRAY_H */
