Index: /trunk/include/VBox/VBoxGuestLib.h
===================================================================
--- /trunk/include/VBox/VBoxGuestLib.h	(revision 31051)
+++ /trunk/include/VBox/VBoxGuestLib.h	(revision 31052)
@@ -527,6 +527,6 @@
 VBGLR3DECL(int)     VbglR3SharedFolderDisconnect(uint32_t u32ClientId);
 VBGLR3DECL(int)     VbglR3SharedFolderGetMappings(uint32_t u32ClientId, bool fAutoMountOnly,
-                                                  PVBGLR3SHAREDFOLDERMAPPING paMappings, uint32_t cbMappings,
-                                                  uint32_t *pcMappings);
+                                                  PVBGLR3SHAREDFOLDERMAPPING *ppaMappings, uint32_t *pcMappings);
+VBGLR3DECL(void)    VbglR3SharedFolderFreeMappings(PVBGLR3SHAREDFOLDERMAPPING paMappings);
 VBGLR3DECL(int)     VbglR3SharedFolderGetName(uint32_t  u32ClientId,uint32_t u32Root, char **ppszName);
 /** @}  */
Index: /trunk/include/VBox/shflsvc.h
===================================================================
--- /trunk/include/VBox/shflsvc.h	(revision 31051)
+++ /trunk/include/VBox/shflsvc.h	(revision 31052)
@@ -379,5 +379,5 @@
     SHFLROOT root;
 } SHFLMAPPING;
-
+/** Pointer to a SHFLMAPPING structure. */
 typedef SHFLMAPPING *PSHFLMAPPING;
 
Index: /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxSharedFolders.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxSharedFolders.cpp	(revision 31051)
+++ /trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxSharedFolders.cpp	(revision 31052)
@@ -25,4 +25,5 @@
 int VBoxSharedFoldersAutoMount(void)
 {
+#if 0
     uint32_t u32ClientId;
     int rc = VbglR3SharedFolderConnect(&u32ClientId);
@@ -97,4 +98,6 @@
     }
     return rc;
+#endif
+    return 0;
 }
 
Index: /trunk/src/VBox/Additions/common/VBoxControl/VBoxControl.cpp
===================================================================
--- /trunk/src/VBox/Additions/common/VBoxControl/VBoxControl.cpp	(revision 31051)
+++ /trunk/src/VBox/Additions/common/VBoxControl/VBoxControl.cpp	(revision 31052)
@@ -1274,10 +1274,13 @@
     bool usageOK = true;
     bool fOnlyShowAutoMount = false;
