Index: /trunk/src/VBox/Main/VMMDevInterface.cpp
===================================================================
--- /trunk/src/VBox/Main/VMMDevInterface.cpp	(revision 1384)
+++ /trunk/src/VBox/Main/VMMDevInterface.cpp	(revision 1385)
@@ -459,5 +459,5 @@
     }
 
-    /// @todo hgcmShutdown
+    hgcmReset ();
 #endif
     if (pData->pVMMDev)
@@ -485,5 +485,10 @@
 
         pData->pVMMDev->hgcmDisconnect(cmd, pData->pVMMDev->getShFlClientId());
-
+    }
+    
+    hgcmReset ();
+
+    if (pData->pVMMDev->mSharedFolderClientId)
+    {
         /* Reload Shared Folder HGCM service */
         HGCMSERVICELOCATION loc;
Index: /trunk/src/VBox/Main/hgcm/HGCM.cpp
===================================================================
--- /trunk/src/VBox/Main/hgcm/HGCM.cpp	(revision 1384)
+++ /trunk/src/VBox/Main/hgcm/HGCM.cpp	(revision 1385)
@@ -134,4 +134,8 @@
         VBOXHGCMSVCFNTABLE m_fntable;
 
+        int m_cClients;
+        int m_cClientsAllocated;
+
+        uint32_t *m_paClientIds;
 
         int loadServiceDLL (void);
@@ -150,4 +154,6 @@
     public:
 
+        static void Reset (void);
+    
         static int FindService (HGCMService **ppsvc, HGCMServiceLocation *loc);
         static HGCMService *FindServiceByName (const char *pszServiceName);
@@ -156,4 +162,6 @@
 
         uint32_t SizeOfClient (void) { return m_fntable.cbClient; };
+
+        void DisconnectAll (void);
 
         int Connect (uint32_t u32ClientID);
@@ -279,4 +287,5 @@
 #define HGCMMSGID_LOADSTATE  (14)
 #define HGCMMSGID_SAVESTATE  (15)
+#define HGCMMSGID_RESET      (16)
 
 class HGCMMsgConnect: public HGCMMsgHeader
@@ -348,4 +357,8 @@
 
         VBOXHGCMSVCPARM *paParms;
+};
+
+class HGCMMsgReset: public HGCMMsgHeader
+{
 };
 
@@ -381,5 +394,8 @@
     m_pszSvcLibrary (NULL),
     m_hLdrMod    (NIL_RTLDRMOD),
-    m_pfnLoad    (NULL)
+    m_pfnLoad    (NULL),
+    m_cClients   (0),
+    m_cClientsAllocated (0),
+    m_paClientIds (NULL)
 {
     memset (&m_fntable, 0, sizeof (m_fntable));
@@ -403,6 +419,8 @@
         case HGCMMSGID_LOADSTATE:      
         case HGCMMSGID_SAVESTATE:       return new HGCMMsgLoadSaveState ();
+        case HGCMMSGID_RESET:           return new HGCMMsgReset ();
         default:
             Log(("hgcmMessageAlloc::Unsupported message number %08X\n", u32MsgId));
+            AssertReleaseMsgFailed(("Msg id = %08X\n", u32MsgId));
     }
 
@@ -410,4 +428,5 @@
 }
 
+static g_fResetting = false;
 
 static DECLCALLBACK(void) hgcmMsgCompletionCallback (int32_t result, HGCMMsgCore *pMsgCore)
@@ -418,5 +437,5 @@
     LogFlow(("MAIN::hgcmMsgCompletionCallback: message %p\n", pMsgCore));
 
-    if (pMsgHdr->pHGCMPort)
+    if (pMsgHdr->pHGCMPort && !g_fResetting)
     {
         pMsgHdr->pHGCMPort->pfnCompleted (pMsgHdr->pHGCMPort, result, pMsgHdr->pCmd);
@@ -599,6 +618,5 @@
     RTStrPrintf (achThreadName, sizeof (achThreadName), "HGCM%08X", this);
 
-    /* @todo do a proper fix 0x12345678 -> sizeof */
-    rc = hgcmThreadCreate (&m_thread, achThreadName, hgcmServiceThread, this, 0x12345678 /* sizeof (HGCMMsgCall) */);
+    rc = hgcmThreadCreate (&m_thread, achThreadName, hgcmServiceThread, this);
 
     if (VBOX_SUCCESS(rc))
@@ -627,5 +645,5 @@
             HGCMMSGHANDLE hMsg;
 
-            rc = hgcmMsgAlloc (m_thread, &hMsg, HGCMMSGID_SVC_LOAD, sizeof (HGCMMsgSvcLoad), hgcmMessageAlloc);
+            rc = hgcmMsgAlloc (m_thread, &hMsg, HGCMMSGID_SVC_LOAD, hgcmMessageAlloc);
 
             if (VBOX_SUCCESS(rc))
@@ -656,5 +674,5 @@
     LogFlow(("HGCMService::InstanceDestroy\n"));
 
-    int rc = hgcmMsgAlloc (m_thread, &hMsg, HGCMMSGID_SVC_UNLOAD, sizeof (HGCMMsgSvcUnload), hgcmMessageAlloc);
+    int rc = hgcmMsgAlloc (m_thread, &hMsg, HGCMMSGID_SVC_UNLOAD, hgcmMessageAlloc);
 
     if (VBOX_SUCCESS(rc))
@@ -803,4 +821,25 @@
 }
 
