Index: /trunk/src/VBox/Main/HostImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/HostImpl.cpp	(revision 31357)
+++ /trunk/src/VBox/Main/HostImpl.cpp	(revision 31358)
@@ -1746,6 +1746,6 @@
             {
                 ComObjPtr<Medium> hostDVDDriveObj;
-                Bstr location(it->mDevice);
-                Bstr description(it->mDescription);
+                Utf8Str location(it->mDevice);
+                Utf8Str description(it->mDescription);
                 if (SUCCEEDED(rc))
                     rc = hostDVDDriveObj.createObject();
@@ -1819,6 +1819,6 @@
             {
                 ComObjPtr<Medium> hostFloppyDriveObj;
-                Bstr location(it->mDevice);
-                Bstr description(it->mDescription);
+                Utf8Str location(it->mDevice);
+                Utf8Str description(it->mDescription);
                 if (SUCCEEDED(rc))
                     rc = hostFloppyDriveObj.createObject();
Index: /trunk/src/VBox/Main/MachineImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/MachineImpl.cpp	(revision 31357)
+++ /trunk/src/VBox/Main/MachineImpl.cpp	(revision 31358)
@@ -3415,7 +3415,7 @@
         diff.createObject();
         rc = diff->init(mParent,
-                        medium->preferredDiffFormat().raw(),
-                        BstrFmt("%ls"RTPATH_SLASH_STR,
-                                 mUserData->mSnapshotFolderFull.raw()).raw(),
+                        medium->getPreferredDiffFormat(),
+                        Utf8StrFmt("%ls"RTPATH_SLASH_STR,
+                                   mUserData->mSnapshotFolderFull.raw()).raw(),
                         &fNeedsSaveSettings);
         if (FAILED(rc)) return rc;
@@ -8399,7 +8399,7 @@
             diff.createObject();
             rc = diff->init(mParent,
-                            pMedium->preferredDiffFormat().raw(),
-                            BstrFmt("%ls"RTPATH_SLASH_STR,
-                                    mUserData->mSnapshotFolderFull.raw()).raw(),
+                            pMedium->getPreferredDiffFormat(),
+                            Utf8StrFmt("%ls"RTPATH_SLASH_STR,
+                                       mUserData->mSnapshotFolderFull.raw()).raw(),
                             pfNeedsSaveSettings);
             if (FAILED(rc)) throw rc;
Index: /trunk/src/VBox/Main/MediumFormatImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/MediumFormatImpl.cpp	(revision 31357)
+++ /trunk/src/VBox/Main/MediumFormatImpl.cpp	(revision 31358)
@@ -60,9 +60,9 @@
 
     /* The ID of the backend */
-    unconst(m.id) = aVDInfo->pszBackend;
+    unconst(m.strId) = aVDInfo->pszBackend;
     /* The Name of the backend */
     /* Use id for now as long as VDBACKENDINFO hasn't any extra
      * name/description field. */
-    unconst(m.name) = aVDInfo->pszBackend;
+    unconst(m.strName) = aVDInfo->pszBackend;
     /* The capabilities of the backend */
     unconst(m.capabilities) = aVDInfo->uBackendCaps;
@@ -73,6 +73,6 @@
         while (*papsz != NULL)
         {
-            unconst (m.fileExtensions).push_back (*papsz);
-            ++ papsz;
+            unconst(m.llFileExtensions).push_back(*papsz);
+            ++papsz;
         }
     }
@@ -131,11 +131,11 @@
 
             /* Create one property structure */
-            const Property prop = { Utf8Str (pa->pszKey),
-                                    Utf8Str (""),
+            const Property prop = { Utf8Str(pa->pszKey),
+                                    Utf8Str(""),
                                     dt,
                                     flags,
                                     defaultValue };
-            unconst (m.properties).push_back (prop);
-            ++ pa;
+            unconst(m.llProperties).push_back(prop);
+            ++pa;
         }
     }
@@ -160,9 +160,9 @@
         return;
 
-    unconst (m.properties).clear();
-    unconst (m.fileExtensions).clear();
-    unconst (m.capabilities) = 0;
-    unconst (m.name).setNull();
-    unconst (m.id).setNull();
+    unconst(m.llProperties).clear();
+    unconst(m.llFileExtensions).clear();
+    unconst(m.capabilities) = 0;
+    unconst(m.strName).setNull();
+    unconst(m.strId).setNull();
 }
 
@@ -178,5 +178,5 @@
 
     /* this is const, no need to lock */
-    m.id.cloneTo (aId);
+    m.strId.cloneTo(aId);
 
     return S_OK;
@@ -191,5 +191,5 @@
 
     /* this is const, no need to lock */
-    m.name.cloneTo (aName);
+    m.strName.cloneTo(aName);
 
     return S_OK;
@@ -205,8 +205,8 @@
 
     /* this is const, no need to lock */
-    com::SafeArray<BSTR> fileExtentions(m.fileExtensions.size());
+    com::SafeArray<BSTR> fileExtentions(m.llFileExtensions.size());
     int i = 0;
-    for (BstrList::const_iterator it = m.fileExtensions.begin();
-         it != m.fileExtensions.end();
+    for (StrList::const_iterator it = m.llFileExtensions.begin();
+         it != m.llFileExtensions.end();
          ++it, ++i)
         (*it).cloneTo(&fileExtentions[i]);
@@ -236,9 +236,9 @@
 }
 