-    if (   argc == 1
-        && (   RTStrICmp(argv[0], "-automount") == 0
+    if (argc == 1)
+    {
+        if (   RTStrICmp(argv[0], "-automount") == 0
             || RTStrICmp(argv[0], "/automount") == 0)
-       )
-    {
-        fOnlyShowAutoMount = true;
+        {
+            fOnlyShowAutoMount = true;
+        }
+        else
+            usageOK = false;
     }
     else if (argc > 1)
@@ -1296,41 +1299,34 @@
     else
     {
-        uint32_t cMappings = 64; /* See shflsvc.h for define; should be used later. */
-        uint32_t cbMappings = cMappings * sizeof(VBGLR3SHAREDFOLDERMAPPING);
-        VBGLR3SHAREDFOLDERMAPPING *paMappings = (PVBGLR3SHAREDFOLDERMAPPING)RTMemAlloc(cbMappings);
-
-        if (paMappings)
-        {
-            rc = VbglR3SharedFolderGetMappings(u32ClientId, fOnlyShowAutoMount,
-                                               paMappings, cbMappings,
-                                               &cMappings);
-            if (RT_SUCCESS(rc))
+        PVBGLR3SHAREDFOLDERMAPPING paMappings;
+        uint32_t cMappings;
+        rc = VbglR3SharedFolderGetMappings(u32ClientId, fOnlyShowAutoMount,
+                                           &paMappings, &cMappings);
+        if (RT_SUCCESS(rc))
+        {
+            if (fOnlyShowAutoMount)
+                RTPrintf("Auto-mounted Shared Folder mappings (%u):\n\n", cMappings);
+            else
+                RTPrintf("Shared Folder mappings (%u):\n\n", cMappings);
+
+            for (uint32_t i = 0; i < cMappings; i++)
             {
-                /* Maximum mappings, see shflsvc.h */
-                if (cMappings > 64)
-                    cMappings = 64;
-                RTPrintf("Shared Folder Mappings (%u):\n\n", cMappings);
-                for (uint32_t i = 0; i < cMappings; i++)
+                char *pszName;
+                rc = VbglR3SharedFolderGetName(u32ClientId, paMappings[i].u32Root, &pszName);
+                if (RT_SUCCESS(rc))
                 {
-                    char *pszName;
-                    rc = VbglR3SharedFolderGetName(u32ClientId, paMappings[i].u32Root, &pszName);
-                    if (RT_SUCCESS(rc))
-                    {
-                        RTPrintf("%02u - %s\n", i + 1, pszName);
-                        RTStrFree(pszName);
-                    }
-                    else
-                        VBoxControlError("Error while getting the shared folder name for root node = %u, rc = %Rrc\n",
-                                         paMappings[i].u32Root, rc);
+                    RTPrintf("%02u - %s\n", i + 1, pszName);
+                    RTStrFree(pszName);
                 }
-                if (cMappings == 0)
-                    RTPrintf("No Shared Folders available.\n");
+                else
+                    VBoxControlError("Error while getting the shared folder name for root node = %u, rc = %Rrc\n",
+                                     paMappings[i].u32Root, rc);
             }
-            else
-                VBoxControlError("Error while getting the shared folder mappings, rc = %Rrc\n", rc);
-            RTMemFree(paMappings);
+            if (cMappings == 0)
+                RTPrintf("No Shared Folders available.\n");
+            VbglR3SharedFolderFreeMappings(paMappings);
         }
         else
-            rc = VERR_NO_MEMORY;
+            VBoxControlError("Error while getting the shared folder mappings, rc = %Rrc\n", rc);
         VbglR3SharedFolderDisconnect(u32ClientId);
     }
Index: /trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibSharedFolders.cpp
===================================================================
--- /trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibSharedFolders.cpp	(revision 31051)
+++ /trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibSharedFolders.cpp	(revision 31052)
@@ -90,29 +90,13 @@
  *
  * @returns VBox status code.
- * @param   u32ClientId     The client id returned by VbglR3InvsSvcConnect().
+ * @param   u32ClientId     The client id returned by VbglR3SharedFolderConnect().
  * @param   fAutoMountOnly  Flag whether only auto-mounted shared folders
  *                          should be reported.
- * @param   paMappings      Pointer to a preallocated array which will retrieve the mapping info.
- * @param   cbMappings      Size (in bytes) of the provided array.
- * @param   pcMappings      On input, the size of @a paMappings gives as an
- *                          item count.  On output, the number of mappings
- *                          returned in @a paMappings.
- *
- * @todo    r=bird: cbMappings and @a *pcMappings overlap.  The better API
- *          would be to change cbMappings to cMappings (the entries are fixed
- *          sized) and move the move the input aspect of @a *pcMappings to it.
- *
- *          However, it would be better if this function would do the array
- *          allocation.  This way you could deal with too-much-data conditions
- *          here (or hide the max-number-of-shared-folders-per-vm-define).
- *          Then paMappings would become ppaMappings and cbMappings could be
- *          removed altogether. *pcMappings would only be output.  A
- *          corresponding VbglR3SharedFolderFreeMappings would be required for
- *          a 100% clean API (this is an (/going to be) offical API for C/C++
- *          programs).
+ * @param   ppaMappings     Allocated array which will retrieve the mapping info.  Needs
+ *                          to be freed with VbglR3SharedFolderFreeMappings() later.
+ * @param   pcMappings      The number of mappings returned in @a ppaMappings.
  */
 VBGLR3DECL(int) VbglR3SharedFolderGetMappings(uint32_t u32ClientId, bool fAutoMountOnly,
-                                              PVBGLR3SHAREDFOLDERMAPPING paMappings, uint32_t cbMappings,
-                                              uint32_t *pcMappings)
+                                              PVBGLR3SHAREDFOLDERMAPPING *ppaMappings, uint32_t *pcMappings)
 {
     AssertPtr(pcMappings);
@@ -131,15 +115,59 @@
     VbglHGCMParmUInt32Set(&Msg.flags, u32Flags);
 
-    /* Init the rest of the message. */
-    VbglHGCMParmUInt32Set(&Msg.numberOfMappings, *pcMappings);
-    VbglHGCMParmPtrSet(&Msg.mappings, &paMappings[0], cbMappings);
-
-    int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
-    if (RT_SUCCESS(rc))
+    /*
+     * Prepare and get the actual mappings from the host service.
+     */
+    int rc = VINF_SUCCESS;
+    uint32_t cMappings = 8; /* Should be a good default value. */
+    uint32_t cbSize = cMappings * sizeof(VBGLR3SHAREDFOLDERMAPPING);
+    VBGLR3SHAREDFOLDERMAPPING *ppaMappingsTemp = (PVBGLR3SHAREDFOLDERMAPPING)RTMemAllocZ(cbSize);
+    if (ppaMappingsTemp == NULL)
+        rc = VERR_NO_MEMORY;
+
+    *pcMappings = 0;
+    do
     {
-        VbglHGCMParmUInt32Get(&Msg.numberOfMappings, pcMappings);
-        rc = Msg.callInfo.result;
-    }
-    return rc;
+        VbglHGCMParmUInt32Set(&Msg.numberOfMappings, cMappings);
+        VbglHGCMParmPtrSet(&Msg.mappings, ppaMappingsTemp, cbSize);
+
+        rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
+        if (RT_SUCCESS(rc))
+        {
+            rc = Msg.callInfo.result;
+            if (RT_SUCCESS(rc))
+            {
+                VbglHGCMParmUInt32Get(&Msg.numberOfMappings, pcMappings);
+
+                /* Do we have more mappings than we have allocated space for? */
+                if (rc == VINF_BUFFER_OVERFLOW)
+                {
+                    cMappings = *pcMappings;
+                    cbSize = cMappings * sizeof(VBGLR3SHAREDFOLDERMAPPING);
+                    void *pvNew = RTMemRealloc(ppaMappingsTemp, cbSize);
+                    AssertPtrBreakStmt(pvNew, rc = VERR_NO_MEMORY);
+                    ppaMappingsTemp = (PVBGLR3SHAREDFOLDERMAPPING)pvNew;
+                }
+                else
+                    *ppaMappings = ppaMappingsTemp;
+            }
+        }
+    } while (rc == VINF_BUFFER_OVERFLOW);
+
+    if (RT_FAILURE(rc) && ppaMappingsTemp)
+        RTMemFree(ppaMappingsTemp);
+
+    return rc;
+}
+
+
+/**
+ * Frees the shared folder mappings allocated by
+ * VbglR3SharedFolderGetMappings() before.
+ *
+ * @param   paMappings     What
+ */
+VBGLR3DECL(void) VbglR3SharedFolderFreeMappings(PVBGLR3SHAREDFOLDERMAPPING paMappings)
+{
+    RTMemFree(paMappings);
 }
 
@@ -179,8 +207,10 @@
         if (RT_SUCCESS(rc))
         {
-            *ppszName = NULL;
-            rc = RTUtf16ToUtf8(&pString->String.ucs2[0], ppszName);
+            rc = Msg.callInfo.result;
             if (RT_SUCCESS(rc))
-                rc = Msg.callInfo.result; /** @todo r=bird: Shouldn't you check this *before* doing the conversion? */
+            {
+                *ppszName = NULL;
+                rc = RTUtf16ToUtf8(&pString->String.ucs2[0], ppszName);
+            }
         }
         RTMemFree(pString);
Index: /trunk/src/VBox/HostServices/SharedFolders/mappings.cpp
===================================================================
--- /trunk/src/VBox/HostServices/SharedFolders/mappings.cpp	(revision 31051)
+++ /trunk/src/VBox/HostServices/SharedFolders/mappings.cpp	(revision 31052)
@@ -37,5 +37,5 @@
 }
 
