Index: trunk/src/VBox/HostServices/HostChannel/HostChannel.cpp
===================================================================
--- trunk/src/VBox/HostServices/HostChannel/HostChannel.cpp	(revision 43898)
+++ trunk/src/VBox/HostServices/HostChannel/HostChannel.cpp	(revision 43899)
@@ -712,15 +712,23 @@
 } VBOXHOSTCHANNELEVENT;
 
-/* This is called under the lock. */
-int vboxHostChannelQueryEvent(VBOXHOSTCHCLIENT *pClient,
-                              bool *pfEvent,
-                              uint32_t *pu32Handle,
-                              uint32_t *pu32Id,
-                              void *pvParm,
-                              uint32_t cbParm,
-                              uint32_t *pcbParmOut)
-{
+int vboxHostChannelEventWait(VBOXHOSTCHCLIENT *pClient,
+                             bool *pfEvent,
+                             VBOXHGCMCALLHANDLE callHandle,
+                             VBOXHGCMSVCPARM *paParms)
+{
+    int rc = vboxHostChannelLock();
+    if (RT_FAILURE(rc))
+    {
+        return rc;
+    }
+
+    if (pClient->fAsync)
+    {
+        /* If there is a wait request already, cancel it. */
+        vboxHostChannelReportAsync(pClient, 0, VBOX_HOST_CHANNEL_EVENT_CANCELLED, NULL, 0);
+        pClient->fAsync = false;
+    }
+
     /* Check if there is something in the client's event queue. */
-
     VBOXHOSTCHANNELEVENT *pEvent = RTListGetFirst(&pClient->listEvents, VBOXHOSTCHANNELEVENT, NodeEvent);
 
@@ -732,19 +740,11 @@
         RTListNodeRemove(&pEvent->NodeEvent);
 
+        HOSTCHLOG(("HostChannel: QueryEvent: (%d), cbEvent %d\n",
+                   pClient->u32ClientID, pEvent->cbEvent));
+
+        vboxHostChannelEventParmsSet(paParms, pEvent->u32ChannelHandle,
+                                     pEvent->u32Id, pEvent->pvEvent, pEvent->cbEvent);
+
         *pfEvent = true;
-        *pu32Handle = pEvent->u32ChannelHandle;
-        *pu32Id = pEvent->u32Id;
-
-        uint32_t cbToCopy = RT_MIN(cbParm, pEvent->cbEvent);
-
-        HOSTCHLOG(("HostChannel: QueryEvent: (%d), cbParm %d, cbEvent %d\n",
-                        pClient->u32ClientID, cbParm, pEvent->cbEvent));
-
-        if (cbToCopy > 0)
-        {
-            memcpy(pvParm, pEvent->pvEvent, cbToCopy);
-        }
-
-        *pcbParmOut = cbToCopy;
 
         RTMemFree(pEvent);
@@ -752,10 +752,37 @@
     else
     {
+        /* No event available at the time. Process asynchronously. */
+        pClient->fAsync           = true;
+        pClient->async.callHandle = callHandle;
+        pClient->async.paParms    = paParms;
+
         /* Tell the caller that there is no event. */
         *pfEvent = false;
     }
 
-    return VINF_SUCCESS;
-}
+    vboxHostChannelUnlock();
+    return rc;
+}
+
+int vboxHostChannelEventCancel(VBOXHOSTCHCLIENT *pClient)
+{
+    int rc = vboxHostChannelLock();
+
+    if (RT_SUCCESS(rc))
+    {
+        if (pClient->fAsync)
+        {
+            /* If there is a wait request alredy, cancel it. */
+            vboxHostChannelReportAsync(pClient, 0, VBOX_HOST_CHANNEL_EVENT_CANCELLED, NULL, 0);
+
+            pClient->fAsync = false;
+        }
+
+        vboxHostChannelUnlock();
+    }
+
+    return rc;
+}
+
 
 /* @thread provider */
Index: trunk/src/VBox/HostServices/HostChannel/HostChannel.h
===================================================================
--- trunk/src/VBox/HostServices/HostChannel/HostChannel.h	(revision 43898)
+++ trunk/src/VBox/HostServices/HostChannel/HostChannel.h	(revision 43899)
@@ -98,11 +98,10 @@
                            uint32_t *pu32SizeDataReturned);
 
-int vboxHostChannelQueryEvent(VBOXHOSTCHCLIENT *pClient,
-                              bool *pfEvent,
-                              uint32_t *pu32Handle,
-                              uint32_t *pu32Id,
-                              void *pvParm,
-                              uint32_t cbParm,
-                              uint32_t *pcbParmOut);
+int vboxHostChannelEventWait(VBOXHOSTCHCLIENT *pClient,
+                             bool *pfEvent,
+                             VBOXHGCMCALLHANDLE callHandle,
+                             VBOXHGCMSVCPARM *paParms);
+
+int vboxHostChannelEventCancel(VBOXHOSTCHCLIENT *pClient);
 
 int vboxHostChannelQuery(VBOXHOSTCHCLIENT *pClient,
@@ -121,6 +120,15 @@
 
 
-void vboxHostChannelReportAsync(VBOXHOSTCHCLIENT *pClient, uint32_t u32ChannelHandle,
-                                uint32_t u32Id, const void *pvEvent, uint32_t cbEvent);
+void vboxHostChannelEventParmsSet(VBOXHGCMSVCPARM *paParms,
+                                  uint32_t u32ChannelHandle,
+                                  uint32_t u32Id,
+                                  const void *pvEvent,
+                                  uint32_t cbEvent);
+
+void vboxHostChannelReportAsync(VBOXHOSTCHCLIENT *pClient,
+                                uint32_t u32ChannelHandle,
+                                uint32_t u32Id,
+                                const void *pvEvent,
+                                uint32_t cbEvent);
 
 #endif /* __VBOXHOSTCHANNEL__H */
Index: trunk/src/VBox/HostServices/HostChannel/service.cpp
===================================================================
--- trunk/src/VBox/HostServices/HostChannel/service.cpp	(revision 43898)
+++ trunk/src/VBox/HostServices/HostChannel/service.cpp	(revision 43899)
@@ -107,4 +107,30 @@
 }
 