-STDMETHODIMP MediumFormat::DescribeProperties(ComSafeArrayOut (BSTR, aNames),
-                                                ComSafeArrayOut (BSTR, aDescriptions),
-                                                ComSafeArrayOut (DataType_T, aTypes),
-                                                ComSafeArrayOut (ULONG, aFlags),
-                                                ComSafeArrayOut (BSTR, aDefaults))
+STDMETHODIMP MediumFormat::DescribeProperties(ComSafeArrayOut(BSTR, aNames),
+                                              ComSafeArrayOut(BSTR, aDescriptions),
+                                              ComSafeArrayOut(DataType_T, aTypes),
+                                              ComSafeArrayOut(ULONG, aFlags),
+                                              ComSafeArrayOut(BSTR, aDefaults))
 {
     CheckComArgSafeArrayNotNull(aNames);
@@ -252,21 +252,22 @@
 
     /* this is const, no need to lock */
-    com::SafeArray<BSTR>        propertyNames(m.properties.size());
-    com::SafeArray<BSTR>        propertyDescriptions (m.properties.size());
-    com::SafeArray<DataType_T>  propertyTypes(m.properties.size());
-    com::SafeArray<ULONG>       propertyFlags(m.properties.size());
-    com::SafeArray<BSTR>        propertyDefaults(m.properties.size());
+    size_t c = m.llProperties.size();
+    com::SafeArray<BSTR>        propertyNames(c);
+    com::SafeArray<BSTR>        propertyDescriptions(c);
+    com::SafeArray<DataType_T>  propertyTypes(c);
+    com::SafeArray<ULONG>       propertyFlags(c);
+    com::SafeArray<BSTR>        propertyDefaults(c);
 
     int i = 0;
-    for (PropertyList::const_iterator it = m.properties.begin();
-         it != m.properties.end();
+    for (PropertyList::const_iterator it = m.llProperties.begin();
+         it != m.llProperties.end();
          ++it, ++i)
     {
         const Property &prop = (*it);
-        prop.name.cloneTo(&propertyNames[i]);
-        prop.description.cloneTo(&propertyDescriptions[i]);
+        prop.strName.cloneTo(&propertyNames[i]);
+        prop.strDescription.cloneTo(&propertyDescriptions[i]);
         propertyTypes[i] = prop.type;
         propertyFlags[i] = prop.flags;
-        prop.defaultValue.cloneTo(&propertyDefaults[i]);
+        prop.strDefaultValue.cloneTo(&propertyDefaults[i]);
     }
 
Index: /trunk/src/VBox/Main/MediumImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/MediumImpl.cpp	(revision 31357)
+++ /trunk/src/VBox/Main/MediumImpl.cpp	(revision 31358)
@@ -730,6 +730,8 @@
 
 /**
- * Initializes the hard disk object without creating or opening an associated
+ * Initializes an empty hard disk object without creating or opening an associated
  * storage unit.
+ *
+ * This gets called by VirtualBox::CreateHardDisk().
  *
  * For hard disks that don't have the VD_CAP_CREATE_FIXED or
@@ -744,10 +746,10 @@
  */
 HRESULT Medium::init(VirtualBox *aVirtualBox,
-                     CBSTR aFormat,
-                     CBSTR aLocation,
+                     const Utf8Str &aFormat,
+                     const Utf8Str &aLocation,
                      bool *pfNeedsSaveSettings)
 {
     AssertReturn(aVirtualBox != NULL, E_FAIL);
-    AssertReturn(aFormat != NULL && *aFormat != '\0', E_FAIL);
+    AssertReturn(!aFormat.isEmpty(), E_FAIL);
 
     /* Enclose the state transition NotReady->InInit->Ready */
@@ -771,5 +773,5 @@
     if (FAILED(rc)) return rc;
 
-    if (m->formatObj->capabilities() & MediumFormatCapabilities_File)
+    if (m->formatObj->getCapabilities() & MediumFormatCapabilities_File)
     {
         rc = setLocation(aLocation);
@@ -782,6 +784,6 @@
     }
 
-    if (!(m->formatObj->capabilities() & (   MediumFormatCapabilities_CreateFixed
-                                           | MediumFormatCapabilities_CreateDynamic))
+    if (!(m->formatObj->getCapabilities() & (   MediumFormatCapabilities_CreateFixed
+                                              | MediumFormatCapabilities_CreateDynamic))
        )
     {
@@ -807,4 +809,8 @@
  * location. The enOpenMode parameter defines whether the medium will be opened
  * read/write or read-only.
+ *
+ * This gets called by VirtualBox::OpenHardDisk(), OpenDVDImage and OpenFloppyImage();
+ * this also gets called by Machine::AttachDevice() and createImplicitDiffs()
+ * when new diff images are created.
  *
  * Note that the UUID, format and the parent of this medium will be
@@ -825,5 +831,5 @@
  */
 HRESULT Medium::init(VirtualBox *aVirtualBox,
-                     CBSTR aLocation,
+                     const Utf8Str &aLocation,
                      HDDOpenMode enOpenMode,
                      DeviceType_T aDeviceType,
@@ -834,5 +840,5 @@
 {
     AssertReturn(aVirtualBox, E_INVALIDARG);
-    AssertReturn(aLocation, E_INVALIDARG);
+    AssertReturn(!aLocation.isEmpty(), E_INVALIDARG);
 
     /* Enclose the state transition NotReady->InInit->Ready */
@@ -954,5 +960,5 @@
     {
         AssertReturn(!data.strFormat.isEmpty(), E_FAIL);
-        rc = setFormat(Bstr(data.strFormat));
+        rc = setFormat(data.strFormat);
         if (FAILED(rc)) return rc;
     }
@@ -961,7 +967,7 @@
         /// @todo handle host drive settings here as well?
         if (!data.strFormat.isEmpty())
-            rc = setFormat(Bstr(data.strFormat));
+            rc = setFormat(data.strFormat);
         else
-            rc = setFormat(Bstr("RAW"));
+            rc = setFormat("RAW");
         if (FAILED(rc)) return rc;
     }
@@ -1045,8 +1051,4 @@
  * Not used for anything but the host floppy/host DVD case.
  *
- * @todo optimize all callers to avoid reconstructing objects with the same
- * information over and over again - in the typical case each VM referring to
- * a particular host drive has its own instance.
- *
  * @param aVirtualBox   VirtualBox object.
  * @param aDeviceType   Device type of the medium.
@@ -1058,9 +1060,9 @@
 HRESULT Medium::init(VirtualBox *aVirtualBox,
                      DeviceType_T aDeviceType,
-                     CBSTR aLocation,
-                     CBSTR aDescription)
+                     const Utf8Str &aLocation,
+                     const Utf8Str &aDescription /* = Utf8Str::Empty */)
 {
     ComAssertRet(aDeviceType == DeviceType_DVD || aDeviceType == DeviceType_Floppy, E_INVALIDARG);
-    ComAssertRet(aLocation, E_INVALIDARG);
+    ComAssertRet(!aLocation.isEmpty(), E_INVALIDARG);
 
     /* Enclose the state transition NotReady->InInit->Ready */
@@ -1079,10 +1081,9 @@
         memcpy(&uuid.au8[0], "FD", 2);
     /* use device name, adjusted to the end of uuid, shortened if necessary */
-    Utf8Str loc(aLocation);
-    size_t cbLocation = strlen(loc.raw());
-    if (cbLocation > 12)
-        memcpy(&uuid.au8[4], loc.raw() + (cbLocation - 12), 12);
+    size_t lenLocation = aLocation.length();
+    if (lenLocation > 12)
+        memcpy(&uuid.au8[4], aLocation.raw() + (lenLocation - 12), 12);
     else
-        memcpy(&uuid.au8[4 + 12 - cbLocation], loc.raw(), cbLocation);
+        memcpy(&uuid.au8[4 + 12 - lenLocation], aLocation.raw(), lenLocation);
     unconst(m->id) = uuid;
 
@@ -1091,5 +1092,5 @@
     m->state = MediumState_Created;
     m->hostDrive = true;
-    HRESULT rc = setFormat(Bstr("RAW"));
+    HRESULT rc = setFormat("RAW");
     if (FAILED(rc)) return rc;
     rc = setLocation(aLocation);
@@ -2106,10 +2107,10 @@
         aVariant = (MediumVariant_T)((unsigned)aVariant & (unsigned)~MediumVariant_Diff);
         if (    !(aVariant & MediumVariant_Fixed)
-            &&  !(m->formatObj->capabilities() & MediumFormatCapabilities_CreateDynamic))
+            &&  !(m->formatObj->getCapabilities() & MediumFormatCapabilities_CreateDynamic))
             throw setError(VBOX_E_NOT_SUPPORTED,
                            tr("Medium format '%s' does not support dynamic storage creation"),
                            m->strFormat.raw());
         if (    (aVariant & MediumVariant_Fixed)
-            &&  !(m->formatObj->capabilities() & MediumFormatCapabilities_CreateDynamic))
+            &&  !(m->formatObj->getCapabilities() & MediumFormatCapabilities_CreateDynamic))
             throw setError(VBOX_E_NOT_SUPPORTED,
                            tr("Medium format '%s' does not support fixed storage creation"),
@@ -2554,5 +2555,5 @@
 ////////////////////////////////////////////////////////////////////////////////
 //
-// Medium internal methods
+// Medium public internal methods
 //
 ////////////////////////////////////////////////////////////////////////////////
@@ -2643,5 +2644,5 @@
  * @return
  */
-const ComObjPtr<MediumFormat> & Medium::getMediumFormat() const
+const ComObjPtr<MediumFormat>& Medium::getMediumFormat() const
 {
     return m->formatObj;
@@ -3099,5 +3100,5 @@
     /// @todo NEWMEDIA delegate the comparison to the backend?
 
-    if (m->formatObj->capabilities() & MediumFormatCapabilities_File)
+    if (m->formatObj->getCapabilities() & MediumFormatCapabilities_File)
     {
         Utf8Str location(aLocation);
@@ -3222,23 +3223,16 @@
  * Returns a preferred format for differencing media.
  */
-Bstr Medium::preferredDiffFormat()
-{
-    Utf8Str strFormat;
-
+Utf8Str Medium::getPreferredDiffFormat()
+{
     AutoCaller autoCaller(this);
-    AssertComRCReturn(autoCaller.rc(), strFormat);
+    AssertComRCReturn(autoCaller.rc(), Utf8Str::Empty);
+
+    /* check that our own format supports diffs */
+    if (!(m->formatObj->getCapabilities() & MediumFormatCapabilities_Differencing))
+        /* use the default format if not */
+        return m->pVirtualBox->getDefaultHardDiskFormat();
 
     /* m->strFormat is const, no need to lock */
-    strFormat = m->strFormat;
-
-    /* check that our own format supports diffs */
-    if (!(m->formatObj->capabilities() & MediumFormatCapabilities_Differencing))
-    {
-        /* use the default format if not */
-        AutoReadLock propsLock(m->pVirtualBox->getSystemProperties() COMMA_LOCKVAL_SRC_POS);
-        strFormat = m->pVirtualBox->getDefaultHardDiskFormat();
-    }
-
-    return strFormat;
+    return m->strFormat;
 }
 
@@ -3260,7 +3254,4 @@
     return m->type;
 }
-
-// private methods
-////////////////////////////////////////////////////////////////////////////////
 
 /**
@@ -3296,5 +3287,6 @@
  * @note Must be called from under this object's write lock.
  */
-HRESULT Medium::setLocation(const Utf8Str &aLocation, const Utf8Str &aFormat)
+HRESULT Medium::setLocation(const Utf8Str &aLocation,
+                            const Utf8Str &aFormat /* = Utf8Str::Empty */)
 {
     AssertReturn(!aLocation.isEmpty(), E_FAIL);
@@ -3318,5 +3310,5 @@
 
     if (   isImport
-        || (   (m->formatObj->capabilities() & MediumFormatCapabilities_File)
+        || (   (m->formatObj->getCapabilities() & MediumFormatCapabilities_File)
             && !m->hostDrive))
     {
@@ -3328,5 +3320,5 @@
         {
             /* must be a file (formatObj must be already known) */
-            Assert(m->formatObj->capabilities() & MediumFormatCapabilities_File);
+            Assert(m->formatObj->getCapabilities() & MediumFormatCapabilities_File);
 
             if (RTPathFilename(location.c_str()) == NULL)
@@ -3336,10 +3328,10 @@
                  * this */
 
-                ComAssertMsgRet(!m->formatObj->fileExtensions().empty(),
+                ComAssertMsgRet(!m->formatObj->getFileExtensions().empty(),
                                 ("Must be at least one extension if it is MediumFormatCapabilities_File\n"),
                                 E_FAIL);
 
-                Bstr ext = m->formatObj->fileExtensions().front();
-                ComAssertMsgRet(!ext.isEmpty(),
+                Utf8Str strExt = m->formatObj->getFileExtensions().front();
+                ComAssertMsgRet(!strExt.isEmpty(),
                                 ("Default extension must not be empty\n"),
                                 E_FAIL);
@@ -3347,6 +3339,6 @@
                 id.create();
 
-                location = Utf8StrFmt("%s{%RTuuid}.%ls",
-                                      location.raw(), id.raw(), ext.raw());
+                location = Utf8StrFmt("%s{%RTuuid}.%s",
+                                      location.raw(), id.raw(), strExt.c_str());
             }
         }
@@ -3402,5 +3394,5 @@
                 else
                 {
-                    HRESULT rc = setFormat(Bstr(aFormat));
+                    HRESULT rc = setFormat(aFormat);
                     /* setFormat() must not fail since we've just used the backend so
                      * the format object must be there */
@@ -3412,5 +3404,5 @@
                 ComAssertRet(backendName != NULL && *backendName != '\0', E_FAIL);
 
-                HRESULT rc = setFormat(Bstr(backendName));
+                HRESULT rc = setFormat(backendName);
                 RTStrFree(backendName);
 
@@ -3422,5 +3414,5 @@
 
         /* is it still a file? */
-        if (m->formatObj->capabilities() & MediumFormatCapabilities_File)
+        if (m->formatObj->getCapabilities() & MediumFormatCapabilities_File)
         {
             m->strLocation = location;
@@ -3572,5 +3564,5 @@
             }
 
-            if (formatObj->capabilities() & MediumFormatCapabilities_Uuid)
+            if (formatObj->getCapabilities() & MediumFormatCapabilities_Uuid)
             {
                 /* Modify the UUIDs if necessary. The associated fields are
@@ -3612,9 +3604,9 @@
                     {
                         lastAccessError = Utf8StrFmt(
-                            tr("UUID {%RTuuid} of the medium '%s' does not match the value {%RTuuid} stored in the media registry ('%s')"),
-                            &uuid,
-                            location.c_str(),
-                            mediumId.raw(),
-                            m->pVirtualBox->settingsFilePath().c_str());
+                                tr("UUID {%RTuuid} of the medium '%s' does not match the value {%RTuuid} stored in the media registry ('%s')"),
+                                &uuid,
+                                location.c_str(),
+                                mediumId.raw(),
+                                m->pVirtualBox->settingsFilePath().c_str());
                         throw S_OK;
                     }
@@ -3660,13 +3652,11 @@
                     Guid id = parentId;
                     ComObjPtr<Medium> pParent;
-                    rc = m->pVirtualBox->findHardDisk(&id, NULL,
-                                                      false /* aSetError */,
-                                                      &pParent);
+                    rc = m->pVirtualBox->findHardDisk(&id, NULL, false /* aSetError */, &pParent);
                     if (FAILED(rc))
                     {
                         lastAccessError = Utf8StrFmt(
-                            tr("Parent medium with UUID {%RTuuid} of the medium '%s' is not found in the media registry ('%s')"),
-                            &parentId, location.c_str(),
-                            m->pVirtualBox->settingsFilePath().c_str());
+                                tr("Parent medium with UUID {%RTuuid} of the medium '%s' is not found in the media registry ('%s')"),
+                                &parentId, location.c_str(),
+                                m->pVirtualBox->settingsFilePath().c_str());
                         throw S_OK;
                     }
@@ -3691,7 +3681,7 @@
                     {
                         lastAccessError = Utf8StrFmt(
-                            tr("Medium type of '%s' is differencing but it is not associated with any parent medium in the media registry ('%s')"),
-                            location.c_str(),
-                            m->pVirtualBox->settingsFilePath().c_str());
+                                tr("Medium type of '%s' is differencing but it is not associated with any parent medium in the media registry ('%s')"),
+                                location.c_str(),
+                                m->pVirtualBox->settingsFilePath().c_str());
                         throw S_OK;
                     }
@@ -3702,8 +3692,8 @@
                     {
                         lastAccessError = Utf8StrFmt(
-                            tr("Parent UUID {%RTuuid} of the medium '%s' does not match UUID {%RTuuid} of its parent medium stored in the media registry ('%s')"),
-                            &parentId, location.c_str(),
-                            m->pParent->getId().raw(),
-                            m->pVirtualBox->settingsFilePath().c_str());
+                                tr("Parent UUID {%RTuuid} of the medium '%s' does not match UUID {%RTuuid} of its parent medium stored in the media registry ('%s')"),
+                                &parentId, location.c_str(),
+                                m->pParent->getId().raw(),
+                                m->pVirtualBox->settingsFilePath().c_str());
                         throw S_OK;
                     }
@@ -3748,6 +3738,6 @@
         m->strLastAccessError = lastAccessError;
         LogWarningFunc(("'%s' is not accessible (error='%s', rc=%Rhrc, vrc=%Rrc)\n",
-                         location.c_str(), m->strLastAccessError.c_str(),
-                         rc, vrc));
+                        location.c_str(), m->strLastAccessError.c_str(),
+                        rc, vrc));
     }
 
@@ -3963,6 +3953,6 @@
         LogFlowThisFunc(("aWait=%RTbool locationFull=%s\n", aWait, getLocationFull().c_str() ));
 
-        if (    !(m->formatObj->capabilities() & (   MediumFormatCapabilities_CreateDynamic
-                                                   | MediumFormatCapabilities_CreateFixed)))
+        if (    !(m->formatObj->getCapabilities() & (   MediumFormatCapabilities_CreateDynamic
+                                                      | MediumFormatCapabilities_CreateFixed)))
             throw setError(VBOX_E_NOT_SUPPORTED,
                            tr("Medium format '%s' does not support storage deletion"),
@@ -4834,4 +4824,87 @@
 }
 
+////////////////////////////////////////////////////////////////////////////////
+//
+// Private methods
+//
+////////////////////////////////////////////////////////////////////////////////
+
+/**
+ * Performs extra checks if the medium can be closed and returns S_OK in
+ * this case. Otherwise, returns a respective error message. Called by
+ * Close() under the medium tree lock and the medium lock.
+ *
+ * @note Also reused by Medium::Reset().
+ *
+ * @note Caller must hold the media tree write lock!
+ */
+HRESULT Medium::canClose()
+{
+    Assert(m->pVirtualBox->getMediaTreeLockHandle().isWriteLockOnCurrentThread());
+
+    if (getChildren().size() != 0)
+        return setError(VBOX_E_OBJECT_IN_USE,
+                        tr("Cannot close medium '%s' because it has %d child media"),
+                        m->strLocationFull.raw(), getChildren().size());
+
+    return S_OK;
+}
+
+/**
+ * Unregisters this medium with mVirtualBox. Called by close() under the medium tree lock.
+ *
+ * This calls either VirtualBox::unregisterImage or VirtualBox::unregisterHardDisk depending
+ * on the device type of this medium.
+ *
+ * @param pfNeedsSaveSettings Optional pointer to a bool that must have been initialized to false and that will be set to true
+ *                by this function if the caller should invoke VirtualBox::saveSettings() because the global settings have changed.
+ *
+ * @note Caller must have locked the media tree lock for writing!
+ */
+HRESULT Medium::unregisterWithVirtualBox(bool *pfNeedsSaveSettings)
+{
+    /* Note that we need to de-associate ourselves from the parent to let
+     * unregisterHardDisk() properly save the registry */
+
+    /* we modify mParent and access children */
+    Assert(m->pVirtualBox->getMediaTreeLockHandle().isWriteLockOnCurrentThread());
+
+    Medium *pParentBackup = m->pParent;
+    AssertReturn(getChildren().size() == 0, E_FAIL);
+    if (m->pParent)
+        deparent();
+
+    HRESULT rc = E_FAIL;
+    switch (m->devType)
+    {
+        case DeviceType_DVD:
+            rc = m->pVirtualBox->unregisterImage(this, DeviceType_DVD, pfNeedsSaveSettings);
+        break;
+
+        case DeviceType_Floppy:
+            rc = m->pVirtualBox->unregisterImage(this, DeviceType_Floppy, pfNeedsSaveSettings);
+        break;
+
+        case DeviceType_HardDisk:
+            rc = m->pVirtualBox->unregisterHardDisk(this, pfNeedsSaveSettings);
+        break;
+
+        default:
+        break;
+    }
+
+    if (FAILED(rc))
+    {
+        if (pParentBackup)
+        {
+            // re-associate with the parent as we are still relatives in the registry
+            m->pParent = pParentBackup;
+            m->pParent->m->llChildren.push_back(this);
+        }
+    }
+
+    return rc;
+}
+
 /**
  * Checks that the format ID is valid and sets it on success.
@@ -4843,5 +4916,5 @@
  * @note Must be called from under this object's write lock.
  */
-HRESULT Medium::setFormat(CBSTR aFormat)
+HRESULT Medium::setFormat(const Utf8Str &aFormat)
 {
     /* get the format object first */
@@ -4853,6 +4926,6 @@
         if (m->formatObj.isNull())
             return setError(E_INVALIDARG,
-                            tr("Invalid medium storage format '%ls'"),
-                            aFormat);
+                            tr("Invalid medium storage format '%s'"),
+                            aFormat.c_str());
 
         /* reference the format permanently to prevent its unexpected
@@ -4867,10 +4940,9 @@
         Assert(m->properties.empty());
 
-        for (MediumFormat::PropertyList::const_iterator it =
-                m->formatObj->properties().begin();
-             it != m->formatObj->properties().end();
+        for (MediumFormat::PropertyList::const_iterator it = m->formatObj->getProperties().begin();
+             it != m->formatObj->getProperties().end();
              ++it)
         {
-            m->properties.insert(std::make_pair(it->name, Bstr::Empty));
+            m->properties.insert(std::make_pair(it->strName, Utf8Str::Empty));
         }
     }
@@ -4879,81 +4951,4 @@
 
     return S_OK;
-}
-
-/**
- * Performs extra checks if the medium can be closed and returns S_OK in
- * this case. Otherwise, returns a respective error message. Called by
- * Close() under the medium tree lock and the medium lock.
- *
- * @note Also reused by Medium::Reset().
- *
- * @note Caller must hold the media tree write lock!
- */
-HRESULT Medium::canClose()
-{
-    Assert(m->pVirtualBox->getMediaTreeLockHandle().isWriteLockOnCurrentThread());
-
-    if (getChildren().size() != 0)
-        return setError(VBOX_E_OBJECT_IN_USE,
-                        tr("Cannot close medium '%s' because it has %d child media"),
-                        m->strLocationFull.raw(), getChildren().size());
-
-    return S_OK;
-}
-
-/**
- * Unregisters this medium with mVirtualBox. Called by close() under the medium tree lock.
- *
- * This calls either VirtualBox::unregisterImage or VirtualBox::unregisterHardDisk depending
- * on the device type of this medium.
- *
- * @param pfNeedsSaveSettings Optional pointer to a bool that must have been initialized to false and that will be set to true
- *                by this function if the caller should invoke VirtualBox::saveSettings() because the global settings have changed.
- *
- * @note Caller must have locked the media tree lock for writing!
- */
-HRESULT Medium::unregisterWithVirtualBox(bool *pfNeedsSaveSettings)
-{
-    /* Note that we need to de-associate ourselves from the parent to let
-     * unregisterHardDisk() properly save the registry */
-
-    /* we modify mParent and access children */
-    Assert(m->pVirtualBox->getMediaTreeLockHandle().isWriteLockOnCurrentThread());
-
-    Medium *pParentBackup = m->pParent;
-    AssertReturn(getChildren().size() == 0, E_FAIL);
-    if (m->pParent)
-        deparent();
-
-    HRESULT rc = E_FAIL;
-    switch (m->devType)
-    {
-        case DeviceType_DVD:
-            rc = m->pVirtualBox->unregisterImage(this, DeviceType_DVD, pfNeedsSaveSettings);
-        break;
-
-        case DeviceType_Floppy:
-            rc = m->pVirtualBox->unregisterImage(this, DeviceType_Floppy, pfNeedsSaveSettings);
-        break;
-
-        case DeviceType_HardDisk:
-            rc = m->pVirtualBox->unregisterHardDisk(this, pfNeedsSaveSettings);
-        break;
-
-        default:
-        break;
-    }
-
-    if (FAILED(rc))
-    {
-        if (pParentBackup)
-        {
-            // re-associate with the parent as we are still relatives in the registry
-            m->pParent = pParentBackup;
-            m->pParent->m->llChildren.push_back(this);
-        }
-    }
-
-    return rc;
 }
 
@@ -5370,5 +5365,5 @@
         Utf8Str format(m->strFormat);
         Utf8Str location(m->strLocationFull);
-        uint64_t capabilities = m->formatObj->capabilities();
+        uint64_t capabilities = m->formatObj->getCapabilities();
         ComAssertThrow(capabilities & (  VD_CAP_CREATE_FIXED
                                        | VD_CAP_CREATE_DYNAMIC), E_FAIL);
@@ -5506,5 +5501,5 @@
         Utf8Str targetFormat(pTarget->m->strFormat);
         Utf8Str targetLocation(pTarget->m->strLocationFull);
-        uint64_t capabilities = m->formatObj->capabilities();
+        uint64_t capabilities = m->formatObj->getCapabilities();
         ComAssertThrow(capabilities & VD_CAP_CREATE_DYNAMIC, E_FAIL);
 
Index: /trunk/src/VBox/Main/SnapshotImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/SnapshotImpl.cpp	(revision 31357)
+++ /trunk/src/VBox/Main/SnapshotImpl.cpp	(revision 31358)
@@ -2624,5 +2624,5 @@
                     // Diff medium not backed by a file - cannot get status so
                     // be pessimistic.
-                    if (!(sourceFormat->capabilities() & MediumFormatCapabilities_File))
+                    if (!(sourceFormat->getCapabilities() & MediumFormatCapabilities_File))
                         throw rc;
                     const Utf8Str &loc = it->mpSource->getLocationFull();
Index: /trunk/src/VBox/Main/SystemPropertiesImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/SystemPropertiesImpl.cpp	(revision 31357)
+++ /trunk/src/VBox/Main/SystemPropertiesImpl.cpp	(revision 31358)
@@ -828,5 +828,5 @@
  * @return ComObjPtr<MediumFormat>
  */
-ComObjPtr<MediumFormat> SystemProperties::mediumFormat (CBSTR aFormat)
+ComObjPtr<MediumFormat> SystemProperties::mediumFormat(const Utf8Str &aFormat)
 {
     ComObjPtr<MediumFormat> format;
@@ -843,5 +843,5 @@
         /* MediumFormat is all const, no need to lock */
 
-        if ((*it)->id().compare(aFormat, Bstr::CaseInsensitive) == 0)
+        if ((*it)->getId().compare(aFormat, Utf8Str::CaseInsensitive) == 0)
         {
             format = *it;
Index: /trunk/src/VBox/Main/VirtualBoxImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/VirtualBoxImpl.cpp	(revision 31357)
+++ /trunk/src/VBox/Main/VirtualBoxImpl.cpp	(revision 31358)
@@ -1376,5 +1376,5 @@
     /* we don't access non-const data members so no need to lock */
 
-    Bstr format = aFormat;
+    Utf8Str format(aFormat);
     if (format.isEmpty())
         format = getDefaultHardDiskFormat();
@@ -1386,5 +1386,8 @@
     ComObjPtr<Medium> hardDisk;
     hardDisk.createObject();
-    rc = hardDisk->init(this, format.raw(), aLocation, &fNeedsSaveSettings);
+    rc = hardDisk->init(this,
+                        format,
+                        aLocation,
+                        &fNeedsSaveSettings);
 
     if (SUCCEEDED(rc))
@@ -1502,5 +1505,6 @@
 
 /** @note Doesn't lock anything. */
-STDMETHODIMP VirtualBox::OpenDVDImage(IN_BSTR aLocation, IN_BSTR aId,
+STDMETHODIMP VirtualBox::OpenDVDImage(IN_BSTR aLocation,
+                                      IN_BSTR aId,
                                       IMedium **aDVDImage)
 {
@@ -1520,5 +1524,12 @@
     ComObjPtr<Medium> image;
     image.createObject();
-    rc = image->init(this, aLocation, Medium::OpenReadOnly, DeviceType_DVD, true, id, false, Guid());
+    rc = image->init(this,
+                     aLocation,
+                     Medium::OpenReadOnly,
+                     DeviceType_DVD,
+                     true /* aSetImageId */,
+                     id,
+                     false /* aSetParentId */,
+                     Guid());
     if (SUCCEEDED(rc))
     {
@@ -1578,5 +1589,6 @@
 
 /** @note Doesn't lock anything. */
-STDMETHODIMP VirtualBox::OpenFloppyImage(IN_BSTR aLocation, IN_BSTR aId,
+STDMETHODIMP VirtualBox::OpenFloppyImage(IN_BSTR aLocation,
+                                         IN_BSTR aId,
                                          IMedium **aFloppyImage)
 {
@@ -1596,5 +1608,12 @@
     ComObjPtr<Medium> image;
     image.createObject();
-    rc = image->init(this, aLocation, Medium::OpenReadWrite, DeviceType_Floppy, true, id, false, Guid());
+    rc = image->init(this,
+                     aLocation,
+                     Medium::OpenReadWrite,
+                     DeviceType_Floppy,
+                     true /* aSetImageId */,
+                     id,
+                     false /* aSetParentId */,
+                     Guid());
     if (SUCCEEDED(rc))
     {
@@ -2956,5 +2975,5 @@
  * @return
  */
-const Utf8Str& VirtualBox::getDefaultMachineFolder() const
+Utf8Str VirtualBox::getDefaultMachineFolder() const
 {
     AutoReadLock propsLock(m->pSystemProperties COMMA_LOCKVAL_SRC_POS);
@@ -2967,5 +2986,5 @@
  * @return
  */
-const Utf8Str& VirtualBox::getDefaultHardDiskFolder() const
+Utf8Str VirtualBox::getDefaultHardDiskFolder() const
 {
     AutoReadLock propsLock(m->pSystemProperties COMMA_LOCKVAL_SRC_POS);
@@ -2978,5 +2997,5 @@
  * @return
  */
-const Utf8Str& VirtualBox::getDefaultHardDiskFormat() const
+Utf8Str VirtualBox::getDefaultHardDiskFormat() const
 {
     AutoReadLock propsLock(m->pSystemProperties COMMA_LOCKVAL_SRC_POS);
Index: /trunk/src/VBox/Main/include/MediumFormatImpl.h
===================================================================
--- /trunk/src/VBox/Main/include/MediumFormatImpl.h	(revision 31357)
+++ /trunk/src/VBox/Main/include/MediumFormatImpl.h	(revision 31358)
@@ -46,13 +46,13 @@
     struct Property
     {
-        Bstr name;
-        Bstr description;
-        DataType_T type;
-        ULONG flags;
-        Bstr defaultValue;
+        Utf8Str     strName;
+        Utf8Str     strDescription;
+        DataType_T  type;
+        ULONG       flags;
+        Utf8Str     strDefaultValue;
     };
 
-    typedef std::list <Bstr> BstrList;
-    typedef std::list <Property> PropertyList;
+    typedef std::list<Utf8Str>  StrList;
+    typedef std::list<Property> PropertyList;
 
     struct Data
@@ -60,9 +60,9 @@
         Data() : capabilities (0) {}
 
-        const Bstr id;
-        const Bstr name;
-        const BstrList fileExtensions;
-        const uint64_t capabilities;
-        const PropertyList properties;
+        const Utf8Str       strId;
+        const Utf8Str       strName;
+        const StrList       llFileExtensions;
+        const uint64_t      capabilities;
+        const PropertyList  llProperties;
     };
 
@@ -107,11 +107,11 @@
 
     /** Const, no need to lock */
-    const Bstr &id() const { return m.id; }
+    const Utf8Str& getId() const { return m.strId; }
     /** Const, no need to lock */
-    const BstrList &fileExtensions() const { return m.fileExtensions; }
+    const StrList& getFileExtensions() const { return m.llFileExtensions; }
     /** Const, no need to lock */
-    uint64_t capabilities() const { return m.capabilities; }
+    uint64_t getCapabilities() const { return m.capabilities; }
     /** Const, no need to lock */
-    const PropertyList &properties() const { return m.properties; }
+    const PropertyList& getProperties() const { return m.llProperties; }
 
 private:
Index: /trunk/src/VBox/Main/include/MediumImpl.h
===================================================================
--- /trunk/src/VBox/Main/include/MediumImpl.h	(revision 31357)
+++ /trunk/src/VBox/Main/include/MediumImpl.h	(revision 31358)
@@ -67,10 +67,15 @@
 
     // public initializer/uninitializer for internal purposes only
+
+    // initializer to create empty medium (VirtualBox::CreateHardDisk())
     HRESULT init(VirtualBox *aVirtualBox,
-                 CBSTR aFormat,
-                 CBSTR aLocation,
+                 const Utf8Str &aFormat,
+                 const Utf8Str &aLocation,
                  bool *pfNeedsSaveSettings);
+
+    // initializer for opening existing media
+    // (VirtualBox::OpenHardDisk/DVD(); Machine::AttachDevice())
     HRESULT init(VirtualBox *aVirtualBox,
-                 CBSTR aLocation,
+                 const Utf8Str &aLocation,
                  HDDOpenMode enOpenMode,
                  DeviceType_T aDeviceType,
@@ -79,4 +84,5 @@
                  BOOL aSetParentId,
                  const Guid &aParentId);
+
     // initializer used when loading settings
     HRESULT init(VirtualBox *aVirtualBox,
@@ -84,9 +90,11 @@
                  DeviceType_T aDeviceType,
                  const settings::Medium &data);
+
     // initializer for host floppy/DVD
     HRESULT init(VirtualBox *aVirtualBox,
                  DeviceType_T aDeviceType,
-                 CBSTR aLocation,
-                 CBSTR aDescription = NULL);
+                 const Utf8Str &aLocation,
+                 const Utf8Str &aDescription = Utf8Str::Empty);
+
     void uninit();
 
@@ -150,10 +158,9 @@
     STDMETHOD(Reset)(IProgress **aProgress);
 
-    // public methods for internal purposes only
+    // unsafe methods for internal purposes only (ensure there is
+    // a caller and a read lock before calling them!)
     const ComObjPtr<Medium>& getParent() const;
     const MediaList& getChildren() const;
 
-    // unsafe methods for internal purposes only (ensure there is
-    // a caller and a read lock before calling them!)
     const Guid& getId() const;
     MediumState_T getState() const;
@@ -233,5 +240,5 @@
 
     /** Returns a preferred format for a differencing hard disk. */
-    Bstr preferredDiffFormat();
+    Utf8Str getPreferredDiffFormat();
 
 private:
@@ -244,6 +251,6 @@
     HRESULT setStateError();
 
-    HRESULT setLocation(const Utf8Str &aLocation, const Utf8Str &aFormat = Utf8Str());
-    HRESULT setFormat(CBSTR aFormat);
+    HRESULT setLocation(const Utf8Str &aLocation, const Utf8Str &aFormat = Utf8Str::Empty);
+    HRESULT setFormat(const Utf8Str &aFormat);
 
     Utf8Str vdError(int aVRC);
Index: /trunk/src/VBox/Main/include/SystemPropertiesImpl.h
===================================================================
--- /trunk/src/VBox/Main/include/SystemPropertiesImpl.h	(revision 31357)
+++ /trunk/src/VBox/Main/include/SystemPropertiesImpl.h	(revision 31358)
@@ -107,5 +107,5 @@
     HRESULT saveSettings(settings::SystemProperties &data);
 
-    ComObjPtr<MediumFormat> mediumFormat(CBSTR aFormat);
+    ComObjPtr<MediumFormat> mediumFormat(const Utf8Str &aFormat);
 
     // public methods for internal purposes only
Index: /trunk/src/VBox/Main/include/VirtualBoxImpl.h
===================================================================
--- /trunk/src/VBox/Main/include/VirtualBoxImpl.h	(revision 31357)
+++ /trunk/src/VBox/Main/include/VirtualBoxImpl.h	(revision 31358)
@@ -242,7 +242,7 @@
 #endif /* VBOX_WITH_RESOURCE_USAGE_API */
 
-    const Utf8Str& getDefaultMachineFolder() const;
-    const Utf8Str& getDefaultHardDiskFolder() const;
-    const Utf8Str& getDefaultHardDiskFormat() const;
+    Utf8Str getDefaultMachineFolder() const;
+    Utf8Str getDefaultHardDiskFolder() const;
+    Utf8Str getDefaultHardDiskFormat() const;
 
     /** Returns the VirtualBox home directory */