-int vbsfMappingLoaded (const MAPPING *pLoadedMapping, SHFLROOT root)
+int vbsfMappingLoaded(const PMAPPING pLoadedMapping, SHFLROOT root)
 {
     /* Mapping loaded from the saved state with the index. Which means
@@ -173,6 +173,6 @@
  * We are always executed from one specific HGCM thread. So thread safe.
  */
-int vbsfMappingsAdd (PSHFLSTRING pFolderName, PSHFLSTRING pMapName,
-                     uint32_t    fWritable,   uint32_t    fAutoMount)
+int vbsfMappingsAdd(PSHFLSTRING pFolderName, PSHFLSTRING pMapName,
+                    uint32_t fWritable, uint32_t fAutoMount)
 {
     unsigned i;
@@ -255,5 +255,5 @@
 }
 
-int vbsfMappingsRemove (PSHFLSTRING pMapName)
+int vbsfMappingsRemove(PSHFLSTRING pMapName)
 {
     unsigned i;
@@ -294,5 +294,5 @@
 }
 
-PCRTUTF16 vbsfMappingsQueryHostRoot (SHFLROOT root, uint32_t *pcbRoot)
+PCRTUTF16 vbsfMappingsQueryHostRoot(SHFLROOT root, uint32_t *pcbRoot)
 {
     MAPPING *pFolderMapping = vbsfMappingGetByRoot(root);
@@ -307,5 +307,5 @@
 }
 
-bool vbsfIsGuestMappingCaseSensitive (SHFLROOT root)
+bool vbsfIsGuestMappingCaseSensitive(SHFLROOT root)
 {
     MAPPING *pFolderMapping = vbsfMappingGetByRoot(root);
@@ -319,5 +319,5 @@
 }
 
-bool vbsfIsHostMappingCaseSensitive (SHFLROOT root)
+bool vbsfIsHostMappingCaseSensitive(SHFLROOT root)
 {
     MAPPING *pFolderMapping = vbsfMappingGetByRoot(root);
@@ -331,14 +331,20 @@
 }
 
-int vbsfMappingsQuery (SHFLCLIENTDATA *pClient, SHFLMAPPING *pMappings, uint32_t *pcMappings)
+/**
+ * Note: If pMappings/*pcMappings is smaller than the actual amount of mappings
+ *       that *could* have been returned *pcMappings contains the required buffer size
+ *       so that the caller can retry the operation if wanted.
+ */
+int vbsfMappingsQuery(PSHFLCLIENTDATA pClient, PSHFLMAPPING pMappings, uint32_t *pcMappings)
 {
     int rc = VINF_SUCCESS;
-    uint32_t cMaxMappings = RT_MIN(*pcMappings, SHFL_MAX_MAPPINGS);
+
+    uint32_t cMappings = 0; /* Will contain actual valid mappings. */
+    uint32_t idx = 0;       /* Current index in mappings buffer. */
 
     LogFlow(("vbsfMappingsQuery: pClient = %p, pMappings = %p, pcMappings = %p, *pcMappings = %d\n",
              pClient, pMappings, pcMappings, *pcMappings));
 
-    *pcMappings = 0;
-    for (uint32_t i=0;i<cMaxMappings;i++)
+    for (uint32_t i = 0; i < SHFL_MAX_MAPPINGS; i++)
     {
         MAPPING *pFolderMapping = vbsfMappingGetByRoot(i);
@@ -346,20 +352,29 @@
             && pFolderMapping->fValid == true)
         {
-            /* Skip mappings which are not marked for auto-mounting if
-             * the SHFL_MF_AUTOMOUNT flag ist set. */
-            if (   (pClient->fu32Flags & SHFL_MF_AUTOMOUNT)
-                && !pFolderMapping->fAutoMount)
-                continue;
-
-            pMappings[*pcMappings].u32Status = SHFL_MS_NEW;
-            pMappings[*pcMappings].root = i;
-            *pcMappings = *pcMappings + 1;
-        }
-    }
+            if (idx < *pcMappings)
+            {
+                /* Skip mappings which are not marked for auto-mounting if
+                 * the SHFL_MF_AUTOMOUNT flag ist set. */
+                if (   (pClient->fu32Flags & SHFL_MF_AUTOMOUNT)
+                    && !pFolderMapping->fAutoMount)
+                    continue;
+
+                pMappings[idx].u32Status = SHFL_MS_NEW;
+                pMappings[idx].root = i;
+                idx++;
+            }
+            cMappings++;
+        }
+    }
+
+    /* Return actual number of mappings, regardless whether the handed in
+     * mapping buffer was big enough. */
+    *pcMappings = cMappings;
+
     LogFlow(("vbsfMappingsQuery: return rc = %Rrc\n", rc));
     return rc;
 }
 