+/* static */ void HGCMService::Reset (void)
+{
+    /* This is called when the VM is being reset,
+     * that is no more requests from guest is expected.
+     * Scan al services and disconnect all clients.
+     */
+    
+    g_fResetting = true;
+
+    HGCMService *psvc = sm_pSvcListHead;
+
+    while (psvc)
+    {
+        psvc->DisconnectAll ();
+
+        psvc = psvc->m_pSvcNext;
+    }
+    
+    g_fResetting = false;
+}
+
 void HGCMService::ReleaseService (void)
 {
@@ -926,5 +965,5 @@
     LogFlow(("MAIN::HGCMService::Connect: client id = %d\n", u32ClientID));
 
-    int rc = hgcmMsgAlloc (m_thread, &hMsg, HGCMMSGID_SVC_CONNECT, sizeof (HGCMMsgSvcConnect), hgcmMessageAlloc);
+    int rc = hgcmMsgAlloc (m_thread, &hMsg, HGCMMSGID_SVC_CONNECT, hgcmMessageAlloc);
 
     if (VBOX_SUCCESS(rc))
@@ -939,4 +978,17 @@
 
         rc = hgcmMsgSend (hMsg);
+        
+        if (VBOX_SUCCESS (rc))
+        {
+            /* Add the client Id to the array. */
+            if (m_cClients == m_cClientsAllocated)
+            {
+                m_paClientIds = (uint32_t *)RTMemRealloc (m_paClientIds, (m_cClientsAllocated + 64) * sizeof (m_paClientIds[0]));
+                Assert(m_paClientIds);
+            }
+            
+            m_paClientIds[m_cClients] = u32ClientID;
+            m_cClients++;
+        }
     }
     else
@@ -956,5 +1008,5 @@
     HGCMMSGHANDLE hMsg;
 
-    rc = hgcmMsgAlloc (m_thread, &hMsg, HGCMMSGID_SVC_DISCONNECT, sizeof (HGCMMsgSvcDisconnect), hgcmMessageAlloc);
+    rc = hgcmMsgAlloc (m_thread, &hMsg, HGCMMSGID_SVC_DISCONNECT, hgcmMessageAlloc);
 
     if (VBOX_SUCCESS(rc))
@@ -969,4 +1021,21 @@
 
         rc = hgcmMsgSend (hMsg);
+        
+        /* Remove the client id from the array. */
+        int i;
+        for (i = 0; i < m_cClients; i++)
+        {
+            if (m_paClientIds[i] == u32ClientID)
+            {
+                m_cClients--;
+                
+                if (m_cClients > i)
+                {
+                    memmove (&m_paClientIds[i], &m_paClientIds[i + 1], m_cClients - i);
+                }
+                
+                break;
+            }
+        }
     }
     else
@@ -978,4 +1047,13 @@
 }
 
+void HGCMService::DisconnectAll (void)
+{
+    while (m_cClients && m_paClientIds)
+    {
+        Log(("MAIN::HGCMService::DisconnectAll: id %d\n", m_paClientIds[0]));
+        Disconnect (m_paClientIds[0]);
+    }
+}
+
 /* Forward the call request to the dedicated service thread.
  */
@@ -986,5 +1064,5 @@
     LogFlow(("MAIN::HGCMService::Call\n"));
 
-    int rc = hgcmMsgAlloc (m_thread, &hMsg, HGCMMSGID_GUESTCALL, sizeof (HGCMMsgCall), hgcmMessageAlloc);
+    int rc = hgcmMsgAlloc (m_thread, &hMsg, HGCMMSGID_GUESTCALL, hgcmMessageAlloc);
 
     if (VBOX_SUCCESS(rc))
