Index: /trunk/src/VBox/Main/ConsoleImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/ConsoleImpl.cpp	(revision 29803)
+++ /trunk/src/VBox/Main/ConsoleImpl.cpp	(revision 29804)
@@ -3417,104 +3417,106 @@
     AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
 
-    /* Don't do anything if the VM isn't running */
-    if (!mpVM)
-        return S_OK;
-
-    /* protect mpVM */
-    AutoVMCaller autoVMCaller(this);
-    if (FAILED(autoVMCaller.rc())) return autoVMCaller.rc();
-
-    /* Get the properties we need from the adapter */
-    BOOL fCableConnected, fTraceEnabled;
-    HRESULT rc = aNetworkAdapter->COMGETTER(CableConnected)(&fCableConnected);
-    AssertComRC(rc);
-    if (SUCCEEDED(rc))
-    {
-        rc = aNetworkAdapter->COMGETTER(TraceEnabled)(&fTraceEnabled);
-        AssertComRC(rc);
-    }
-    if (SUCCEEDED(rc))
-    {
-        ULONG ulInstance;
-        rc = aNetworkAdapter->COMGETTER(Slot)(&ulInstance);
+    HRESULT rc = S_OK;
+
+    /* don't trigger network change if the VM isn't running */
+    if (mpVM)
+    {
+        /* protect mpVM */
+        AutoVMCaller autoVMCaller(this);
+        if (FAILED(autoVMCaller.rc())) return autoVMCaller.rc();
+
+        /* Get the properties we need from the adapter */
+        BOOL fCableConnected, fTraceEnabled;
+        rc = aNetworkAdapter->COMGETTER(CableConnected)(&fCableConnected);
         AssertComRC(rc);
         if (SUCCEEDED(rc))
         {
-            /*
-             * Find the pcnet instance, get the config interface and update
-             * the link state.
-             */
-            NetworkAdapterType_T adapterType;
-            rc = aNetworkAdapter->COMGETTER(AdapterType)(&adapterType);
+            rc = aNetworkAdapter->COMGETTER(TraceEnabled)(&fTraceEnabled);
             AssertComRC(rc);
-            const char *pszAdapterName = NULL;
-            switch (adapterType)
+        }
+        if (SUCCEEDED(rc))
+        {
+            ULONG ulInstance;
+            rc = aNetworkAdapter->COMGETTER(Slot)(&ulInstance);
+            AssertComRC(rc);
+            if (SUCCEEDED(rc))
             {
-                case NetworkAdapterType_Am79C970A:
-                case NetworkAdapterType_Am79C973:
-                    pszAdapterName = "pcnet";
-                    break;
+                /*
+                 * Find the pcnet instance, get the config interface and update
+                 * the link state.
+                 */
+                NetworkAdapterType_T adapterType;
+                rc = aNetworkAdapter->COMGETTER(AdapterType)(&adapterType);
+                AssertComRC(rc);
+                const char *pszAdapterName = NULL;
+                switch (adapterType)
+                {
+                    case NetworkAdapterType_Am79C970A:
+                    case NetworkAdapterType_Am79C973:
+                        pszAdapterName = "pcnet";
+                        break;
 #ifdef VBOX_WITH_E1000
-                case NetworkAdapterType_I82540EM:
-                case NetworkAdapterType_I82543GC:
-                case NetworkAdapterType_I82545EM:
-                    pszAdapterName = "e1000";
-                    break;
+                    case NetworkAdapterType_I82540EM:
+                    case NetworkAdapterType_I82543GC:
+                    case NetworkAdapterType_I82545EM:
+                        pszAdapterName = "e1000";
+                        break;
 #endif
 #ifdef VBOX_WITH_VIRTIO
-                case NetworkAdapterType_Virtio:
-                    pszAdapterName = "virtio-net";
-                    break;
+                    case NetworkAdapterType_Virtio:
+                        pszAdapterName = "virtio-net";
+                        break;
 #endif
-                default:
-                    AssertFailed();
-                    pszAdapterName = "unknown";
-                    break;
-            }
-
-            PPDMIBASE pBase;
-            int vrc = PDMR3QueryDeviceLun(mpVM, pszAdapterName, ulInstance, 0, &pBase);
-            ComAssertRC(vrc);
-            if (RT_SUCCESS(vrc))
-            {
-                Assert(pBase);
-                PPDMINETWORKCONFIG pINetCfg;
-                pINetCfg = PDMIBASE_QUERY_INTERFACE(pBase, PDMINETWORKCONFIG);
-                if (pINetCfg)
+                    default:
+                        AssertFailed();
+                        pszAdapterName = "unknown";
+                        break;
+                }
+
+                PPDMIBASE pBase;
+                int vrc = PDMR3QueryDeviceLun(mpVM, pszAdapterName, ulInstance, 0, &pBase);
+                ComAssertRC(vrc);
+                if (RT_SUCCESS(vrc))
                 {
-                    Log(("Console::onNetworkAdapterChange: setting link state to %d\n",
-                          fCableConnected));
-                    vrc = pINetCfg->pfnSetLinkState(pINetCfg,
-                                                    fCableConnected ? PDMNETWORKLINKSTATE_UP
-                                                                    : PDMNETWORKLINKSTATE_DOWN);
-                    ComAssertRC(vrc);
-                }
+                    Assert(pBase);
+                    PPDMINETWORKCONFIG pINetCfg;
+                    pINetCfg = PDMIBASE_QUERY_INTERFACE(pBase, PDMINETWORKCONFIG);
+                    if (pINetCfg)
+                    {
+                        Log(("Console::onNetworkAdapterChange: setting link state to %d\n",
+                              fCableConnected));
+                        vrc = pINetCfg->pfnSetLinkState(pINetCfg,
+                                                        fCableConnected ? PDMNETWORKLINKSTATE_UP
+                                                                        : PDMNETWORKLINKSTATE_DOWN);
+                        ComAssertRC(vrc);
+                    }
 #ifdef VBOX_DYNAMIC_NET_ATTACH
-                if (RT_SUCCESS(vrc) && changeAdapter)
-                {
-                    VMSTATE enmVMState = VMR3GetState(mpVM);
-                    if (    enmVMState == VMSTATE_RUNNING    /** @todo LiveMigration: Forbit or deal correctly with the _LS variants */
-                        ||  enmVMState == VMSTATE_SUSPENDED)
+                    if (RT_SUCCESS(vrc) && changeAdapter)
                     {
-                        if (fTraceEnabled && fCableConnected && pINetCfg)
+                        VMSTATE enmVMState = VMR3GetState(mpVM);
+                        if (    enmVMState == VMSTATE_RUNNING    /** @todo LiveMigration: Forbid or deal correctly with the _LS variants */
+                            ||  enmVMState == VMSTATE_SUSPENDED)
                         {
-                            vrc = pINetCfg->pfnSetLinkState(pINetCfg, PDMNETWORKLINKSTATE_DOWN);
-                            ComAssertRC(vrc);
-                        }
-
-                        rc = doNetworkAdapterChange(pszAdapterName, ulInstance, 0, aNetworkAdapter);
-
-                        if (fTraceEnabled && fCableConnected && pINetCfg)
-                        {
-                            vrc = pINetCfg->pfnSetLinkState(pINetCfg, PDMNETWORKLINKSTATE_UP);
-                            ComAssertRC(vrc);
+                            if (fTraceEnabled && fCableConnected && pINetCfg)
+                            {
+                                vrc = pINetCfg->pfnSetLinkState(pINetCfg, PDMNETWORKLINKSTATE_DOWN);
+                                ComAssertRC(vrc);
+                            }
+
+                            rc = doNetworkAdapterChange(pszAdapterName, ulInstance, 0, aNetworkAdapter);
+
+                            if (fTraceEnabled && fCableConnected && pINetCfg)
+                            {
+                                vrc = pINetCfg->pfnSetLinkState(pINetCfg, PDMNETWORKLINKSTATE_UP);
+                                ComAssertRC(vrc);
+                            }
                         }
                     }
+#endif /* VBOX_DYNAMIC_NET_ATTACH */
                 }
-#endif /* VBOX_DYNAMIC_NET_ATTACH */
+
+                if (RT_FAILURE(vrc))
+                    rc = E_FAIL;
             }
-
-            if (RT_FAILURE(vrc))
-                rc = E_FAIL;
         }
     }
@@ -3724,15 +3726,15 @@
     AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
 
-    /* Don't do anything if the VM isn't running */
-    if (!mpVM)
-        return S_OK;
-
     HRESULT rc = S_OK;
 
-    /* protect mpVM */
-    AutoVMCaller autoVMCaller(this);
-    if (FAILED(autoVMCaller.rc())) return autoVMCaller.rc();
-
-    /* nothing to do so far */
+    /* don't trigger serial port change if the VM isn't running */
+    if (mpVM)
+    {
+        /* protect mpVM */
+        AutoVMCaller autoVMCaller(this);
+        if (FAILED(autoVMCaller.rc())) return autoVMCaller.rc();
+
+        /* nothing to do so far */
+    }
 
     /* notify console callbacks on success */
@@ -3758,15 +3760,15 @@
     AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
 
-    /* Don't do anything if the VM isn't running */
-    if (!mpVM)
-        return S_OK;
-
     HRESULT rc = S_OK;
 
-    /* protect mpVM */
-    AutoVMCaller autoVMCaller(this);
-    if (FAILED(autoVMCaller.rc())) return autoVMCaller.rc();
-
-    /* nothing to do so far */
+    /* don't trigger parallel port change if the VM isn't running */
+    if (mpVM)
+    {
+        /* protect mpVM */
+        AutoVMCaller autoVMCaller(this);
+        if (FAILED(autoVMCaller.rc())) return autoVMCaller.rc();
+
+        /* nothing to do so far */
+    }
 
     /* notify console callbacks on success */
@@ -3792,15 +3794,15 @@
     AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
 
-    /* Don't do anything if the VM isn't running */
-    if (!mpVM)
-        return S_OK;
-
     HRESULT rc = S_OK;
 
-    /* protect mpVM */
-    AutoVMCaller autoVMCaller(this);
-    if (FAILED(autoVMCaller.rc())) return autoVMCaller.rc();
-
-    /* nothing to do so far */
+    /* don't trigger storage controller change if the VM isn't running */
+    if (mpVM)
+    {
+        /* protect mpVM */
+        AutoVMCaller autoVMCaller(this);
+        if (FAILED(autoVMCaller.rc())) return autoVMCaller.rc();
+
+        /* nothing to do so far */
+    }
 
     /* notify console callbacks on success */
@@ -3826,15 +3828,15 @@
     AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
 
-    /* Don't do anything if the VM isn't running */
-    if (!mpVM)
-        return S_OK;
-
     HRESULT rc = S_OK;
 
-    /* protect mpVM */
-    AutoVMCaller autoVMCaller(this);
-    if (FAILED(autoVMCaller.rc())) return autoVMCaller.rc();
-
-    rc = doMediumChange(aMediumAttachment, !!aForce);
+    /* don't trigger medium change if the VM isn't running */
+    if (mpVM)
+    {
+        /* protect mpVM */
+        AutoVMCaller autoVMCaller(this);
+        if (FAILED(autoVMCaller.rc())) return autoVMCaller.rc();
+
+        rc = doMediumChange(aMediumAttachment, !!aForce);
+    }
 
     /* notify console callbacks on success */
@@ -3860,18 +3862,18 @@
     AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
 
-    /* Don't do anything if the VM isn't running */
-    if (!mpVM)
-        return S_OK;
-
     HRESULT rc = S_OK;
 
-    /* protect mpVM */
-    AutoVMCaller autoVMCaller(this);
-    if (FAILED(autoVMCaller.rc())) return autoVMCaller.rc();
-
-    if (aRemove)
-        rc = doCPURemove(aCPU);
-    else
-        rc = doCPUAdd(aCPU);
+    /* don't trigger CPU change if the VM isn't running */
+    if (mpVM)
+    {
+        /* protect mpVM */
+        AutoVMCaller autoVMCaller(this);
+        if (FAILED(autoVMCaller.rc())) return autoVMCaller.rc();
+
+        if (aRemove)
+            rc = doCPURemove(aCPU);
+        else
+            rc = doCPUAdd(aCPU);
+    }
 
     /* notify console callbacks on success */
@@ -3975,20 +3977,22 @@
     AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
 
-    /* Ignore if no VM is running yet. */
-    if (!mpVM)
-        return S_OK;
-
     HRESULT rc = S_OK;
 
-/// @todo (dmik)
-// check for the Enabled state and disable virtual USB controller??
-// Anyway, if we want to query the machine's USB Controller we need to cache
-// it to mUSBController in #init() (as it is done with mDVDDrive).
-//
-// bird: While the VM supports hot-plugging, I doubt any guest can handle it at this time... :-)
-//
-//    /* protect mpVM */
-//    AutoVMCaller autoVMCaller(this);
-//    if (FAILED(autoVMCaller.rc())) return autoVMCaller.rc();
+    /* don't trigger USB controller change if the VM isn't running */
+    if (mpVM)
+    {
+        /// @todo implement one day.
+        // Anyway, if we want to query the machine's USB Controller we need
+        // to cache it to mUSBController in #init() (similar to mDVDDrive).
+        //
+        // bird: While the VM supports hot-plugging, I doubt any guest can
+        // handle it at this time... :-)
+
+        /* protect mpVM */
+        AutoVMCaller autoVMCaller(this);
+        if (FAILED(autoVMCaller.rc())) return autoVMCaller.rc();
+
+        /* nothing to do so far */
+    }
 
     /* notify console callbacks on success */