-int vbsfMappingsQueryName (SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLSTRING *pString)
+int vbsfMappingsQueryName(PSHFLCLIENTDATA pClient, SHFLROOT root, SHFLSTRING *pString)
 {
     int rc = VINF_SUCCESS;
@@ -394,5 +409,5 @@
 }
 
-int vbsfMappingsQueryWritable (SHFLCLIENTDATA *pClient, SHFLROOT root, bool *fWritable)
+int vbsfMappingsQueryWritable(PSHFLCLIENTDATA pClient, SHFLROOT root, bool *fWritable)
 {
     int rc = VINF_SUCCESS;
@@ -417,5 +432,5 @@
 }
 
-int vbsfMappingsQueryAutoMount (SHFLCLIENTDATA *pClient, SHFLROOT root, bool *fAutoMount)
+int vbsfMappingsQueryAutoMount(PSHFLCLIENTDATA pClient, SHFLROOT root, bool *fAutoMount)
 {
     int rc = VINF_SUCCESS;
@@ -440,5 +455,6 @@
 }
 
-int vbsfMapFolder (SHFLCLIENTDATA *pClient, PSHFLSTRING pszMapName, RTUTF16 delimiter, bool fCaseSensitive, SHFLROOT *pRoot)
+int vbsfMapFolder(PSHFLCLIENTDATA pClient, PSHFLSTRING pszMapName,
+                  RTUTF16 delimiter, bool fCaseSensitive, SHFLROOT *pRoot)
 {
     MAPPING *pFolderMapping = NULL;
@@ -490,5 +506,5 @@
 }
 
