Index: trunk/src/VBox/Frontends/VBoxManage/VBoxInternalManage.cpp
===================================================================
--- trunk/src/VBox/Frontends/VBoxManage/VBoxInternalManage.cpp	(revision 31002)
+++ trunk/src/VBox/Frontends/VBoxManage/VBoxInternalManage.cpp	(revision 31008)
@@ -1864,7 +1864,5 @@
         CHECK_ERROR_RET(aVirtualBox, FindMachine(Bstr(argv[0]), ptrMachine.asOutParam()), 1);
 
-    Bstr bstrMachineUuid;
-    CHECK_ERROR_RET(ptrMachine, COMGETTER(Id)(bstrMachineUuid.asOutParam()), 1);
-    CHECK_ERROR_RET(aVirtualBox, OpenExistingSession(aSession, bstrMachineUuid), 1);
+    CHECK_ERROR_RET(ptrMachine, LockForSession(aSession, true /* fPermitShared */, NULL), 1);
 
     /*
Index: trunk/src/VBox/Frontends/VBoxManage/VBoxManageControlVM.cpp
===================================================================
--- trunk/src/VBox/Frontends/VBoxManage/VBoxManageControlVM.cpp	(revision 31002)
+++ trunk/src/VBox/Frontends/VBoxManage/VBoxManageControlVM.cpp	(revision 31008)
@@ -73,5 +73,5 @@
     /* try to find the given machine */
     ComPtr <IMachine> machine;