@@ -1030,5 +1108,5 @@
     LogFlow(("MAIN::HGCMService::HostCall %s\n", m_pszSvcName));
 
-    int rc = hgcmMsgAlloc (m_thread, &hMsg, HGCMMSGID_HOSTCALL, sizeof (HGCMMsgHostCall), hgcmMessageAlloc);
+    int rc = hgcmMsgAlloc (m_thread, &hMsg, HGCMMSGID_HOSTCALL, hgcmMessageAlloc);
 
     if (VBOX_SUCCESS(rc))
@@ -1063,5 +1141,5 @@
     LogFlow(("MAIN::HGCMService::SaveState %s\n", m_pszSvcName));
 
-    int rc = hgcmMsgAlloc (m_thread, &hMsg, HGCMMSGID_SAVESTATE, sizeof (HGCMMsgLoadSaveState), hgcmMessageAlloc);
+    int rc = hgcmMsgAlloc (m_thread, &hMsg, HGCMMSGID_SAVESTATE, hgcmMessageAlloc);
 
     if (VBOX_SUCCESS(rc))
@@ -1092,5 +1170,5 @@
     LogFlow(("MAIN::HGCMService::LoadState %s\n", m_pszSvcName));
 
-    int rc = hgcmMsgAlloc (m_thread, &hMsg, HGCMMSGID_LOADSTATE, sizeof (HGCMMsgLoadSaveState), hgcmMessageAlloc);
+    int rc = hgcmMsgAlloc (m_thread, &hMsg, HGCMMSGID_LOADSTATE, hgcmMessageAlloc);
 
     if (VBOX_SUCCESS(rc))
@@ -1242,5 +1320,4 @@
             } break;
 
-
             case HGCMMSGID_HOSTCALL:
             {
@@ -1258,4 +1335,13 @@
             } break;
 
+            case HGCMMSGID_RESET:
+            {
+                LogFlow(("HGCMMSGID_RESET\n"));
+
+                HGCMMsgReset *pMsg = (HGCMMsgReset *)pMsgCore;
+
+                HGCMService::Reset ();
+            } break;
+
             default:
             {
@@ -1291,5 +1377,5 @@
     HGCMMSGHANDLE hMsg = 0;
 
-    rc = hgcmMsgAlloc (g_hgcmThread, &hMsg, HGCMMSGID_CONNECT, sizeof (HGCMMsgConnect), hgcmMessageAlloc);
+    rc = hgcmMsgAlloc (g_hgcmThread, &hMsg, HGCMMSGID_CONNECT, hgcmMessageAlloc);
 
     if (VBOX_SUCCESS(rc))
@@ -1346,5 +1432,5 @@
     HGCMMSGHANDLE hMsg = 0;
 
-    rc = hgcmMsgAlloc (g_hgcmThread, &hMsg, HGCMMSGID_DISCONNECT, sizeof (HGCMMsgDisconnect), hgcmMessageAlloc);
+    rc = hgcmMsgAlloc (g_hgcmThread, &hMsg, HGCMMSGID_DISCONNECT, hgcmMessageAlloc);
 
     if (VBOX_SUCCESS(rc))
@@ -1439,5 +1525,5 @@
     HGCMMSGHANDLE hMsg = 0;
 
-    rc = hgcmMsgAlloc (g_hgcmThread, &hMsg, HGCMMSGID_LOAD, sizeof (HGCMMsgLoad), hgcmMessageAlloc);
+    rc = hgcmMsgAlloc (g_hgcmThread, &hMsg, HGCMMSGID_LOAD, hgcmMessageAlloc);
 
     if (VBOX_SUCCESS(rc))
@@ -1520,5 +1606,5 @@
     HGCMMSGHANDLE hMsg = 0;
 
-    rc = hgcmMsgAlloc (g_hgcmThread, &hMsg, HGCMMSGID_HOSTCALL, sizeof (HGCMMsgHostCall), hgcmMessageAlloc);
+    rc = hgcmMsgAlloc (g_hgcmThread, &hMsg, HGCMMSGID_HOSTCALL, hgcmMessageAlloc);
 
     if (VBOX_SUCCESS(rc))
@@ -1566,6 +1652,5 @@
         /* Start main HGCM thread that will process Connect/Disconnect requests. */
 
-        /* @todo do a proper fix 0x12345678 -> sizeof */
-        rc = hgcmThreadCreate (&g_hgcmThread, "MainHGCMthread", hgcmThread, NULL, 0x12345678 /*sizeof (HGCMMsgConnect)*/);
+        rc = hgcmThreadCreate (&g_hgcmThread, "MainHGCMthread", hgcmThread, NULL);
 
         if (VBOX_FAILURE (rc))
@@ -1579,2 +1664,35 @@
     return rc;
 }