-int vbsfUnmapFolder (SHFLCLIENTDATA *pClient, SHFLROOT root)
+int vbsfUnmapFolder(PSHFLCLIENTDATA pClient, SHFLROOT root)
 {
     int rc = VINF_SUCCESS;
Index: /trunk/src/VBox/HostServices/SharedFolders/mappings.h
===================================================================
--- /trunk/src/VBox/HostServices/SharedFolders/mappings.h	(revision 31051)
+++ /trunk/src/VBox/HostServices/SharedFolders/mappings.h	(revision 31052)
@@ -31,5 +31,7 @@
     bool        fWritable;
     bool        fAutoMount;
-} MAPPING, *PMAPPING;
+} MAPPING;
+/** Pointer to a MAPPING structure. */
+typedef MAPPING *PMAPPING;
 
 void vbsfMappingInit(void);
@@ -40,17 +42,17 @@
 int vbsfMappingsRemove(PSHFLSTRING pMapName);
 
-int vbsfMappingsQuery(SHFLCLIENTDATA *pClient, SHFLMAPPING *pMappings, uint32_t *pcMappings);
-int vbsfMappingsQueryName(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLSTRING *pString);
-int vbsfMappingsQueryWritable(SHFLCLIENTDATA *pClient, SHFLROOT root, bool *fWritable);
+int vbsfMappingsQuery(PSHFLCLIENTDATA pClient, PSHFLMAPPING pMappings, uint32_t *pcMappings);
+int vbsfMappingsQueryName(PSHFLCLIENTDATA pClient, SHFLROOT root, SHFLSTRING *pString);
+int vbsfMappingsQueryWritable(PSHFLCLIENTDATA pClient, SHFLROOT root, bool *fWritable);
 
