Index: trunk/src/VBox/GuestHost/SharedClipboard/clipboard-common.cpp
===================================================================
--- trunk/src/VBox/GuestHost/SharedClipboard/clipboard-common.cpp	(revision 80467)
+++ trunk/src/VBox/GuestHost/SharedClipboard/clipboard-common.cpp	(revision 80468)
@@ -24,4 +24,5 @@
 #include <iprt/semaphore.h>
 #include <iprt/path.h>
+#include <iprt/rand.h>
 
 #include <iprt/errcore.h>
@@ -35,5 +36,5 @@
  *
  * @returns VBox status code.
- * @param   uID                 Event ID to associate payload to.
+ * @param   uID                 Payload ID to set for this payload. Useful for consequtive payloads.
  * @param   pvData              Data block to associate to this payload.
  * @param   cbData              Size (in bytes) of data block to associate.
@@ -97,7 +98,7 @@
  * @returns VBox status code.
  * @param   pEvent              Event to initialize.
- * @param   uID                 ID to use for the event.
- */
-int SharedClipboardEventCreate(PSHAREDCLIPBOARDEVENT pEvent, uint16_t uID)
+ * @param   uID                 Event ID to use.
+ */
+int SharedClipboardEventCreate(PSHAREDCLIPBOARDEVENT pEvent, VBOXCLIPBOARDEVENTID uID)
 {
     AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
@@ -145,5 +146,5 @@
  * @param   uID                 ID to use for event source.
  */
-int SharedClipboardEventSourceCreate(PSHAREDCLIPBOARDEVENTSOURCE pSource, uint16_t uID)
+int SharedClipboardEventSourceCreate(PSHAREDCLIPBOARDEVENTSOURCE pSource, VBOXCLIPBOARDEVENTSOURCEID uID)
 {
     AssertPtrReturn(pSource, VERR_INVALID_POINTER);
@@ -156,5 +157,6 @@
 
     pSource->uID          = uID;
-    pSource->uEventIDNext = 1; /* Event ID 0 always is reserved (marks "unused"). */
+    /* Choose a random event ID starting point. */
+    pSource->uEventIDNext = RTRandU32() % VBOX_SHARED_CLIPBOARD_MAX_EVENTS;
 
     LogFlowFuncLeaveRC(rc);
@@ -189,10 +191,10 @@
  * @param   pSource             Event source to generate event for.
  */
-uint16_t SharedClipboardEventIDGenerate(PSHAREDCLIPBOARDEVENTSOURCE pSource)
+VBOXCLIPBOARDEVENTID SharedClipboardEventIDGenerate(PSHAREDCLIPBOARDEVENTSOURCE pSource)
 {
     AssertPtrReturn(pSource, 0);
 
     LogFlowFunc(("uSource=%RU16: New event: %RU16\n", pSource->uID, pSource->uEventIDNext));
-    return pSource->uEventIDNext++; /** @todo Improve this. */
+    return pSource->uEventIDNext++; /** @todo Handle rollovers? */
 }
 
@@ -204,5 +206,5 @@
  * @param   uID                 Event ID to get.
  */
-inline PSHAREDCLIPBOARDEVENT sharedClipboardEventGet(PSHAREDCLIPBOARDEVENTSOURCE pSource, uint16_t uID)
+inline PSHAREDCLIPBOARDEVENT sharedClipboardEventGet(PSHAREDCLIPBOARDEVENTSOURCE pSource, VBOXCLIPBOARDEVENTID uID)
 {
     PSHAREDCLIPBOARDEVENT pEvIt;
@@ -223,5 +225,5 @@
  * @param   uID                 Event ID to register.
  */
-int SharedClipboardEventRegister(PSHAREDCLIPBOARDEVENTSOURCE pSource, uint16_t uID)
+int SharedClipboardEventRegister(PSHAREDCLIPBOARDEVENTSOURCE pSource, VBOXCLIPBOARDEVENTID uID)
 {
     AssertPtrReturn(pSource, VERR_INVALID_POINTER);
@@ -266,5 +268,5 @@
  * @param   uID                 Event ID to unregister.
  */
-int SharedClipboardEventUnregister(PSHAREDCLIPBOARDEVENTSOURCE pSource, uint16_t uID)
+int SharedClipboardEventUnregister(PSHAREDCLIPBOARDEVENTSOURCE pSource, VBOXCLIPBOARDEVENTID uID)
 {
     AssertPtrReturn(pSource, VERR_INVALID_POINTER);
@@ -305,5 +307,5 @@
  *                              SharedClipboardPayloadFree().
  */
-int SharedClipboardEventWait(PSHAREDCLIPBOARDEVENTSOURCE pSource, uint16_t uID, RTMSINTERVAL uTimeoutMs,
+int SharedClipboardEventWait(PSHAREDCLIPBOARDEVENTSOURCE pSource, VBOXCLIPBOARDEVENTID uID, RTMSINTERVAL uTimeoutMs,
                              PSHAREDCLIPBOARDEVENTPAYLOAD* ppPayload)
 {
@@ -340,5 +342,5 @@
  * @param   pPayload            Event payload to associate. Takes ownership. Optional.
  */
-int SharedClipboardEventSignal(PSHAREDCLIPBOARDEVENTSOURCE pSource, uint16_t uID,
+int SharedClipboardEventSignal(PSHAREDCLIPBOARDEVENTSOURCE pSource, VBOXCLIPBOARDEVENTID uID,
                                PSHAREDCLIPBOARDEVENTPAYLOAD pPayload)
 {
@@ -376,5 +378,5 @@
  * @param   uID                 Event ID to detach payload for.
  */
-void SharedClipboardEventPayloadDetach(PSHAREDCLIPBOARDEVENTSOURCE pSource, uint16_t uID)
+void SharedClipboardEventPayloadDetach(PSHAREDCLIPBOARDEVENTSOURCE pSource, VBOXCLIPBOARDEVENTID uID)
 {
     AssertPtrReturnVoid(pSource);
Index: trunk/src/VBox/HostServices/SharedClipboard/VBoxSharedClipboardSvc-internal.h
===================================================================
--- trunk/src/VBox/HostServices/SharedClipboard/VBoxSharedClipboardSvc-internal.h	(revision 80467)
+++ trunk/src/VBox/HostServices/SharedClipboard/VBoxSharedClipboardSvc-internal.h	(revision 80468)
@@ -181,10 +181,25 @@
 } VBOXCLIPBOARDCLIENT, *PVBOXCLIPBOARDCLIENT;
 
+/**
+ * Structure for keeping a single (HGCM) client map entry.
+ * Currently empty.
+ */
 typedef struct _VBOXCLIPBOARDCLIENTMAPENTRY
 {
 } VBOXCLIPBOARDCLIENTMAPENTRY;
 
-/** Map holding pointers to drag and drop clients. Key is the (unique) HGCM client ID. */
+/**
+ * Structure for keeping a single event source map entry.
+ * Currently empty.
+ */
+typedef struct _VBOXCLIPBOARDEVENTSOURCEMAPENTRY
+{
+} VBOXCLIPBOARDEVENTSOURCEMAPENTRY;
+
+/** Map holding information about connected HGCM clients. Key is the (unique) HGCM client ID. */
 typedef std::map<uint32_t, VBOXCLIPBOARDCLIENTMAPENTRY> ClipboardClientMap;
+
+/** Map holding information about event sources. Key is the (unique) event source ID. */
+typedef std::map<VBOXCLIPBOARDEVENTSOURCEID, VBOXCLIPBOARDEVENTSOURCEMAPENTRY> ClipboardEventSourceMap;
 
 /** Simple queue (list) which holds deferred (waiting) clients. */
Index: trunk/src/VBox/HostServices/SharedClipboard/VBoxSharedClipboardSvc-uri.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedClipboard/VBoxSharedClipboardSvc-uri.cpp	(revision 80467)
+++ trunk/src/VBox/HostServices/SharedClipboard/VBoxSharedClipboardSvc-uri.cpp	(revision 80468)
@@ -89,7 +89,7 @@
     if (pMsgHdr)
     {
-        uint16_t uEventHdrRead = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
-
-        HGCMSvcSetU32(&pMsgHdr->m_paParms[0], VBOX_SHARED_CLIPBOARD_CONTEXTID_MAKE(pCtx->pTransfer->State.uID, uEventHdrRead));
+        VBOXCLIPBOARDEVENTID uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
+
+        HGCMSvcSetU32(&pMsgHdr->m_paParms[0], VBOX_SHARED_CLIPBOARD_CONTEXTID_MAKE(pCtx->pTransfer->State.uID, uEvent));
         HGCMSvcSetU32(&pMsgHdr->m_paParms[1], 0 /* fRoots */);
 
@@ -97,5 +97,5 @@
         if (RT_SUCCESS(rc))
         {
-            int rc2 = SharedClipboardEventRegister(&pCtx->pTransfer->Events, uEventHdrRead);
+            int rc2 = SharedClipboardEventRegister(&pCtx->pTransfer->Events, uEvent);
             AssertRC(rc2);
 
@@ -104,5 +104,5 @@
             {
                 PSHAREDCLIPBOARDEVENTPAYLOAD pPayloadHdr;
-                rc = SharedClipboardEventWait(&pCtx->pTransfer->Events, uEventHdrRead,
+                rc = SharedClipboardEventWait(&pCtx->pTransfer->Events, uEvent,
                                               pCtx->pTransfer->uTimeoutMs, &pPayloadHdr);
                 if (RT_SUCCESS(rc))
@@ -128,12 +128,12 @@
                                                                                                  VBOX_SHARED_CLIPBOARD_CPARMS_ROOT_LIST_ENTRY_READ_REQ);
 
-                                    uint16_t uEventEntryRead = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
+                                    uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
 
                                     HGCMSvcSetU32(&pMsgEntry->m_paParms[0],
-                                                  VBOX_SHARED_CLIPBOARD_CONTEXTID_MAKE(pCtx->pTransfer->State.uID, uEventEntryRead));
+                                                  VBOX_SHARED_CLIPBOARD_CONTEXTID_MAKE(pCtx->pTransfer->State.uID, uEvent));
                                     HGCMSvcSetU32(&pMsgEntry->m_paParms[1], 0 /* fRoots */);
                                     HGCMSvcSetU32(&pMsgEntry->m_paParms[2], i /* uIndex */);
 
-                                    rc2 = SharedClipboardEventRegister(&pCtx->pTransfer->Events, uEventEntryRead);
+                                    rc2 = SharedClipboardEventRegister(&pCtx->pTransfer->Events, uEvent);
                                     AssertRC(rc2);
 
@@ -143,5 +143,5 @@
 
                                     PSHAREDCLIPBOARDEVENTPAYLOAD pPayloadEntry;
-                                    rc = SharedClipboardEventWait(&pCtx->pTransfer->Events, uEventEntryRead,
+                                    rc = SharedClipboardEventWait(&pCtx->pTransfer->Events, uEvent,
                                                                   pCtx->pTransfer->uTimeoutMs, &pPayloadEntry);
                                     if (RT_FAILURE(rc))
@@ -155,5 +155,5 @@
                                     SharedClipboardPayloadFree(pPayloadEntry);
 
-                                    SharedClipboardEventUnregister(&pCtx->pTransfer->Events, uEventEntryRead);
+                                    SharedClipboardEventUnregister(&pCtx->pTransfer->Events, uEvent);
 
                                     if (RT_FAILURE(rc))
@@ -182,5 +182,5 @@
             }
 
-            SharedClipboardEventUnregister(&pCtx->pTransfer->Events, uEventHdrRead);
+            SharedClipboardEventUnregister(&pCtx->pTransfer->Events, uEvent);
         }
     }
@@ -206,5 +206,5 @@
     if (pMsg)
     {
-        const uint16_t uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
+        const VBOXCLIPBOARDEVENTID uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
 
         pMsg->m_Ctx.uContextID = VBOX_SHARED_CLIPBOARD_CONTEXTID_MAKE(pCtx->pTransfer->State.uID, uEvent);
@@ -263,5 +263,5 @@
     if (pMsg)
     {
-        const uint16_t uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
+        const VBOXCLIPBOARDEVENTID uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
 
         pMsg->m_Ctx.uContextID = VBOX_SHARED_CLIPBOARD_CONTEXTID_MAKE(pCtx->pTransfer->State.uID, uEvent);
@@ -310,5 +310,5 @@
     if (pMsg)
     {
-        const uint16_t uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
+        const VBOXCLIPBOARDEVENTID uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
 
         HGCMSvcSetU32(&pMsg->m_paParms[0], VBOX_SHARED_CLIPBOARD_CONTEXTID_MAKE(pCtx->pTransfer->State.uID, uEvent));
@@ -370,5 +370,5 @@
     if (pMsg)
     {
-        const uint16_t uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
+        const VBOXCLIPBOARDEVENTID uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
 
         HGCMSvcSetU32(&pMsg->m_paParms[0], VBOX_SHARED_CLIPBOARD_CONTEXTID_MAKE(pCtx->pTransfer->State.uID, uEvent));
@@ -429,5 +429,5 @@
     if (pMsg)
     {
-        const uint16_t uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
+        const VBOXCLIPBOARDEVENTID uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
 
         LogFlowFunc(("pszPath=%s, fCreate=0x%x\n", pCreateParms->pszPath, pCreateParms->fCreate));
@@ -488,5 +488,5 @@
     if (pMsg)
     {
-        const uint16_t uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
+        const VBOXCLIPBOARDEVENTID uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
 
         HGCMSvcSetU32(&pMsg->m_paParms[0], VBOX_SHARED_CLIPBOARD_CONTEXTID_MAKE(pCtx->pTransfer->State.uID, uEvent));
@@ -541,5 +541,5 @@
     if (pMsg)
     {
-        const uint16_t uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
+        const VBOXCLIPBOARDEVENTID uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
 
         HGCMSvcSetU32(&pMsg->m_paParms[0], VBOX_SHARED_CLIPBOARD_CONTEXTID_MAKE(pCtx->pTransfer->State.uID, uEvent));
@@ -599,5 +599,5 @@
     if (pMsg)
     {
-        const uint16_t uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
+        const VBOXCLIPBOARDEVENTID uEvent = SharedClipboardEventIDGenerate(&pCtx->pTransfer->Events);
 
         HGCMSvcSetU32(&pMsg->m_paParms[0], VBOX_SHARED_CLIPBOARD_CONTEXTID_MAKE(pCtx->pTransfer->State.uID, uEvent));
@@ -1181,5 +1181,5 @@
                         if (RT_SUCCESS(rc))
                         {
-                            const uint16_t uEvent = VBOX_SHARED_CLIPBOARD_CONTEXTID_GET_EVENT(uCID);
+                            const VBOXCLIPBOARDEVENTID uEvent = VBOX_SHARED_CLIPBOARD_CONTEXTID_GET_EVENT(uCID);
 
                             LogFlowFunc(("uCID=%RU32 -> uEvent=%RU32\n", uCID, uEvent));
@@ -1422,5 +1422,5 @@
                 if (RT_SUCCESS(rc))
                 {
-                    const uint16_t uEvent = VBOX_SHARED_CLIPBOARD_CONTEXTID_GET_EVENT(uCID);
+                    const VBOXCLIPBOARDEVENTID uEvent = VBOX_SHARED_CLIPBOARD_CONTEXTID_GET_EVENT(uCID);
 
                     PSHAREDCLIPBOARDEVENTPAYLOAD pPayload;
@@ -1471,5 +1471,5 @@
                 if (RT_SUCCESS(rc))
                 {
-                    const uint16_t uEvent = VBOX_SHARED_CLIPBOARD_CONTEXTID_GET_EVENT(uCID);
+                    const VBOXCLIPBOARDEVENTID uEvent = VBOX_SHARED_CLIPBOARD_CONTEXTID_GET_EVENT(uCID);
 
                     PSHAREDCLIPBOARDEVENTPAYLOAD pPayload;
@@ -1551,5 +1551,5 @@
                     if (RT_SUCCESS(rc))
                     {
-                        const uint16_t uEvent = VBOX_SHARED_CLIPBOARD_CONTEXTID_GET_EVENT(uCID);
+                        const VBOXCLIPBOARDEVENTID uEvent = VBOX_SHARED_CLIPBOARD_CONTEXTID_GET_EVENT(uCID);
 
                         PSHAREDCLIPBOARDEVENTPAYLOAD pPayload;
@@ -1599,5 +1599,5 @@
                     if (RT_SUCCESS(rc))
                     {
-                        const uint16_t uEvent = VBOX_SHARED_CLIPBOARD_CONTEXTID_GET_EVENT(uCID);
+                        const VBOXCLIPBOARDEVENTID uEvent = VBOX_SHARED_CLIPBOARD_CONTEXTID_GET_EVENT(uCID);
 
                         PSHAREDCLIPBOARDEVENTPAYLOAD pPayload;
@@ -1645,5 +1645,5 @@
                 if (RT_SUCCESS(rc))
                 {
-                    const uint16_t uEvent = VBOX_SHARED_CLIPBOARD_CONTEXTID_GET_EVENT(uCID);
+                    const VBOXCLIPBOARDEVENTID uEvent = VBOX_SHARED_CLIPBOARD_CONTEXTID_GET_EVENT(uCID);
 
                     PSHAREDCLIPBOARDEVENTPAYLOAD pPayload;
Index: trunk/src/VBox/HostServices/SharedClipboard/VBoxSharedClipboardSvc.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedClipboard/VBoxSharedClipboardSvc.cpp	(revision 80467)
+++ trunk/src/VBox/HostServices/SharedClipboard/VBoxSharedClipboardSvc.cpp	(revision 80468)
@@ -186,4 +186,5 @@
 #include <iprt/assert.h>
 #include <iprt/critsect.h>
+#include <iprt/rand.h>
 
 #include <VBox/err.h>
@@ -228,8 +229,34 @@
 ClipboardClientMap g_mapClients;
 
+/** Global map of all registered event sources. */
+ClipboardEventSourceMap g_mapEventSources;
+
 /** Global list of all clients which are queued up (deferred return) and ready
  *  to process new commands. The key is the (unique) client ID. */
 ClipboardClientQueue g_listClientsDeferred;
 
+
+/**
+ * Creates a (unique) event source ID.
+ *
+ * @returns VBox status code, or VERR_NOT_FOUND on error.
+ * @param   puID                Where to store the created event source ID on success.
+ */
+int vboxSvcClipboardEventSourceCreateID(PVBOXCLIPBOARDEVENTSOURCEID puID)
+{
+    AssertPtrReturn(puID, VERR_INVALID_POINTER);
+
+    for (uint32_t i = 0; i < 32; i++) /* Don't try too hard. */
+    {
+        VBOXCLIPBOARDEVENTSOURCEID uID = RTRandU32() % VBOX_SHARED_CLIPBOARD_MAX_EVENT_SOURCES;
+        if (g_mapEventSources.find(uID) == g_mapEventSources.end())
+        {
+            *puID = uID;
+            return VINF_SUCCESS;
+        }
+    }
+
+    return VERR_NOT_FOUND;
+}
 
 uint32_t vboxSvcClipboardGetMode(void)
@@ -738,5 +765,5 @@
     if (pMsg)
     {
-        uint16_t uEvent = SharedClipboardEventIDGenerate(&pClient->Events);
+        VBOXCLIPBOARDEVENTID uEvent = SharedClipboardEventIDGenerate(&pClient->Events);
 
         HGCMSvcSetU32(&pMsg->m_paParms[0], VBOX_SHARED_CLIPBOARD_CONTEXTID_MAKE(pClient->Events.uID, uEvent));
@@ -1035,6 +1062,4 @@
     RT_NOREF(fRequestor, fRestoring);
 
-    int rc = VINF_SUCCESS;
-
     PVBOXCLIPBOARDCLIENT pClient = (PVBOXCLIPBOARDCLIENT)pvClient;
     AssertPtr(pvClient);
@@ -1044,26 +1069,32 @@
 
     /* Create the client's own event source. */
-    SharedClipboardEventSourceCreate(&pClient->Events, (uint16_t)g_mapClients.size());
-
-    LogFlowFunc(("[Client %RU32] Using event source %RU32\n", u32ClientID, pClient->Events.uID));
-
-    /* Reset the client state. */
-    vboxSvcClipboardClientStateReset(&pClient->State);
-
-    /* (Re-)initialize the client state. */
-    vboxSvcClipboardClientStateInit(&pClient->State, u32ClientID);
-
-    rc = VBoxClipboardSvcImplConnect(pClient, VBoxSvcClipboardGetHeadless());
+    VBOXCLIPBOARDEVENTSOURCEID uEventSourceID;
+    int rc = vboxSvcClipboardEventSourceCreateID(&uEventSourceID);
+    if (RT_SUCCESS(rc))
+        rc = SharedClipboardEventSourceCreate(&pClient->Events, uEventSourceID);
+    if (RT_SUCCESS(rc))
+    {
+        LogFlowFunc(("[Client %RU32] Using event source %RU32\n", u32ClientID, pClient->Events.uID));
+
+        /* Reset the client state. */
+        vboxSvcClipboardClientStateReset(&pClient->State);
+
+        /* (Re-)initialize the client state. */
+        rc = vboxSvcClipboardClientStateInit(&pClient->State, u32ClientID);
+        if (RT_SUCCESS(rc))
+        {
+            rc = VBoxClipboardSvcImplConnect(pClient, VBoxSvcClipboardGetHeadless());
 #ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
-    if (RT_SUCCESS(rc))
-        rc = SharedClipboardURICtxInit(&pClient->URI);
+            if (RT_SUCCESS(rc))
+                rc = SharedClipboardURICtxInit(&pClient->URI);
 #endif
-
-    if (RT_SUCCESS(rc))
-    {
-        VBOXCLIPBOARDCLIENTMAPENTRY ClientEntry;
-        RT_ZERO(ClientEntry);
-
-        g_mapClients[u32ClientID] = ClientEntry; /** @todo Handle OOM / collisions? */
+            if (RT_SUCCESS(rc))
+            {
+                VBOXCLIPBOARDCLIENTMAPENTRY ClientEntry;
+                RT_ZERO(ClientEntry);
+
+                g_mapClients[u32ClientID] = ClientEntry; /** @todo Handle OOM / collisions? */
+            }
+        }
     }
 