-    Bstr machineuuid (a->argv[0]);
+    Bstr machineuuid(a->argv[0]);
     if (!Guid(machineuuid).isEmpty())
     {
@@ -88,5 +88,5 @@
 
     /* open a session for the VM */
-    CHECK_ERROR_RET(a->virtualBox, OpenExistingSession(a->session, machineuuid), 1);
+    CHECK_ERROR_RET(machine, LockForSession(a->session, true /* fPermitShared */, NULL), 1);
 
     do
Index: trunk/src/VBox/Frontends/VBoxManage/VBoxManageGuestCtrl.cpp
===================================================================
--- trunk/src/VBox/Frontends/VBoxManage/VBoxManageGuestCtrl.cpp	(revision 31002)
+++ trunk/src/VBox/Frontends/VBoxManage/VBoxManageGuestCtrl.cpp	(revision 31008)
@@ -285,9 +285,7 @@
         do
         {
-            Bstr uuid;
-            machine->COMGETTER(Id)(uuid.asOutParam());
-
-            /* open an existing session for VM - so the VM has to be running */
-            CHECK_ERROR_BREAK(a->virtualBox, OpenExistingSession(a->session, uuid));
+            /* open an existing session for VM */
+            CHECK_ERROR_BREAK(machine, LockForSession(a->session, true /* fPermitShared */, NULL));
+            // @todo r=dj assert that it's an existing session
 
             /* get the mutable session machine */
Index: trunk/src/VBox/Frontends/VBoxManage/VBoxManageGuestProp.cpp
===================================================================
--- trunk/src/VBox/Frontends/VBoxManage/VBoxManageGuestProp.cpp	(revision 31002)
+++ trunk/src/VBox/Frontends/VBoxManage/VBoxManageGuestProp.cpp	(revision 31008)
@@ -87,10 +87,6 @@
     if (machine)
     {
-        Bstr uuid;
-        machine->COMGETTER(Id)(uuid.asOutParam());
-
         /* open a session for the VM - new or existing */
-        if (FAILED (a->virtualBox->OpenSession(a->session, uuid)))
-            CHECK_ERROR_RET(a->virtualBox, OpenExistingSession(a->session, uuid), 1);
+        CHECK_ERROR_RET(machine, LockForSession(a->session, true /* fPermitShared */, NULL), 1);
 
         /* get the mutable session machine */
@@ -156,10 +152,6 @@
     if (machine)
     {
-        Bstr uuid;
-        machine->COMGETTER(Id)(uuid.asOutParam());
-
         /* open a session for the VM - new or existing */
-        if (FAILED (a->virtualBox->OpenSession(a->session, uuid)))
-            CHECK_ERROR_RET (a->virtualBox, OpenExistingSession(a->session, uuid), 1);
+        CHECK_ERROR_RET(machine, LockForSession(a->session, true /* fPermitShared */, NULL), 1);
 
         /* get the mutable session machine */
@@ -220,18 +212,14 @@
     if (machine)
     {
-        Bstr uuid;
-        machine->COMGETTER(Id)(uuid.asOutParam());
-
         /* open a session for the VM - new or existing */
-        if (FAILED(a->virtualBox->OpenSession(a->session, uuid)))
-            CHECK_ERROR_RET (a->virtualBox, OpenExistingSession(a->session, uuid), 1);
+        CHECK_ERROR_RET(machine, LockForSession(a->session, true /* fPermitShared */, NULL), 1);
 
         /* get the mutable session machine */
         a->session->COMGETTER(Machine)(machine.asOutParam());
 
-        com::SafeArray <BSTR> names;
-        com::SafeArray <BSTR> values;
-        com::SafeArray <ULONG64> timestamps;
-        com::SafeArray <BSTR> flags;
+        com::SafeArray<BSTR> names;
+        com::SafeArray<BSTR> values;
+        com::SafeArray<ULONG64> timestamps;
+        com::SafeArray<BSTR> flags;
         CHECK_ERROR(machine, EnumerateGuestProperties(Bstr(Utf8Patterns),
                                                       ComSafeArrayAsOutParam(names),
Index: trunk/src/VBox/Frontends/VBoxManage/VBoxManageInfo.cpp
===================================================================
--- trunk/src/VBox/Frontends/VBoxManage/VBoxManageInfo.cpp	(revision 31002)
+++ trunk/src/VBox/Frontends/VBoxManage/VBoxManageInfo.cpp	(revision 31008)
@@ -2132,8 +2132,8 @@
             details = VMINFO_STANDARD;
 
-        ComPtr <IConsole> console;
+        ComPtr<IConsole> console;
 
         /* open an existing session for the VM */
-        rc = a->virtualBox->OpenExistingSession(a->session, uuid);
+        rc = machine->LockForSession(a->session, true /* fPermitShared */, NULL);
         if (SUCCEEDED(rc))
             /* get the session machine */
Index: trunk/src/VBox/Frontends/VBoxManage/VBoxManageMisc.cpp
===================================================================
--- trunk/src/VBox/Frontends/VBoxManage/VBoxManageMisc.cpp	(revision 31002)
+++ trunk/src/VBox/Frontends/VBoxManage/VBoxManageMisc.cpp	(revision 31008)
@@ -356,8 +356,4 @@
     if (machine)
     {
-        Bstr uuid;
-        machine->COMGETTER(Id)(uuid.asOutParam());
-
-
         Bstr env;
 #if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS)
@@ -373,6 +369,5 @@
 #endif
         ComPtr<IProgress> progress;
-        CHECK_ERROR_RET(a->virtualBox, OpenRemoteSession(a->session, uuid, sessionType,
-                                                         env, progress.asOutParam()), rc);
+        CHECK_ERROR_RET(machine, LaunchVMProcess(a->session, sessionType, env, progress.asOutParam()), rc);
         RTPrintf("Waiting for the VM to power on...\n");
         CHECK_ERROR_RET(progress, WaitForCompletion(-1), 1);
@@ -423,7 +418,5 @@
         {
             /* we have to open a session for this task */
-            Bstr guid;
-            machine->COMGETTER(Id)(guid.asOutParam());
-            CHECK_ERROR_BREAK(a->virtualBox, OpenSession(a->session, guid));
+            CHECK_ERROR_BREAK(machine, LockForSession(a->session, false /* fPermitShared */, NULL));
             do
             {
@@ -459,7 +452,5 @@
         {
             /* we have to open a session for this task */
-            Bstr guid;
-            machine->COMGETTER(Id)(guid.asOutParam());
-            CHECK_ERROR_BREAK(a->virtualBox, OpenSession(a->session, guid));
+            CHECK_ERROR_BREAK(machine, LockForSession(a->session, false /* fPermitShared */, NULL));
             do
             {
@@ -671,6 +662,4 @@
     if (!machine)
         return 1;
-    Bstr uuid;
-    machine->COMGETTER(Id)(uuid.asOutParam());
 
     if (!strcmp(a->argv[0], "add"))
@@ -737,5 +726,5 @@
 
             /* open an existing session for the VM */
-            CHECK_ERROR_RET(a->virtualBox, OpenExistingSession(a->session, uuid), 1);
+            CHECK_ERROR_RET(machine, LockForSession(a->session, true /* fPermitShared */, NULL), 1);
             /* get the session machine */
             CHECK_ERROR_RET(a->session, COMGETTER(Machine)(machine.asOutParam()), 1);
@@ -751,5 +740,5 @@
         {
             /* open a session for the VM */
-            CHECK_ERROR_RET(a->virtualBox, OpenSession(a->session, uuid), 1);
+            CHECK_ERROR_RET(machine, LockForSession(a->session, false /* fPermitShared */, NULL), 1);
 
             /* get the mutable session machine */
@@ -801,5 +790,5 @@
 
             /* open an existing session for the VM */
-            CHECK_ERROR_RET(a->virtualBox, OpenExistingSession(a->session, uuid), 1);
+            CHECK_ERROR_RET(machine, LockForSession(a->session, true /* fPermitShared */, NULL), 1);
             /* get the session machine */
             CHECK_ERROR_RET(a->session, COMGETTER(Machine)(machine.asOutParam()), 1);
@@ -815,5 +804,5 @@
         {
             /* open a session for the VM */
-            CHECK_ERROR_RET(a->virtualBox, OpenSession(a->session, uuid), 1);
+            CHECK_ERROR_RET(machine, LockForSession(a->session, false /* fPermitShared */, NULL), 1);
 
             /* get the mutable session machine */
@@ -842,12 +831,12 @@
 
     /* try to find the given machine */
-    ComPtr <IMachine> machine;
-    Bstr uuid (a->argv[0]);
-    if (!Guid (a->argv[0]).isEmpty())
+    ComPtr<IMachine> machine;
+    Bstr uuid(a->argv[0]);
+    if (!Guid(a->argv[0]).isEmpty())
         CHECK_ERROR(a->virtualBox, GetMachine(uuid, machine.asOutParam()));
     else
     {
         CHECK_ERROR(a->virtualBox, FindMachine(Bstr(a->argv[0]), machine.asOutParam()));
-        if (SUCCEEDED (rc))
+        if (SUCCEEDED(rc))
             machine->COMGETTER(Id)(uuid.asOutParam());
     }
@@ -885,5 +874,5 @@
 
     /* open an existing session for the VM. */
-    CHECK_ERROR(a->virtualBox, OpenExistingSession(a->session, uuid));
+    CHECK_ERROR(machine, LockForSession(a->session, true /* fPermitShared */, NULL));
     if (SUCCEEDED(rc))
     {
Index: trunk/src/VBox/Frontends/VBoxManage/VBoxManageModifyVM.cpp
===================================================================
--- trunk/src/VBox/Frontends/VBoxManage/VBoxManageModifyVM.cpp	(revision 31002)
+++ trunk/src/VBox/Frontends/VBoxManage/VBoxManageModifyVM.cpp	(revision 31008)
@@ -275,5 +275,5 @@
     HRESULT rc;
     Bstr name;
-    Bstr machineuuid (a->argv[0]);
+    Bstr machineuuid(a->argv[0]);
     RTGETOPTUNION ValueUnion;
     RTGETOPTSTATE GetOptState;
@@ -312,5 +312,5 @@
 
     /* open a session for the VM */
-    CHECK_ERROR_RET(a->virtualBox, OpenSession(a->session, machineuuid), 1);
+    CHECK_ERROR_RET(machine, LockForSession(a->session, false /* fPermitShared */, NULL), 1);
 
     /* get the mutable session machine */
Index: trunk/src/VBox/Frontends/VBoxManage/VBoxManageSnapshot.cpp
===================================================================
--- trunk/src/VBox/Frontends/VBoxManage/VBoxManageSnapshot.cpp	(revision 31002)
+++ trunk/src/VBox/Frontends/VBoxManage/VBoxManageSnapshot.cpp	(revision 31008)
@@ -236,13 +236,9 @@
     if (!pMachine)
         return 1;
-    Bstr guidMachine;
-    pMachine->COMGETTER(Id)(guidMachine.asOutParam());
 
     do
     {
-        /* we have to open a session for this task. First try an existing session */
-        rc = a->virtualBox->OpenExistingSession(a->session, guidMachine);
-        if (FAILED(rc))
-            CHECK_ERROR_BREAK(a->virtualBox, OpenSession(a->session, guidMachine));
+        /* we have to open a session for this task (new or shared) */
+        rc = pMachine->LockForSession(a->session, true /* fPermitShared */, NULL);
         ComPtr<IConsole> console;
         CHECK_ERROR_BREAK(a->session, COMGETTER(Console)(console.asOutParam()));
Index: trunk/src/VBox/Frontends/VBoxManage/VBoxManageStorageController.cpp
===================================================================
--- trunk/src/VBox/Frontends/VBoxManage/VBoxManageStorageController.cpp	(revision 31002)
+++ trunk/src/VBox/Frontends/VBoxManage/VBoxManageStorageController.cpp	(revision 31008)
@@ -60,5 +60,4 @@
     ULONG port   = ~0U;
     ULONG device = ~0U;
-    bool fRunTime = false;
     bool fForceUnmount = false;
     const char *pszCtl  = NULL;
@@ -148,5 +147,5 @@
         }
     }
-    
+
     if (FAILED(rc))
         return 1;
@@ -173,13 +172,8 @@
     }
 
-    /* open a session for the VM */
-    rc = a->virtualBox->OpenSession(a->session, machineuuid);
-    if (FAILED(rc))
-    {
-        /* try to open an existing session for the VM */
-        CHECK_ERROR_RET(a->virtualBox, OpenExistingSession(a->session, machineuuid), 1);
-        fRunTime = true;
-    }
-
+    /* open a session for the VM (new or shared) */
+    SessionType_T type;
+    rc = machine->LockForSession(a->session, true /* fPermitShared */, &type);
+    bool fRunTime = (type == SessionType_Shared);
     if (fRunTime && !RTStrICmp(pszType, "hdd"))
     {
@@ -761,5 +755,5 @@
 
     /* open a session for the VM */
-    CHECK_ERROR_RET(a->virtualBox, OpenSession (a->session, machineuuid), 1);
+    CHECK_ERROR_RET(machine, LockForSession(a->session, false /* fPermitShared */, NULL), 1);
 
     /* get the mutable session machine */
Index: trunk/src/VBox/Frontends/VBoxManage/VBoxManageUSB.cpp
===================================================================
--- trunk/src/VBox/Frontends/VBoxManage/VBoxManageUSB.cpp	(revision 31002)
+++ trunk/src/VBox/Frontends/VBoxManage/VBoxManageUSB.cpp	(revision 31008)
@@ -416,12 +416,10 @@
     else
     {
-        Bstr uuid;
-        cmd.mMachine->COMGETTER(Id)(uuid.asOutParam());
         /* open a session for the VM */
-        CHECK_ERROR_RET (a->virtualBox, OpenSession(a->session, uuid), 1);
+        CHECK_ERROR_RET(cmd.mMachine, LockForSession(a->session, false /* fPermitShared */, NULL), 1);
         /* get the mutable session machine */
         a->session->COMGETTER(Machine)(cmd.mMachine.asOutParam());
         /* and get the USB controller */
-        CHECK_ERROR_RET (cmd.mMachine, COMGETTER(USBController) (ctl.asOutParam()), 1);
+        CHECK_ERROR_RET(cmd.mMachine, COMGETTER(USBController)(ctl.asOutParam()), 1);
     }
 