-int vbsfMapFolder(SHFLCLIENTDATA *pClient, PSHFLSTRING pszMapName, RTUTF16 delimiter, bool fCaseSensitive, SHFLROOT *pRoot);
-int vbsfUnmapFolder(SHFLCLIENTDATA *pClient, SHFLROOT root);
+int vbsfMapFolder(PSHFLCLIENTDATA pClient, PSHFLSTRING pszMapName, RTUTF16 delimiter, bool fCaseSensitive, SHFLROOT *pRoot);
+int vbsfUnmapFolder(PSHFLCLIENTDATA pClient, SHFLROOT root);
 
-PCRTUTF16 vbsfMappingsQueryHostRoot (SHFLROOT root, uint32_t *pcbRoot);
-bool vbsfIsGuestMappingCaseSensitive (SHFLROOT root);
-bool vbsfIsHostMappingCaseSensitive (SHFLROOT root);
+PCRTUTF16 vbsfMappingsQueryHostRoot(SHFLROOT root, uint32_t *pcbRoot);
+bool vbsfIsGuestMappingCaseSensitive(SHFLROOT root);
+bool vbsfIsHostMappingCaseSensitive(SHFLROOT root);
 
-int vbsfMappingLoaded (const MAPPING *pLoadedMapping, SHFLROOT root);
-MAPPING *vbsfMappingGetByRoot(SHFLROOT root);
+int vbsfMappingLoaded(const PMAPPING pLoadedMapping, SHFLROOT root);
+PMAPPING vbsfMappingGetByRoot(SHFLROOT root);
 
 #endif /* !___MAPPINGS_H */
Index: /trunk/src/VBox/HostServices/SharedFolders/service.cpp
===================================================================
--- /trunk/src/VBox/HostServices/SharedFolders/service.cpp	(revision 31051)
+++ /trunk/src/VBox/HostServices/SharedFolders/service.cpp	(revision 31052)
@@ -4,5 +4,5 @@
 
 /*
- * Copyright (C) 2006-2007 Oracle Corporation
+ * Copyright (C) 2006-2010 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -327,4 +327,9 @@
                     if (RT_SUCCESS(rc))
                     {
+                        /* Report that there are more mappings to get if
+                         * handed in buffer is too small. */
+                        if (paParms[1].u.uint32 < cMappings)
+                            rc = VINF_BUFFER_OVERFLOW;
+
                         /* Update parameters. */
                         paParms[1].u.uint32 = cMappings;
Index: /trunk/src/VBox/HostServices/SharedFolders/shfl.h
===================================================================
--- /trunk/src/VBox/HostServices/SharedFolders/shfl.h	(revision 31051)
+++ /trunk/src/VBox/HostServices/SharedFolders/shfl.h	(revision 31052)
@@ -4,5 +4,5 @@
 
 /*
- * Copyright (C) 2006-2007 Oracle Corporation
+ * Copyright (C) 2006-2010 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -46,8 +46,9 @@
     /** Client flags */
     uint32_t fu32Flags;
-
+    /** Path delimiter. */
     RTUTF16  PathDelimiter;
 } SHFLCLIENTDATA;
-
+/** Pointer to a SHFLCLIENTDATA structure. */
+typedef SHFLCLIENTDATA *PSHFLCLIENTDATA;
 
 #endif /* !___SHFL_H */
