Index: /trunk/include/VBox/com/ptr.h
===================================================================
--- /trunk/include/VBox/com/ptr.h	(revision 6439)
+++ /trunk/include/VBox/com/ptr.h	(revision 6440)
@@ -62,4 +62,5 @@
 {
 protected:
+
     static void addref (C *p) { p->AddRef(); }
     static void release (C *p) { p->Release(); }
@@ -73,4 +74,5 @@
 {
 protected:
+
     static void addref (C *p) {}
     static void release (C *p) {}
@@ -78,14 +80,79 @@
 
 /**
+ *  Equality operations for the ComPtrBase template.
+ */
+template <class C>
+class ComPtrEqOps
+{
+protected:
+
+    template <class I>
+    static bool equals (C *aThis, I *aThat)
+    {
+        IUnknown *thatUnk = NULL, *thisUnk = NULL;
+        if (aThat)
+            aThat->QueryInterface (COM_IIDOF (IUnknown), (void **) &thatUnk);
+        if (aThis)
+            aThis->QueryInterface (COM_IIDOF (IUnknown), (void **) &thisUnk);
+        bool equal = thisUnk == thatUnk;
+        if (thisUnk)
+            thisUnk->Release();
+        if (thatUnk)
+            thatUnk->Release();
+        return equal;
+    }
+
+    /* specialization for IUnknown */
+    template<>
+    static bool equals <IUnknown> (C *aThis, IUnknown *aThat)
+    {
+        IUnknown *thisUnk = NULL;
+        if (aThis)
+            aThis->QueryInterface (COM_IIDOF (IUnknown), (void **) &thisUnk);
+        bool equal = thisUnk == aThat;
+        if (thisUnk)
+            thisUnk->Release();
+        return equal;
+    }
+};
+
+/** Specialization for IUnknown */
+template<>
+class ComPtrEqOps <IUnknown>
+{
+protected:
+
+    template <class I>
+    static bool equals (IUnknown *aThis, I *aThat)
+    {
+        IUnknown *thatUnk = NULL;
+        if (aThat)
+            aThat->QueryInterface (COM_IIDOF (IUnknown), (void **) &thatUnk);
+        bool equal = aThis == thatUnk;
+        if (thatUnk)
+            thatUnk->Release();
+        return equal;
+    }
+
+    /* specialization for IUnknown */
+    template<>
+    static bool equals <IUnknown> (IUnknown *aThis, IUnknown *aThat)
+    {
+        return aThis == aThat;
+    }
+};
+
+/** 
  *  Base template for smart COM pointers. Not intended to be used directly.
  */
 template <class C, template <class> class RefOps = ComStrongRef>
-class ComPtrBase : protected RefOps <C>
+class ComPtrBase : protected RefOps <C>, protected ComPtrEqOps <C>
 {
 public:
 
-    // a special template to disable AddRef()/Release()
+    /* special template to disable AddRef()/Release() */
     template <class I>
-    class NoAddRefRelease : public I {
+    class NoAddRefRelease : public I
+    {
         private:
 #if !defined (VBOX_WITH_XPCOM)
@@ -106,9 +173,12 @@
     ~ComPtrBase() { release(); }
 
-    ComPtrBase &operator= (const ComPtrBase &that) {
+    ComPtrBase &operator= (const ComPtrBase &that)
+    {
         safe_assign (that.p);
         return *this;
     }
-    ComPtrBase &operator= (C *that_p) {
+
+    ComPtrBase &operator= (C *that_p)
+    {
         safe_assign (that_p);
         return *this;
@@ -117,12 +187,15 @@
 public:
 
-    void setNull() {
+    void setNull()
+    {
         release();
         p = NULL;
     }
 
-    bool isNull() const {
+    bool isNull() const
+    {
         return (p == NULL);
     }
+
     bool operator! () const { return isNull(); }
 
@@ -131,20 +204,12 @@
 
     template <class I>
-    bool equalsTo (I *i) const {
-        IUnknown *this_unk = NULL, *that_unk = NULL;
-        if (i)
-            i->QueryInterface (COM_IIDOF (IUnknown), (void**) &that_unk);
-        if (p)
-            p->QueryInterface (COM_IIDOF (IUnknown), (void**) &this_unk);
-        bool equal = this_unk == that_unk;
-        if (that_unk)
-            that_unk->Release();
-        if (this_unk)
-            this_unk->Release();
-        return equal;
+    bool equalsTo (I *aThat) const
+    {
+        return equals (p, aThat);
     }
 
     template <class OC>
-    bool equalsTo (const ComPtrBase <OC> &oc) const {
+    bool equalsTo (const ComPtrBase <OC> &oc) const
+    {
         return equalsTo ((OC *) oc);
     }
@@ -157,5 +222,6 @@
      *  pointer).
      */
-    NoAddRefRelease <C> *operator-> () const {
+    NoAddRefRelease <C> *operator-> () const
+    {
         AssertMsg (p, ("Managed pointer must not be null\n"));
         return (NoAddRefRelease <C> *) p;
@@ -163,19 +229,25 @@
 
     template <class I>
-    HRESULT queryInterfaceTo (I **pp) const {
-        if (pp) {
-            if (p) {
-                return p->QueryInterface (COM_IIDOF (I), (void**) pp);
-            } else {
+    HRESULT queryInterfaceTo (I **pp) const
+    {
+        if (pp)
+        {
+            if (p)
+            {
+                return p->QueryInterface (COM_IIDOF (I), (void **) pp);
+            }
+            else
+            {
                 *pp = NULL;
                 return S_OK;
             }
-        } else {
-            return E_INVALIDARG;
         }
+
+        return E_INVALIDARG;
     }
 
     /** Intended to pass instances as out parameters to interface methods */
-    C **asOutParam() {
+    C **asOutParam()
+    {
         setNull();
         return &p;
@@ -184,15 +256,19 @@
 private:
 
-    void addref() {
+    void addref()
+    {
         if (p)
             RefOps <C>::addref (p);
     }
-    void release() {
+
+    void release()
+    {
         if (p)
             RefOps <C>::release (p);
     }
 
-    void safe_assign (C *that_p) {
-        // be aware of self-assignment
+    void safe_assign (C *that_p)
+    {
+        /* be aware of self-assignment */
         if (that_p)
             RefOps <C>::addref (that_p);
@@ -219,5 +295,6 @@
     ComPtr () : Base() {}
     ComPtr (const ComPtr &that) : Base (that) {}
-    ComPtr &operator= (const ComPtr &that) {
+    ComPtr &operator= (const ComPtr &that)
+    {
         Base::operator= (that);
         return *this;
@@ -226,5 +303,6 @@
     template <class OI>
     ComPtr (OI *that_p) : Base () { operator= (that_p); }
-    // specialization for I
+
+    /* specialization for I */
     ComPtr (I *that_p) : Base (that_p) {}
 
@@ -233,5 +311,6 @@
 
     template <class OI>
-    ComPtr &operator= (OI *that_p) {
+    ComPtr &operator= (OI *that_p)
+    {
         if (that_p)
             that_p->QueryInterface (COM_IIDOF (I), (void **) Base::asOutParam());
@@ -240,6 +319,8 @@
         return *this;
     }
-    // specialization for I
-    ComPtr &operator= (I *that_p) {
+
+    /* specialization for I */
+    ComPtr &operator=(I *that_p)
+    {
         Base::operator= (that_p);
         return *this;
@@ -247,5 +328,6 @@
 
     template <class OC>
-    ComPtr &operator= (const ComPtr <OC, RefOps> &oc) {
+    ComPtr &operator= (const ComPtr <OC, RefOps> &oc)
+    {
         return operator= ((OC *) oc);
     }
@@ -316,5 +398,6 @@
             PRUint32 serverID = 0;
             rc = ipcServ->ResolveClientName (serverName, &serverID);
-            if (SUCCEEDED (rc)) {
+            if (SUCCEEDED (rc))
+            {
                 nsCOMPtr <ipcIDConnectService> dconServ =
                     do_GetService (IPC_DCONNECTSERVICE_CONTRACTID, &rc);
@@ -346,5 +429,6 @@
     ComPtr () : Base() {}
     ComPtr (const ComPtr &that) : Base (that) {}
-    ComPtr &operator= (const ComPtr &that) {
+    ComPtr &operator= (const ComPtr &that)
+    {
         Base::operator= (that);
         return *this;
@@ -358,5 +442,6 @@
 
     template <class OI>
-    ComPtr &operator= (OI *that_p) {
+    ComPtr &operator= (OI *that_p)
+    {
         if (that_p)
             that_p->QueryInterface (COM_IIDOF (IUnknown), (void **) Base::asOutParam());
@@ -367,5 +452,6 @@
 
     template <class OC>
-    ComPtr &operator= (const ComPtr <OC, RefOps> &oc) {
+    ComPtr &operator= (const ComPtr <OC, RefOps> &oc)
+    {
         return operator= ((OC *) oc);
     }
@@ -390,9 +476,13 @@
     ComObjPtr (const ComObjPtr &that) : Base (that) {}
     ComObjPtr (C *that_p) : Base (that_p) {}
-    ComObjPtr &operator= (const ComObjPtr &that) {
+
+    ComObjPtr &operator= (const ComObjPtr &that)
+    {
         Base::operator= (that);
         return *this;
     }
-    ComObjPtr &operator= (C *that_p) {
+
+    ComObjPtr &operator= (C *that_p)
+    {
         Base::operator= (that_p);
         return *this;
@@ -412,16 +502,18 @@
      *  does otherwise.
      */
-    HRESULT createObject() {
+    HRESULT createObject()
+    {
         HRESULT rc;
 #if !defined (VBOX_WITH_XPCOM)
 #   ifdef VBOX_COM_OUTOFPROC_MODULE
         CComObjectNoLock <C> *obj = new CComObjectNoLock <C>();
-        if (obj) {
+        if (obj)
+        {
             obj->InternalFinalConstructAddRef();
             rc = obj->FinalConstruct();
             obj->InternalFinalConstructRelease();
-        } else {
+        }
+        else
             rc = E_OUTOFMEMORY;
-        }
 #   else
         CComObject <C> *obj = NULL;
@@ -430,9 +522,8 @@
 #else /* !defined (VBOX_WITH_XPCOM) */
         CComObject <C> *obj = new CComObject <C>();
-        if (obj) {
+        if (obj)
             rc = obj->FinalConstruct();
-        } else {
+        else
             rc = E_OUTOFMEMORY;
-        }
 #endif /* !defined (VBOX_WITH_XPCOM) */
         *this = obj;
Index: /trunk/src/VBox/Main/ConsoleImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/ConsoleImpl.cpp	(revision 6439)
+++ /trunk/src/VBox/Main/ConsoleImpl.cpp	(revision 6440)
@@ -883,5 +883,5 @@
         AssertRC (vrc);
 
-        vrc = SSMR3PutBool (pSSM, folder->writable());
+        vrc = SSMR3PutBool (pSSM, !!folder->writable());
         AssertRC (vrc);
     }
Index: /trunk/src/VBox/Main/MachineImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/MachineImpl.cpp	(revision 6439)
+++ /trunk/src/VBox/Main/MachineImpl.cpp	(revision 6440)
@@ -2959,6 +2959,7 @@
     if (SUCCEEDED (rc))
     {
-        /* memorize the direct session control */
+        /* memorize the direct session control and cache IUnknown for it */
         mData->mSession.mDirectControl = aControl;
+        mData->mSession.mDirectControlUnk = aControl;
         mData->mSession.mState = SessionState_SessionOpen;
         /* associate the SessionMachine with this Machine */
@@ -5663,5 +5664,5 @@
             folderNode.setValue <Bstr> ("name", folder->name());
             folderNode.setValue <Bstr> ("hostPath", folder->hostPath());
-            folderNode.setValue <bool> ("writable", folder->writable());
+            folderNode.setValue <bool> ("writable", !!folder->writable());
         }
     }
@@ -7305,5 +7306,8 @@
 
     if (aReason != Uninit::Normal)
+    {
         mData->mSession.mDirectControl.setNull();
+        mData->mSession.mDirectControlUnk.setNull();
+    }
     else
     {
@@ -7531,5 +7535,5 @@
     AutoMultiLock <2> alock (mParent->wlock(), this->wlock());
 
-    if (control.equalsTo (mData->mSession.mDirectControl))
+    if (control.equalsTo (mData->mSession.mDirectControlUnk))
     {
         ComAssertRet (aProgress, E_POINTER);
@@ -7545,4 +7549,5 @@
         /* set direct control to NULL to release the remote instance */
         mData->mSession.mDirectControl.setNull();
+        mData->mSession.mDirectControlUnk.setNull();
         LogFlowThisFunc (("Direct control is set to NULL\n"));
 
Index: /trunk/src/VBox/Main/include/MachineImpl.h
===================================================================
--- /trunk/src/VBox/Main/include/MachineImpl.h	(revision 6439)
+++ /trunk/src/VBox/Main/include/MachineImpl.h	(revision 6440)
@@ -103,4 +103,6 @@
             /** Control of the direct session opened by openSession() */
             ComPtr <IInternalSessionControl> mDirectControl;
+            /** Cached IUnknown of mDirectControl (IPC) for fast comparison */
+            ComPtr <IUnknown> mDirectControlUnk;
 
             typedef std::list <ComPtr <IInternalSessionControl> > RemoteControlList;