+
+int hgcmReset (void)
+{
+    int rc = VINF_SUCCESS;
+    Log(("MAIN::hgcmReset\n"));
+    
+    /* Disconnect all clients. */
+    HGCMMSGHANDLE hMsg = 0;
+
+    rc = hgcmMsgAlloc (g_hgcmThread, &hMsg, HGCMMSGID_RESET, hgcmMessageAlloc);
+
+    if (VBOX_SUCCESS(rc))
+    {
+        HGCMMsgReset *pMsg = (HGCMMsgReset *)hgcmObjReference (hMsg, HGCMOBJ_MSG);
+
+        AssertRelease(pMsg);
+
+        pMsg->pHGCMPort = NULL; /* Not used by the call. */
+        
+        rc = hgcmMsgSend (hMsg);
+
+        hgcmObjDereference (pMsg);
+
+        LogFlow(("MAIN::hgcmReset: hgcmMsgSend returned %Vrc\n", rc));
+    }
+    else
+    {
+        Log(("MAIN::hgcmReset:Message allocation failed: %Vrc\n", rc));
+    }
+    
+    LogFlow(("MAIN::hgcmReset: rc = %Vrc\n", rc));
+    return rc;
+}
Index: /trunk/src/VBox/Main/hgcm/HGCMThread.cpp
===================================================================
--- /trunk/src/VBox/Main/hgcm/HGCMThread.cpp	(revision 1384)
+++ /trunk/src/VBox/Main/hgcm/HGCMThread.cpp	(revision 1385)
@@ -134,7 +134,7 @@
         HGCMThread ();
 
-        int Initialize (HGCMTHREADHANDLE handle, const char *pszThreadName, PFNHGCMTHREAD pfnThread, void *pvUser, uint32_t cbMsg);
-
-        int MsgAlloc (HGCMMSGHANDLE *pHandle, uint32_t u32MsgId, uint32_t cbMsg, PFNHGCMNEWMSGALLOC pfnNewMessage);
+        int Initialize (HGCMTHREADHANDLE handle, const char *pszThreadName, PFNHGCMTHREAD pfnThread, void *pvUser);
+
+        int MsgAlloc (HGCMMSGHANDLE *pHandle, uint32_t u32MsgId, PFNHGCMNEWMSGALLOC pfnNewMessage);
         int MsgGet (HGCMMsgCore **ppMsg);
         int MsgPost (HGCMMsgCore *pMsg, PHGCMMSGCALLBACK pfnCallback, bool bWait);
@@ -151,5 +151,5 @@
 #define HGCM_MSG_F_IN_PROCESS (0x00000004)
 
-void HGCMMsgCore::InitializeCore (uint32_t cbMsg, uint32_t u32MsgId, HGCMThread *pThread)
+void HGCMMsgCore::InitializeCore (uint32_t u32MsgId, HGCMThread *pThread)
 {
     m_u32Version  = HGCMMSG_VERSION;
@@ -248,5 +248,5 @@
 }
 