+void vboxHostChannelEventParmsSet(VBOXHGCMSVCPARM *paParms,
+                                  uint32_t u32ChannelHandle,
+                                  uint32_t u32Id,
+                                  const void *pvEvent,
+                                  uint32_t cbEvent)
+{
+    if (cbEvent > 0)
+    {
+        void *pvParm = NULL;
+        uint32_t cbParm = 0;
+
+        VBoxHGCMParmPtrGet(&paParms[2], &pvParm, &cbParm);
+
+        uint32_t cbToCopy = RT_MIN(cbParm, cbEvent);
+        if (cbToCopy > 0)
+        {
+            Assert(pvParm);
+            memcpy(pvParm, pvEvent, cbToCopy);
+        }
+    }
+
+    VBoxHGCMParmUInt32Set(&paParms[0], u32ChannelHandle);
+    VBoxHGCMParmUInt32Set(&paParms[1], u32Id);
+    VBoxHGCMParmUInt32Set(&paParms[3], cbEvent);
+}
+
 /* This is called under the lock. */
 void vboxHostChannelReportAsync(VBOXHOSTCHCLIENT *pClient,
@@ -114,21 +140,11 @@
                                 uint32_t cbEvent)
 {
-    if (cbEvent > 0)
-    {
-        void *pvParm = NULL;
-        uint32_t cbParm = 0;
-
-        VBoxHGCMParmPtrGet(&pClient->async.paParms[2], &pvParm, &cbParm);
-
-        uint32_t cbToCopy = RT_MIN(cbParm, cbEvent);
-        if (cbToCopy > 0)
-        {
-            memcpy(pvParm, pvEvent, cbToCopy);
-        }
-    }
-
-    VBoxHGCMParmUInt32Set(&pClient->async.paParms[0], u32ChannelHandle);
-    VBoxHGCMParmUInt32Set(&pClient->async.paParms[1], u32Id);
-    VBoxHGCMParmUInt32Set(&pClient->async.paParms[3], cbEvent);
+    Assert(RTCritSectIsOwner(&g_critsect));
+
+    vboxHostChannelEventParmsSet(pClient->async.paParms,
+                                 u32ChannelHandle,
+                                 u32Id,
+                                 pvEvent,
+                                 cbEvent);
 
     LogRelFlow(("svcCall: CallComplete for pending\n"));
@@ -438,54 +454,16 @@
             else
             {
-                void *pvParm;
-                uint32_t cbParm;
-
-                rc = VBoxHGCMParmPtrGet(&paParms[2], &pvParm, &cbParm);
+                bool fEvent = false;
+
+                rc = vboxHostChannelEventWait(pClient, &fEvent, callHandle, paParms);
 
                 if (RT_SUCCESS(rc))
                 {
-                    /* This is accessed from the SVC thread and other threads. */
-                    rc = vboxHostChannelLock();
-
-                    if (RT_SUCCESS(rc))
+                    if (!fEvent)
                     {
-                        if (pClient->fAsync)
-                        {
-                            /* If there is a wait request already, cancel it. */
-                            vboxHostChannelReportAsync(pClient, 0, VBOX_HOST_CHANNEL_EVENT_CANCELLED, NULL, 0);
-
-                            pClient->fAsync = false;
-                        }
-
-                        bool fEvent = false;
-                        uint32_t u32Handle = 0;
-                        uint32_t u32Id = 0;
-                        uint32_t cbParmOut = 0;
-
-                        rc = vboxHostChannelQueryEvent(pClient, &fEvent, &u32Handle, &u32Id,
-                                                       pvParm, cbParm, &cbParmOut);
-
-                        if (RT_SUCCESS(rc))
-                        {
-                            if (fEvent)
-                            {
-                                VBoxHGCMParmUInt32Set(&paParms[0], u32Handle);
-                                VBoxHGCMParmUInt32Set(&paParms[1], u32Id);
-                                VBoxHGCMParmUInt32Set(&paParms[3], cbParmOut);
-                            }
-                            else
-                            {
-                                /* No event available at the time. Process asynchronously. */
-                                fAsynchronousProcessing = true;
-
-                                pClient->fAsync           = true;
-                                pClient->async.callHandle = callHandle;
-                                pClient->async.paParms    = paParms;
-
-                                LogRel2(("svcCall: async.\n"));
-                            }
-                        }
-
-                        vboxHostChannelUnlock();
+                        /* No event available at the time. Process asynchronously. */
+                        fAsynchronousProcessing = true;
+
+                        LogRel2(("svcCall: async.\n"));
                     }
                 }
@@ -503,19 +481,5 @@
             else
             {
-                /* This is accessed from the SVC thread and other threads. */
-                rc = vboxHostChannelLock();
-
-                if (RT_SUCCESS(rc))
-                {
-                    if (pClient->fAsync)
-                    {
-                        /* If there is a wait request alredy, cancel it. */
-                        vboxHostChannelReportAsync(pClient, 0, VBOX_HOST_CHANNEL_EVENT_CANCELLED, NULL, 0);
-
-                        pClient->fAsync = false;
-                    }
-
-                    vboxHostChannelUnlock();
-                }
+                rc = vboxHostChannelEventCancel(pClient);
             }
         } break;
