Index: /trunk/src/VBox/Frontends/VBoxManage/VBoxManageMisc.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VBoxManage/VBoxManageMisc.cpp	(revision 30928)
+++ /trunk/src/VBox/Frontends/VBoxManage/VBoxManageMisc.cpp	(revision 30929)
@@ -162,5 +162,9 @@
         machine->COMGETTER(Id)(uuid.asOutParam());
         machine = NULL;
-        CHECK_ERROR(a->virtualBox, UnregisterMachine(uuid, machine.asOutParam()));
+        SafeArray<BSTR> abstrFiles;
+        CHECK_ERROR(a->virtualBox, UnregisterMachine(uuid,
+                                                     false /* fDetachMedia */,
+                                                     ComSafeArrayAsOutParam(abstrFiles),
+                                                     machine.asOutParam()));
         if (SUCCEEDED(rc) && machine && fDelete)
             CHECK_ERROR(machine, DeleteSettings());
Index: /trunk/src/VBox/Frontends/VirtualBox/src/selector/VBoxSelectorWnd.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/selector/VBoxSelectorWnd.cpp	(revision 30928)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/selector/VBoxSelectorWnd.cpp	(revision 30929)
@@ -679,6 +679,7 @@
         if (ok)
         {
-            CMachine machine = item->machine();
-            vbox.UnregisterMachine (id);
+            QVector<QString> files;
+            CMachine machine;
+            vbox.UnregisterMachine(id, false /*fDetachMedia*/, files, machine);
             if (vbox.isOk() && item->accessible())
             {
Index: /trunk/src/VBox/Frontends/VirtualBox/src/wizards/newvm/UINewVMWzd.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/wizards/newvm/UINewVMWzd.cpp	(revision 30928)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/wizards/newvm/UINewVMWzd.cpp	(revision 30929)
@@ -782,5 +782,7 @@
         {
             /* Unregister on failure */
-            vbox.UnregisterMachine(machineId);
+            QVector<QString> files;
+            CMachine machine;
+            vbox.UnregisterMachine(machineId, false /*fDetachMedia*/, files, machine);
             if (vbox.isOk())
                 m_Machine.DeleteSettings();
Index: /trunk/src/VBox/Main/ApplianceImplImport.cpp
===================================================================
--- /trunk/src/VBox/Main/ApplianceImplImport.cpp	(revision 30928)
+++ /trunk/src/VBox/Main/ApplianceImplImport.cpp	(revision 30929)
@@ -1228,6 +1228,5 @@
         HRESULT rc2;
         // detach all hard disks from all machines we created
-        list<MyHardDiskAttachment>::iterator itM;
-        for (itM = stack.llHardDiskAttachments.begin();
+        for (list<MyHardDiskAttachment>::iterator itM = stack.llHardDiskAttachments.begin();
              itM != stack.llHardDiskAttachments.end();
              ++itM)
@@ -1250,6 +1249,5 @@
 
         // now clean up all hard disks we created
-        list< ComPtr<IMedium> >::iterator itHD;
-        for (itHD = stack.llHardDisksCreated.begin();
+        for (list< ComPtr<IMedium> >::iterator itHD = stack.llHardDisksCreated.begin();
              itHD != stack.llHardDisksCreated.end();
              ++itHD)
@@ -1269,5 +1267,6 @@
             Bstr bstrGuid = guid.toUtf16();
             ComPtr<IMachine> failedMachine;
-            rc2 = mVirtualBox->UnregisterMachine(bstrGuid, failedMachine.asOutParam());
+            SafeArray<BSTR> abstrPaths;
+            rc2 = mVirtualBox->UnregisterMachine(bstrGuid, false, ComSafeArrayAsOutParam(abstrPaths), failedMachine.asOutParam());
             if (SUCCEEDED(rc2))
                 rc2 = failedMachine->DeleteSettings();
Index: /trunk/src/VBox/Main/MachineImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/MachineImpl.cpp	(revision 30928)
+++ /trunk/src/VBox/Main/MachineImpl.cpp	(revision 30929)
@@ -5919,18 +5919,11 @@
 
 /**
- *  Checks that the registered flag of the machine can be set according to
- *  the argument and sets it. On success, commits and saves all settings.
- *
- *  @note When this machine is inaccessible, the only valid value for \a
- *  aRegistered is FALSE (i.e. unregister the machine) because unregistered
- *  inaccessible machines are not currently supported. Note that unregistering
- *  an inaccessible machine will \b uninitialize this machine object. Therefore,
- *  the caller must make sure there are no active Machine::addCaller() calls
- *  on the current thread because this will block Machine::uninit().
+ *  Checks whether the machine can be registered. If so, commits and saves
+ *  all settings.
  *
  *  @note Must be called from mParent's write lock. Locks this object and
  *  children for writing.
  */
-HRESULT Machine::trySetRegistered(BOOL argNewRegistered)
+HRESULT Machine::prepareRegister()
 {
     AssertReturn(mParent->isWriteLockOnCurrentThread(), E_FAIL);
@@ -5944,67 +5937,17 @@
     ensureNoStateDependencies();
 
-    ComAssertRet(mData->mRegistered != argNewRegistered, E_FAIL);
-
     if (!mData->mAccessible)
-    {
-        /* A special case: the machine is not accessible. */
-
-        /* inaccessible machines can only be unregistered */
-        AssertReturn(!argNewRegistered, E_FAIL);
-
-        /* Uninitialize ourselves here because currently there may be no
-         * unregistered that are inaccessible (this state combination is not
-         * supported). Note releasing the caller and leaving the lock before
-         * calling uninit() */
-
-        alock.leave();
-        autoCaller.release();
-
-        uninit();
-
-        return S_OK;
-    }
+        return setError(VBOX_E_INVALID_OBJECT_STATE,
+                        tr("The machine '%ls' with UUID {%s} is inaccessible and cannot be registered"),
+                        mUserData->mName.raw(),
+                        mData->mUuid.toString().raw());
 
     AssertReturn(autoCaller.state() == Ready, E_FAIL);
 
-    if (argNewRegistered)
-    {
-        if (mData->mRegistered)
-            return setError(VBOX_E_INVALID_OBJECT_STATE,
-                            tr("The machine '%ls' with UUID {%s} is already registered"),
-                            mUserData->mName.raw(),
-                            mData->mUuid.toString().raw());
-    }
-    else
-    {
-        if (mData->mMachineState == MachineState_Saved)
-            return setError(VBOX_E_INVALID_VM_STATE,
-                            tr("Cannot unregister the machine '%ls' because it is in the Saved state"),
-                            mUserData->mName.raw());
-
-        size_t snapshotCount = 0;
-        if (mData->mFirstSnapshot)
-            snapshotCount = mData->mFirstSnapshot->getAllChildrenCount() + 1;
-        if (snapshotCount)
-            return setError(VBOX_E_INVALID_OBJECT_STATE,
-                            tr("Cannot unregister the machine '%ls' because it has %d snapshots"),
-                            mUserData->mName.raw(), snapshotCount);
-
-        if (mData->mSession.mState != SessionState_Closed)
-            return setError(VBOX_E_INVALID_OBJECT_STATE,
-                            tr("Cannot unregister the machine '%ls' because it has an open session"),
-                            mUserData->mName.raw());
-
-        if (mMediaData->mAttachments.size() != 0)
-            return setError(VBOX_E_INVALID_OBJECT_STATE,
-                            tr("Cannot unregister the machine '%ls' because it has %d medium attachments"),
-                            mUserData->mName.raw(),
-                            mMediaData->mAttachments.size());
-
-        /* Note that we do not prevent unregistration of a DVD or Floppy image
-         * is attached: as opposed to hard disks detaching such an image
-         * implicitly in this method (which we will do below) won't have any
-         * side effects (like detached orphan base and diff hard disks etc).*/
-    }
+    if (mData->mRegistered)
+        return setError(VBOX_E_INVALID_OBJECT_STATE,
+                        tr("The machine '%ls' with UUID {%s} is already registered"),
+                        mUserData->mName.raw(),
+                        mData->mUuid.toString().raw());
 
     HRESULT rc = S_OK;
@@ -6013,5 +5956,5 @@
     // no config file exists yet, create it by calling saveSettings() too.
     if (    (mData->flModifications)
-         || (argNewRegistered && !mData->pMachineConfigFile->fileExists())
+         || (!mData->pMachineConfigFile->fileExists())
        )
     {
@@ -6029,5 +5972,5 @@
         commit();
 
-        mData->mRegistered = argNewRegistered;
+        mData->mRegistered = true;
     }
     else
@@ -6038,4 +5981,76 @@
 
     return rc;
+}
+
+/**
+ * Called from VirtualBox::UnregisterMachine() with the flags given there to
+ * give the machine a chance to clean up for itself.
+ * @param fDetachMedia As passed to VirtualBox::UnregisterMachine(). If true, all
+ *                     media are detached from the machine and its snapshots.
+ * @param llFiles
+ * @return
+ */
+HRESULT Machine::prepareUnregister(bool fCloseMedia,
+                                   MediaList &llMedia)
+{
+    AutoLimitedCaller autoCaller(this);
+    AssertComRCReturnRC(autoCaller.rc());
+
+    AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
+
+    if (mData->mSession.mState != SessionState_Closed)
+        return setError(VBOX_E_INVALID_OBJECT_STATE,
+                        tr("Cannot unregister the machine '%ls' because it has an open session"),
+                        mUserData->mName.raw());
+
+    // @todo optionally discard saved state
+    if (mData->mMachineState == MachineState_Saved)
+        return setError(VBOX_E_INVALID_VM_STATE,
+                        tr("Cannot unregister the machine '%ls' because it is in the Saved state"),
+                        mUserData->mName.raw());
+
+    // @todo optionally nuke snapshots
+    size_t snapshotCount = 0;
+    if (mData->mFirstSnapshot)
+        snapshotCount = mData->mFirstSnapshot->getAllChildrenCount() + 1;
+    if (snapshotCount)
+        return setError(VBOX_E_INVALID_OBJECT_STATE,
+                        tr("Cannot unregister the machine '%ls' because it has %d snapshots"),
+                        mUserData->mName.raw(), snapshotCount);
+
+    if (fCloseMedia)
+    {
+        // caller wants automatic detachment: then do that and report all media to the array
+        MediaData::AttachmentList::iterator it = mMediaData->mAttachments.begin();
+        while (it != mMediaData->mAttachments.end())
+        {
+            ComObjPtr<MediumAttachment> pAttach = *it;
+            ComObjPtr<Medium> pMedium = pAttach->getMedium();
+
+            if (!pMedium.isNull())
+                llMedia.push_back(pMedium);
+
+            setModified(IsModified_Storage);
+            mMediaData.backup();
+
+            // for non-hard disk media, detach straight away
+            // (same logic as in DetachDevice())
+            if (!pMedium.isNull())
+                pMedium->detachFrom(mData->mUuid);
+
+            // remove this attachment; erase() returns the iterator of the next element
+            it = mMediaData->mAttachments.erase(it);
+        }
+    }
+    else
+        // caller wants no automatic detachment: then fail if there are any
+        if (mMediaData->mAttachments.size())
+            return setError(VBOX_E_INVALID_OBJECT_STATE,
+                            tr("Cannot unregister the machine '%ls' because it has %d media attachments"),
+                               mMediaData->mAttachments.size());
+
+    mData->mRegistered = false;
+
+    return S_OK;
 }
 
Index: /trunk/src/VBox/Main/MediumImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/MediumImpl.cpp	(revision 30928)
+++ /trunk/src/VBox/Main/MediumImpl.cpp	(revision 30929)
@@ -1904,53 +1904,9 @@
     if (FAILED(autoCaller.rc())) return autoCaller.rc();
 
-    // we're accessing parent/child and backrefs, so lock the tree first, then ourselves
-    AutoMultiWriteLock2 multilock(&m->pVirtualBox->getMediaTreeLockHandle(),
-                                  this->lockHandle()
-                                  COMMA_LOCKVAL_SRC_POS);
-
-    bool wasCreated = true;
-    bool fNeedsSaveSettings = false;
-
-    switch (m->state)
-    {
-        case MediumState_NotCreated:
-            wasCreated = false;
-            break;
-        case MediumState_Created:
-        case MediumState_Inaccessible:
-            break;
-        default:
-            return setStateError();
-    }
-
-    if (m->backRefs.size() != 0)
-        return setError(VBOX_E_OBJECT_IN_USE,
-                        tr("Medium '%s' is attached to %d virtual machines"),
-                        m->strLocationFull.raw(), m->backRefs.size());
-
-    /* perform extra media-dependent close checks */
-    HRESULT rc = canClose();
-    if (FAILED(rc)) return rc;
-
-    if (wasCreated)
-    {
-        /* remove from the list of known media before performing actual
-         * uninitialization (to keep the media registry consistent on
-         * failure to do so) */
-        rc = unregisterWithVirtualBox(&fNeedsSaveSettings);
-        if (FAILED(rc)) return rc;
-    }
-
     // make a copy of VirtualBox pointer which gets nulled by uninit()
     ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
 
-    // leave the AutoCaller, as otherwise uninit() will simply hang
-    autoCaller.release();
-
-    /* Keep the locks held until after uninit, as otherwise the consistency
-     * of the medium tree cannot be guaranteed. */
-    uninit();
-
-    multilock.release();
+    bool fNeedsSaveSettings = false;
+    HRESULT rc = close(&fNeedsSaveSettings, autoCaller);
 
     if (fNeedsSaveSettings)
@@ -1960,5 +1916,5 @@
     }
 
-    return S_OK;
+    return rc;
 }
 
@@ -3820,4 +3776,69 @@
         }
     }
+
+    return rc;
+}
+
+/**
+ * Implementation for the public Medium::Close() with the exception of calling
+ * VirtualBox::saveSettings(), in case someone wants to call this for several
+ * media.
+ *
+ * After this returns with success, uninit() has been called on the medium, and
+ * the object is no longer usable ("not ready" state).
+ *
+ * @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.
+ *                This only works in "wait" mode; otherwise saveSettings gets called automatically by the thread that was created,
+ *                and this parameter is ignored.
+ * @param autoCaller AutoCaller instance which must have been created on the caller's stack for this medium. This gets released here
+ *                   upon which the Medium instance gets uninitialized.
+ * @return
+ */
+HRESULT Medium::close(bool *pfNeedsSaveSettings, AutoCaller &autoCaller)
+{
+    // we're accessing parent/child and backrefs, so lock the tree first, then ourselves
+    AutoMultiWriteLock2 multilock(&m->pVirtualBox->getMediaTreeLockHandle(),
+                                   this->lockHandle()
+                                           COMMA_LOCKVAL_SRC_POS);
+
+    bool wasCreated = true;
+
+    switch (m->state)
+    {
+        case MediumState_NotCreated:
+            wasCreated = false;
+            break;
+        case MediumState_Created:
+        case MediumState_Inaccessible:
+            break;
+        default:
+            return setStateError();
+    }
+
+    if (m->backRefs.size() != 0)
+        return setError(VBOX_E_OBJECT_IN_USE,
+                        tr("Medium '%s' is attached to %d virtual machines"),
+                           m->strLocationFull.raw(), m->backRefs.size());
+
+    // perform extra media-dependent close checks
+    HRESULT rc = canClose();
+    if (FAILED(rc)) return rc;
+
+    if (wasCreated)
+    {
+        // remove from the list of known media before performing actual
+        // uninitialization (to keep the media registry consistent on
+        // failure to do so)
+        rc = unregisterWithVirtualBox(pfNeedsSaveSettings);
+        if (FAILED(rc)) return rc;
+    }
+
+    // leave the AutoCaller, as otherwise uninit() will simply hang
+    autoCaller.release();
+
+    // Keep the locks held until after uninit, as otherwise the consistency
+    // of the medium tree cannot be guaranteed.
+    uninit();
 
     return rc;
@@ -4789,4 +4810,8 @@
 
 /**
+ * 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().
  *
@@ -4806,5 +4831,7 @@
 
 /**
- * Calls either VirtualBox::unregisterImage or VirtualBox::unregisterHardDisk depending
+ * 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.
  *
@@ -4850,6 +4877,5 @@
         if (pParentBackup)
         {
-            /* re-associate with the parent as we are still relatives in the
-             * registry */
+            // re-associate with the parent as we are still relatives in the registry
             m->pParent = pParentBackup;
             m->pParent->m->llChildren.push_back(this);
Index: /trunk/src/VBox/Main/VirtualBoxImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/VirtualBoxImpl.cpp	(revision 30928)
+++ /trunk/src/VBox/Main/VirtualBoxImpl.cpp	(revision 30929)
@@ -1367,5 +1367,7 @@
 
 /** @note Locks objects! */
-STDMETHODIMP VirtualBox::UnregisterMachine(IN_BSTR  aId,
+STDMETHODIMP VirtualBox::UnregisterMachine(IN_BSTR aId,
+                                           BOOL fCloseMedia,
+                                           ComSafeArrayOut(BSTR, aFiles),
                                            IMachine **aMachine)
 {
@@ -1382,13 +1384,41 @@
     if (FAILED(rc)) return rc;
 
-    // trySetRegistered needs VirtualBox object write lock
     AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
-    rc = pMachine->trySetRegistered(FALSE);
+
+    MediaList llMedia;
+    rc = pMachine->prepareUnregister(fCloseMedia, llMedia);
     if (FAILED(rc)) return rc;
 
-    /* remove from the collection of registered machines */
+    // remove from the collection of registered machines
     m->allMachines.removeChild(pMachine);
 
-    /* save the global registry */
+    if (fCloseMedia)
+    {
+        // now go thru the list of attached media reported by prepareUnregister() and close them all
+        SafeArray<BSTR> sfaFiles(llMedia.size());
+        size_t u = 0;
+        for (MediaList::const_iterator it = llMedia.begin();
+             it != llMedia.end();
+             ++it)
+        {
+            ComObjPtr<Medium> pMedium = *it;
+            Bstr bstrFile = pMedium->getLocationFull();
+
+            AutoCaller autoCaller2(pMedium);
+            if (FAILED(autoCaller2.rc())) return autoCaller2.rc();
+
+            pMedium->close(NULL /*fNeedsSaveSettings*/,     // we'll call saveSettings() in any case below
+                           autoCaller2);
+                // this uninitializes the medium
+
+            // report the path to the caller
+            bstrFile.detachTo(&sfaFiles[u]);
+            ++u;
+        }
+        // report all paths to the caller
+        sfaFiles.detachTo(ComSafeArrayOutArg(aFiles));
+    }
+
+    // save the global registry
     rc = saveSettings();
 
@@ -3544,5 +3574,5 @@
     if (autoCaller.state() != InInit)
     {
-        rc = aMachine->trySetRegistered(TRUE);
+        rc = aMachine->prepareRegister();
         if (FAILED(rc)) return rc;
     }
Index: /trunk/src/VBox/Main/idl/VirtualBox.xidl
===================================================================
--- /trunk/src/VBox/Main/idl/VirtualBox.xidl	(revision 30928)
+++ /trunk/src/VBox/Main/idl/VirtualBox.xidl	(revision 30929)
@@ -1742,10 +1742,24 @@
           Machine has snapshot or open session or medium attached.
         </result>
-
-      </desc>
+      </desc>
+
       <param name="id" type="uuid" mod="string" dir="in">
         <desc>UUID of the machine to unregister.</desc>
       </param>
-      <param name="machine" type="IMachine" dir="return">
+      <param name="fCloseMedia" type="boolean" dir="in">
+        <desc>If true, the method will automatically detach all media from the
+          machine and its snapshots, call <link to="IMedium::close" /> on each
+          medium, and the paths of all media files involved will be reported to
+          the caller in the @a aFiles array so the caller can then delete the
+          image files.
+          If false, the method will fail if media attachments are present.</desc>
+      </param>
+      <param name="aFiles" type="wstring" safearray="yes" dir="out">
+        <desc>
+          List of all files detached from medium attachments of the machine, if
+          @a fDetachMedia is true.
+        </desc>
+      </param>
+      <param name="machine" type="IMachine" dir="out">
         <desc>Unregistered machine object.</desc>
       </param>
Index: /trunk/src/VBox/Main/include/MachineImpl.h
===================================================================
--- /trunk/src/VBox/Main/include/MachineImpl.h	(revision 30928)
+++ /trunk/src/VBox/Main/include/MachineImpl.h	(revision 30929)
@@ -682,5 +682,6 @@
     bool checkForSpawnFailure();
 
-    HRESULT trySetRegistered(BOOL aRegistered);
+    HRESULT prepareRegister();
+    HRESULT prepareUnregister(bool fDetachMedia, MediaList &llMedia);
 
     HRESULT getSharedFolder(CBSTR aName,
Index: /trunk/src/VBox/Main/include/MediumImpl.h
===================================================================
--- /trunk/src/VBox/Main/include/MediumImpl.h	(revision 30928)
+++ /trunk/src/VBox/Main/include/MediumImpl.h	(revision 30929)
@@ -198,4 +198,5 @@
                               bool *pfNeedsSaveSettings);
 
+    HRESULT close(bool *pfNeedsSaveSettings, AutoCaller &autoCaller);
     HRESULT deleteStorage(ComObjPtr<Progress> *aProgress, bool aWait, bool *pfNeedsSaveSettings);
     HRESULT markForDeletion();
@@ -232,15 +233,5 @@
     HRESULT queryInfo();
 
-    /**
-     * 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.
-     */
     HRESULT canClose();
-
-    /**
-     * Unregisters this medium with mVirtualBox. Called by Close() under
-     * the medium tree lock.
-     */
     HRESULT unregisterWithVirtualBox(bool *pfNeedsSaveSettings);
 
Index: /trunk/src/VBox/Main/include/VirtualBoxImpl.h
===================================================================
--- /trunk/src/VBox/Main/include/VirtualBoxImpl.h	(revision 30928)
+++ /trunk/src/VBox/Main/include/VirtualBoxImpl.h	(revision 30929)
@@ -100,21 +100,21 @@
 
     /* IVirtualBox properties */
-    STDMETHOD(COMGETTER(Version)) (BSTR *aVersion);
-    STDMETHOD(COMGETTER(Revision)) (ULONG *aRevision);
-    STDMETHOD(COMGETTER(PackageType)) (BSTR *aPackageType);
-    STDMETHOD(COMGETTER(HomeFolder)) (BSTR *aHomeFolder);
-    STDMETHOD(COMGETTER(SettingsFilePath)) (BSTR *aSettingsFilePath);
-    STDMETHOD(COMGETTER(Host)) (IHost **aHost);
-    STDMETHOD(COMGETTER(SystemProperties)) (ISystemProperties **aSystemProperties);
-    STDMETHOD(COMGETTER(Machines)) (ComSafeArrayOut (IMachine *, aMachines));
-    STDMETHOD(COMGETTER(HardDisks)) (ComSafeArrayOut (IMedium *, aHardDisks));
-    STDMETHOD(COMGETTER(DVDImages)) (ComSafeArrayOut (IMedium *, aDVDImages));
-    STDMETHOD(COMGETTER(FloppyImages)) (ComSafeArrayOut (IMedium *, aFloppyImages));
-    STDMETHOD(COMGETTER(ProgressOperations)) (ComSafeArrayOut (IProgress *, aOperations));
-    STDMETHOD(COMGETTER(GuestOSTypes)) (ComSafeArrayOut (IGuestOSType *, aGuestOSTypes));
-    STDMETHOD(COMGETTER(SharedFolders)) (ComSafeArrayOut (ISharedFolder *, aSharedFolders));
-    STDMETHOD(COMGETTER(PerformanceCollector)) (IPerformanceCollector **aPerformanceCollector);
-    STDMETHOD(COMGETTER(DHCPServers)) (ComSafeArrayOut (IDHCPServer *, aDHCPServers));
-    STDMETHOD(COMGETTER(EventSource)) (IEventSource ** aEventSource);
+    STDMETHOD(COMGETTER(Version))               (BSTR *aVersion);
+    STDMETHOD(COMGETTER(Revision))              (ULONG *aRevision);
+    STDMETHOD(COMGETTER(PackageType))           (BSTR *aPackageType);
+    STDMETHOD(COMGETTER(HomeFolder))            (BSTR *aHomeFolder);
+    STDMETHOD(COMGETTER(SettingsFilePath))      (BSTR *aSettingsFilePath);
+    STDMETHOD(COMGETTER(Host))                  (IHost **aHost);
+    STDMETHOD(COMGETTER(SystemProperties))      (ISystemProperties **aSystemProperties);
+    STDMETHOD(COMGETTER(Machines))              (ComSafeArrayOut(IMachine *, aMachines));
+    STDMETHOD(COMGETTER(HardDisks))             (ComSafeArrayOut(IMedium *, aHardDisks));
+    STDMETHOD(COMGETTER(DVDImages))             (ComSafeArrayOut(IMedium *, aDVDImages));
+    STDMETHOD(COMGETTER(FloppyImages))          (ComSafeArrayOut(IMedium *, aFloppyImages));
+    STDMETHOD(COMGETTER(ProgressOperations))    (ComSafeArrayOut(IProgress *, aOperations));
+    STDMETHOD(COMGETTER(GuestOSTypes))          (ComSafeArrayOut(IGuestOSType *, aGuestOSTypes));
+    STDMETHOD(COMGETTER(SharedFolders))         (ComSafeArrayOut(ISharedFolder *, aSharedFolders));
+    STDMETHOD(COMGETTER(PerformanceCollector))  (IPerformanceCollector **aPerformanceCollector);
+    STDMETHOD(COMGETTER(DHCPServers))           (ComSafeArrayOut(IDHCPServer *, aDHCPServers));
+    STDMETHOD(COMGETTER(EventSource))           (IEventSource ** aEventSource);
 
     /* IVirtualBox methods */
@@ -128,5 +128,5 @@
     STDMETHOD(GetMachine) (IN_BSTR aId, IMachine **aMachine);
     STDMETHOD(FindMachine) (IN_BSTR aName, IMachine **aMachine);
-    STDMETHOD(UnregisterMachine) (IN_BSTR aId, IMachine **aMachine);
+    STDMETHOD(UnregisterMachine) (IN_BSTR aId, BOOL fCloseMedia, ComSafeArrayOut(BSTR, aFiles), IMachine **aMachine);
     STDMETHOD(CreateAppliance) (IAppliance **anAppliance);
 
Index: /trunk/src/VBox/Main/testcase/tstOVF.cpp
===================================================================
--- /trunk/src/VBox/Main/testcase/tstOVF.cpp	(revision 30928)
+++ /trunk/src/VBox/Main/testcase/tstOVF.cpp	(revision 30929)
@@ -238,5 +238,5 @@
     if (FAILED(rc)) throw MyError(rc, "Progress::WaitForCompletion() failed\n");
     pProgress->COMGETTER(ResultCode)(&rc2);
-    if (FAILED(rc2)) throw MyError(rc2, "Progress::GetResultCode() failed\n");
+    if (FAILED(rc2)) throw MyError(rc2, "Appliance::ImportMachines() failed\n", pProgress);
 
     com::SafeArray<BSTR> aMachineUUIDs;
@@ -262,6 +262,10 @@
  * @param pcszDest Target for dummy VMDK.
  */
-void copyDummyDiskImage(std::list<Utf8Str> &llFiles2Delete, const char *pcszDest)
+void copyDummyDiskImage(const char *pcszPrefix,
+                        std::list<Utf8Str> &llFiles2Delete,
+                        const char *pcszDest)
 {
+    RTPrintf("%s: copying ovf-dummy.vmdk to \"%s\"...\n", pcszPrefix, pcszDest);
+
     int vrc = RTFileCopy("ovf-testcases/ovf-dummy.vmdk", pcszDest);
     if (RT_FAILURE(vrc)) throw MyError(0, Utf8StrFmt("Cannot copy ovf-dummy.vmdk to %s: %Rra\n", pcszDest, vrc).c_str());
@@ -306,11 +310,11 @@
 
         // testcase 1: import ovf-joomla-0.9/joomla-1.1.4-ovf.ovf
-        copyDummyDiskImage(llFiles2Delete, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf-0.vmdk");
-        copyDummyDiskImage(llFiles2Delete, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf-1.vmdk");
+        copyDummyDiskImage("joomla-0.9", llFiles2Delete, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf-0.vmdk");
+        copyDummyDiskImage("joomla-0.9", llFiles2Delete, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf-1.vmdk");
         importOVF("joomla-0.9", pVirtualBox, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf.ovf", llMachinesCreated);
 
         // testcase 2: import ovf-winxp-vbox-sharedfolders/winxp.ovf
-        copyDummyDiskImage(llFiles2Delete, "ovf-testcases/ovf-winxp-vbox-sharedfolders/Windows 5.1 XP 1 merged.vmdk");
-        copyDummyDiskImage(llFiles2Delete, "ovf-testcases/ovf-winxp-vbox-sharedfolders/smallvdi.vmdk");
+        copyDummyDiskImage("winxp-vbox-sharedfolders", llFiles2Delete, "ovf-testcases/ovf-winxp-vbox-sharedfolders/Windows 5.1 XP 1 merged.vmdk");
+        copyDummyDiskImage("winxp-vbox-sharedfolders", llFiles2Delete, "ovf-testcases/ovf-winxp-vbox-sharedfolders/smallvdi.vmdk");
         importOVF("winxp-vbox-sharedfolders", pVirtualBox, "ovf-testcases/ovf-winxp-vbox-sharedfolders/winxp.ovf", llMachinesCreated);
 
@@ -330,42 +334,28 @@
              ++it)
         {
-//             const Guid &uuid = *it;
-//             Bstr bstrUUID(uuid.toUtf16());
-//             ComPtr<IMachine> pMachine;
-//             rc = pVirtualBox->GetMachine(bstrUUID, pMachine.asOutParam());
-//             if (FAILED(rc)) throw MyError(rc, "VirtualBox::FindMachine() failed\n");
-//
-//             SafeIfaceArray<IMediumAttachment> aMediumAttachments;
-//             rc = pMachine->COMGETTER(MediumAttachments)(ComSafeArrayAsOutParam(aMediumAttachments));
-//             if (FAILED(rc)) throw MyError(rc, "Machine::MediumAttachments() failed\n");
-//
-//             for (size_t u2 = 0;
-//                  u2 < aMediumAttachments.size();
-//                  ++u2)
-//             {
-//                 ComPtr<IMediumAttachment> pMediumAttachment(aMediumAttachments[u2]);
-//                 ComPtr<IMedium> pMedium;
-//                 rc = pMediumAttachment->COMGETTER(Medium)(pMedium.asOutParam());
-//                 if (FAILED(rc)) throw MyError(rc, "MediumAttachment::GetMedium() failed\n");
-//
-//                 if (!pMedium.isNull())
-//                 {
-//                     Bstr bstrLocation;
-//                     rc = pMedium->COMGETTER(Location)(bstrLocation.asOutParam());
-//                     if (FAILED(rc)) throw MyError(rc, "Medium::GetLocation() failed\n");
-//
-//                     llFiles2Delete.push_back(bstrLocation);
-//                 }
-//             }
-
-//             RTPrintf("  Deleting machine %ls...\n", bstrUUID.raw());
-//             pVirtualBox->UnregisterMachine(bstrUUID, pMachine.asOutParam());
-//             if (FAILED(rc)) throw MyError(rc, "VirtualBox::UnregisterMachine() failed\n");
-//
-//             if (!pMachine.isNull())
-//             {
-//                 rc = pMachine->DeleteSettings();
-//                 if (FAILED(rc)) throw MyError(rc, "Machine::DeleteSettings() failed\n");
-//             }
+            const Guid &uuid = *it;
+            Bstr bstrUUID(uuid.toUtf16());
+            ComPtr<IMachine> pMachine;
+            rc = pVirtualBox->GetMachine(bstrUUID, pMachine.asOutParam());
+            if (FAILED(rc)) throw MyError(rc, "VirtualBox::FindMachine() failed\n");
+
+            RTPrintf("  Deleting machine %ls...\n", bstrUUID.raw());
+            SafeArray<BSTR> sfaFiles;
+            rc = pVirtualBox->UnregisterMachine(bstrUUID,
+                                                true /* fDetachMedia */,
+                                                ComSafeArrayAsOutParam(sfaFiles),
+                                                pMachine.asOutParam());
+            if (FAILED(rc)) throw MyError(rc, "VirtualBox::UnregisterMachine() failed\n");
+
+            for (size_t u = 0;
+                 u < sfaFiles.size();
+                 ++u)
+            {
+                RTPrintf("    UnregisterMachine reported disk image %ls\n", sfaFiles[u]);
+                llFiles2Delete.push_back(sfaFiles[u]);
+            }
+
+            rc = pMachine->DeleteSettings();
+            if (FAILED(rc)) throw MyError(rc, "Machine::DeleteSettings() failed\n");
         }
     }
@@ -382,4 +372,5 @@
     {
         const Utf8Str &strFile = *it;
+        RTPrintf("Deleting file %s...\n", strFile.c_str());
         RTFileDelete(strFile.c_str());
     }