-int HGCMThread::Initialize (HGCMTHREADHANDLE handle, const char *pszThreadName, PFNHGCMTHREAD pfnThread, void *pvUser, uint32_t cbMsg)
+int HGCMThread::Initialize (HGCMTHREADHANDLE handle, const char *pszThreadName, PFNHGCMTHREAD pfnThread, void *pvUser)
 {
     int rc = VINF_SUCCESS;
@@ -328,5 +328,5 @@
 
 
-int HGCMThread::MsgAlloc (HGCMMSGHANDLE *pHandle, uint32_t u32MsgId, uint32_t cbMsg, PFNHGCMNEWMSGALLOC pfnNewMessage)
+int HGCMThread::MsgAlloc (HGCMMSGHANDLE *pHandle, uint32_t u32MsgId, PFNHGCMNEWMSGALLOC pfnNewMessage)
 {
     int rc = VINF_SUCCESS;
@@ -353,5 +353,5 @@
 
         /* Initialize just allocated message core */
-        pmsg->InitializeCore (cbMsg, u32MsgId, this);
+        pmsg->InitializeCore (u32MsgId, this);
 
         /* and the message specific data. */
@@ -591,5 +591,5 @@
  */
 
-int hgcmThreadCreate (HGCMTHREADHANDLE *pHandle, const char *pszThreadName, PFNHGCMTHREAD pfnThread, void *pvUser, uint32_t cbMsg)
+int hgcmThreadCreate (HGCMTHREADHANDLE *pHandle, const char *pszThreadName, PFNHGCMTHREAD pfnThread, void *pvUser)
 {
     int rc = VINF_SUCCESS;
@@ -608,5 +608,5 @@
 
         /* Initialize the object. */
-        rc = pThread->Initialize (handle, pszThreadName, pfnThread, pvUser, cbMsg);
+        rc = pThread->Initialize (handle, pszThreadName, pfnThread, pvUser);
     }
     else
@@ -636,9 +636,9 @@
 }
 
-int hgcmMsgAlloc (HGCMTHREADHANDLE hThread, HGCMMSGHANDLE *pHandle, uint32_t u32MsgId, uint32_t cbMsg, PFNHGCMNEWMSGALLOC pfnNewMessage)
-{
-    LogFlow(("hgcmMsgAlloc: thread handle = %d, pHandle = %p, cbMsg = %d, sizeof (HGCMMsgCore) = %d\n", hThread, pHandle, cbMsg, sizeof (HGCMMsgCore)));
-
-    if (!pHandle || cbMsg < sizeof (HGCMMsgCore))
+int hgcmMsgAlloc (HGCMTHREADHANDLE hThread, HGCMMSGHANDLE *pHandle, uint32_t u32MsgId, PFNHGCMNEWMSGALLOC pfnNewMessage)
+{
+    LogFlow(("hgcmMsgAlloc: thread handle = %d, pHandle = %p, sizeof (HGCMMsgCore) = %d\n", hThread, pHandle, sizeof (HGCMMsgCore)));
+
+    if (!pHandle)
     {
         return VERR_INVALID_PARAMETER;
@@ -655,5 +655,5 @@
     else
     {
-        rc = pThread->MsgAlloc (pHandle, u32MsgId, cbMsg, pfnNewMessage);
+        rc = pThread->MsgAlloc (pHandle, u32MsgId, pfnNewMessage);
 
         hgcmObjDereference (pThread);
Index: /trunk/src/VBox/Main/include/hgcm/HGCM.h
===================================================================
--- /trunk/src/VBox/Main/include/hgcm/HGCM.h	(revision 1384)
+++ /trunk/src/VBox/Main/include/hgcm/HGCM.h	(revision 1385)
@@ -36,4 +36,6 @@
 int hgcmInit (void);
 
+int hgcmReset (void);
+
 int hgcmLoadInternal (const char *pszServiceName, const char *pszServiceLibrary);
 
Index: /trunk/src/VBox/Main/include/hgcm/HGCMThread.h
===================================================================
--- /trunk/src/VBox/Main/include/hgcm/HGCMThread.h	(revision 1384)
+++ /trunk/src/VBox/Main/include/hgcm/HGCMThread.h	(revision 1385)
@@ -84,5 +84,5 @@
         int32_t m_rcSend;
 
-        void InitializeCore (uint32_t cbMsg, uint32_t u32MsgId, HGCMThread *pThread);
+        void InitializeCore (uint32_t u32MsgId, HGCMThread *pThread);
 
     protected:
@@ -132,9 +132,8 @@
  * @param pfnThread     The worker thread function.
  * @param pvUser        A pointer passed to worker thread.
- * @param cbMsg         Size of a typical message.
  *
  * @return VBox error code
  */
-int hgcmThreadCreate (HGCMTHREADHANDLE *pHandle, const char *pszThreadName, PFNHGCMTHREAD pfnThread, void *pvUser, uint32_t cbMsg);
+int hgcmThreadCreate (HGCMTHREADHANDLE *pHandle, const char *pszThreadName, PFNHGCMTHREAD pfnThread, void *pvUser);
 
 /** Allocate a message to be posted to HGCM worker thread.
@@ -143,10 +142,9 @@
  * @param pHandle       Where to store the returned message handle.
  * @param u32MsgId      Message identifier.
- * @param cbMsg         Size of message.
  * @param pfnNewMessage New message allocation callback.
  *
  * @return VBox error code
  */
-int hgcmMsgAlloc (HGCMTHREADHANDLE hThread, HGCMMSGHANDLE *pHandle, uint32_t u32MsgId, uint32_t cbMsg, PFNHGCMNEWMSGALLOC pfnNewMessage);
+int hgcmMsgAlloc (HGCMTHREADHANDLE hThread, HGCMMSGHANDLE *pHandle, uint32_t u32MsgId, PFNHGCMNEWMSGALLOC pfnNewMessage);
 
 /** Post a message to HGCM worker thread.
