Index: /trunk/Config.kmk
===================================================================
--- /trunk/Config.kmk	(revision 60764)
+++ /trunk/Config.kmk	(revision 60765)
@@ -8,5 +8,5 @@
 
 #
-# Copyright (C) 2006-2015 Oracle Corporation
+# Copyright (C) 2006-2016 Oracle Corporation
 #
 # This file is part of VirtualBox Open Source Edition (OSE), as
@@ -4862,11 +4862,9 @@
 	$(PATH_TOOL_$(TEMPLATE_VBOXMAINEXE_TOOL.win.x86)_LIB)/msvcrt$(VBOX_VCC_CRT_TYPE).lib \
 	$(PATH_TOOL_$(TEMPLATE_VBOXMAINEXE_TOOL.win.x86)_LIB)/msvcprt$(VBOX_VCC_CRT_TYPE).lib \
-	$(PATH_TOOL_$(TEMPLATE_VBOXMAINEXE_TOOL.win.x86)_LIB)/oldnames.lib \
-	$(PATH_TOOL_$(TEMPLATE_VBOXMAINEXE_TOOL.win.x86)_ATLMFC_LIB)/atls$(VBOX_VCC_CRT_TYPE).lib
+	$(PATH_TOOL_$(TEMPLATE_VBOXMAINEXE_TOOL.win.x86)_LIB)/oldnames.lib
  TEMPLATE_VBOXMAINEXE_LIBS.amd64         = \
 	$(PATH_TOOL_$(TEMPLATE_VBOXMAINEXE_TOOL.win.amd64)_LIB)/msvcrt$(VBOX_VCC_CRT_TYPE).lib \
 	$(PATH_TOOL_$(TEMPLATE_VBOXMAINEXE_TOOL.win.amd64)_LIB)/msvcprt$(VBOX_VCC_CRT_TYPE).lib \
-	$(PATH_TOOL_$(TEMPLATE_VBOXMAINEXE_TOOL.win.amd64)_LIB)/oldnames.lib \
-	$(PATH_TOOL_$(TEMPLATE_VBOXMAINEXE_TOOL.win.amd64)_ATLMFC_LIB)/atls$(VBOX_VCC_CRT_TYPE).lib
+	$(PATH_TOOL_$(TEMPLATE_VBOXMAINEXE_TOOL.win.amd64)_LIB)/oldnames.lib
  TEMPLATE_VBOXMAINEXE_POST_CMDS          = $(VBOX_SIGN_IMAGE_CMDS)
 
@@ -5358,6 +5356,4 @@
   TEMPLATE_VBOXQTGUIEXE_CXXFLAGS.debug = -RTCsu
   TEMPLATE_VBOXQTGUIEXE_CXXFLAGS.dbgopt = $(NO_SUCH_VARIABLE)
-  TEMPLATE_VBOXQTGUIEXE_INCS += \
-	$(PATH_TOOL_$(VBOX_VCC_TOOL)_ATLMFC_INC)
   TEMPLATE_VBOXQTGUIEXE_LDFLAGS = -Ignore:4197 \
 	/NOD /INCREMENTAL:NO /MAPINFO:EXPORTS /LargeAddressAware /DynamicBase /NxCompat /Release /Debug /Opt:Ref /Opt:Icf \
@@ -5375,6 +5371,5 @@
 	$(PATH_TOOL_$(VBOX_VCC_TOOL)_LIB)/msvcrt.lib \
 	$(PATH_TOOL_$(VBOX_VCC_TOOL)_LIB)/oldnames.lib \
-	$(PATH_TOOL_$(VBOX_VCC_TOOL)_LIB)/delayimp.lib \
-	$(PATH_TOOL_$(VBOX_VCC_TOOL)_ATLMFC_LIB)/atls.lib
+	$(PATH_TOOL_$(VBOX_VCC_TOOL)_LIB)/delayimp.lib
   TEMPLATE_VBOXQTGUIEXE_POST_CMDS = $(VBOX_SIGN_IMAGE_CMDS)
 
Index: /trunk/include/VBox/com/defs.h
===================================================================
--- /trunk/include/VBox/com/defs.h	(revision 60764)
+++ /trunk/include/VBox/com/defs.h	(revision 60765)
@@ -4,5 +4,5 @@
 
 /*
- * Copyright (C) 2006-2015 Oracle Corporation
+ * Copyright (C) 2006-2016 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -82,9 +82,8 @@
 #include <objbase.h>
 #ifndef VBOX_COM_NO_ATL
-# define _ATL_FREE_THREADED
-
-# include <atlbase.h>
-#include <atlcom.h>
-#endif
+
+/* Do not use actual ATL, just provide a superficial lookalike ourselves. */
+# include <VBox/com/microatl.h>
+#endif /* VBOX_COM_NO_ATL */
 
 #define NS_DECL_ISUPPORTS
@@ -126,5 +125,5 @@
 #define IN_GUID GUID
 /** Output GUID argument of interface method declaration. */
-#define OUT_GUID GUID*
+#define OUT_GUID GUID *
 
 /** Makes the name of the getter interface function (n must be capitalized). */
@@ -250,5 +249,5 @@
  *  @param I    interface class
  */
-#define COM_IIDOF(I) _ATL_IIDOF(I)
+#define COM_IIDOF(I) __uuidof(I)
 
 /**
@@ -280,4 +279,9 @@
 
 #include <nsID.h>
+
+#ifndef VBOX_COM_NO_ATL
+
+namespace ATL
+{
 
 #define ATL_NO_VTABLE
@@ -292,4 +296,33 @@
 #define END_COM_MAP() NS_DECL_ISUPPORTS
 #define COM_INTERFACE_ENTRY_AGGREGATE(a,b)
+
+/* A few very simple ATL emulator classes to provide
+ * FinalConstruct()/FinalRelease() functionality with XPCOM. */
+
+class CComMultiThreadModel
+{
+};
+
+template <class DummyThreadModel> class CComObjectRootEx
+{
+public:
+    HRESULT FinalConstruct()
+    {
+        return S_OK;
+    }
+    void FinalRelease()
+    {
+    }
+};
+
+template <class DummyThreadModel> class CComObject
+{
+public:
+    virtual ~CComObject() { this->FinalRelease(); }
+};
+
+} /* namespace ATL */
+
+#endif /* !VBOX_COM_NO_ATL */
 
 #define HRESULT     nsresult
@@ -394,35 +427,8 @@
 #define COM_STRUCT_OR_CLASS(I) class I
 
-namespace ATL
-{
-
-/* A few very simple ATL emulator classes to provide
- * FinalConstruct()/FinalRelease() functionality on Linux. */
-
-class CComMultiThreadModel
-{
-};
-
-template <class Base> class CComObjectRootEx : public Base
-{
-public:
-    HRESULT FinalConstruct() { return S_OK; }
-    void FinalRelease() {}
-};
-
-template <class Base> class CComObject : public Base
-{
-public:
-    virtual ~CComObject() { this->FinalRelease(); }
-};
-
-} /* namespace ATL */
-
-using namespace ATL;
-
 /* helper functions */
 extern "C"
 {
-BSTR SysAllocString(const OLECHAR* sz);
+BSTR SysAllocString(const OLECHAR *sz);
 BSTR SysAllocStringByteLen(char *psz, unsigned int len);
 BSTR SysAllocStringLen(const OLECHAR *pch, unsigned int cch);
@@ -434,4 +440,6 @@
 }
 
+#ifndef VBOX_COM_NO_ATL
+
 /**
  *  'Constructor' for the component class.
@@ -455,5 +463,5 @@
     }                                                                         \
                                                                               \
-    CComObject <_InstanceClass> *inst = new CComObject <_InstanceClass>();    \
+    CComObject<_InstanceClass> *inst = new CComObject<_InstanceClass>();      \
     if (NULL == inst) {                                                       \
         rv = NS_ERROR_OUT_OF_MEMORY;                                          \
@@ -486,5 +494,5 @@
     nsresult rv;                                                              \
                                                                               \
-    _InstanceClass * inst = NULL;       /* initialized to shut up gcc */      \
+    _InstanceClass *inst = NULL;       /* initialized to shut up gcc */       \
                                                                               \
     *aResult = NULL;                                                          \
@@ -510,4 +518,6 @@
     return rv;                                                                \
 }
+
+#endif /* !VBOX_COM_NO_ATL */
 
 #endif /* !defined(VBOX_WITH_XPCOM) */
@@ -529,16 +539,18 @@
 {
 
+#ifndef VBOX_COM_NO_ATL
+
 // use this macro to implement scriptable interfaces
 #ifdef RT_OS_WINDOWS
 #define VBOX_SCRIPTABLE_IMPL(iface)                                          \
-    public IDispatchImpl<iface, &IID_##iface, &LIBID_VirtualBox,             \
-                         kTypeLibraryMajorVersion, kTypeLibraryMinorVersion>
+    public ATL::IDispatchImpl<iface, &IID_##iface, &LIBID_VirtualBox,        \
+                              kTypeLibraryMajorVersion, kTypeLibraryMinorVersion>
 
 #define VBOX_SCRIPTABLE_DISPATCH_IMPL(iface)                                 \
-    STDMETHOD(QueryInterface)(REFIID riid , void **ppObj)                    \
+    STDMETHOD(QueryInterface)(REFIID riid, void **ppObj)                     \
     {                                                                        \
         if (riid == IID_##iface)                                             \
         {                                                                    \
-            *ppObj = (iface*)this;                                           \
+            *ppObj = (iface *)this;                                          \
             AddRef();                                                        \
             return S_OK;                                                     \
@@ -546,5 +558,5 @@
         if (riid == IID_IUnknown)                                            \
         {                                                                    \
-            *ppObj = (IUnknown*)this;                                        \
+            *ppObj = (IUnknown *)this;                                       \
             AddRef();                                                        \
             return S_OK;                                                     \
@@ -552,5 +564,5 @@
         if (riid == IID_IDispatch)                                           \
         {                                                                    \
-            *ppObj = (IDispatch*)this;                                       \
+            *ppObj = (IDispatch *)this;                                      \
             AddRef();                                                        \
             return S_OK;                                                     \
@@ -559,18 +571,11 @@
         return E_NOINTERFACE;                                                \
     }
-
-
-#define VBOX_DEFAULT_INTERFACE_ENTRIES(iface)                                \
-        COM_INTERFACE_ENTRY(ISupportErrorInfo)                               \
-        COM_INTERFACE_ENTRY(iface)                                           \
-        COM_INTERFACE_ENTRY2(IDispatch,iface)                                \
-        COM_INTERFACE_ENTRY_AGGREGATE(IID_IMarshal, m_pUnkMarshaler.p)
 #else
 #define VBOX_SCRIPTABLE_IMPL(iface)                     \
     public iface
 #define VBOX_SCRIPTABLE_DISPATCH_IMPL(iface)
-#define VBOX_DEFAULT_INTERFACE_ENTRIES(iface)
 #endif
 
+#endif /* !VBOX_COM_NO_ATL */
 
 } /* namespace com */
Index: /trunk/include/VBox/com/microatl.h
===================================================================
--- /trunk/include/VBox/com/microatl.h	(revision 60765)
+++ /trunk/include/VBox/com/microatl.h	(revision 60765)
@@ -0,0 +1,1380 @@
+/** @file
+ * ATL lookalike, just the tiny subset we actually need.
+ */
+
+/*
+ * Copyright (C) 2016 Oracle Corporation
+ *
+ * This file is part of VirtualBox Open Source Edition (OSE), as
+ * available from http://www.virtualbox.org. This file is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU
+ * General Public License (GPL) as published by the Free Software
+ * Foundation, in version 2 as it comes in the "COPYING" file of the
+ * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+ * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+ *
+ * The contents of this file may alternatively be used under the terms
+ * of the Common Development and Distribution License Version 1.0
+ * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
+ * VirtualBox OSE distribution, in which case the provisions of the
+ * CDDL are applicable instead of those of the GPL.
+ *
+ * You may elect to license modified versions of this file under the
+ * terms and conditions of either the GPL or the CDDL or both.
+ */
+
+#ifndef ___VBox_com_microatl_h
+#define ___VBox_com_microatl_h
+
+#include <iprt/assert.h>
+#include <iprt/critsect.h>
+#include <iprt/err.h>
+
+
+namespace ATL
+{
+
+#define ATL_NO_VTABLE __declspec(novtable)
+
+class CAtlModule;
+__declspec(selectany) CAtlModule *_pAtlModule = NULL;
+
+class CComModule;
+__declspec(selectany) CComModule *_pModule = NULL;
+
+typedef HRESULT (WINAPI FNCREATEINSTANCE)(void *pv, REFIID riid, void **ppv);
+typedef FNCREATEINSTANCE *PFNCREATEINSTANCE;
+typedef HRESULT (WINAPI FNINTERFACEMAPHELPER)(void *pv, REFIID riid, void **ppv, DWORD_PTR dw);
+typedef FNINTERFACEMAPHELPER *PFNINTERFACEMAPHELPER;
+typedef void (__stdcall FNATLTERMFUNC)(void *pv);
+typedef FNATLTERMFUNC *PFNATLTERMFUNC;
+
+struct _ATL_TERMFUNC_ELEM
+{
+    PFNATLTERMFUNC pfn;
+    void *pv;
+    _ATL_TERMFUNC_ELEM *pNext;
+};
+
+struct _ATL_INTMAP_ENTRY
+{
+    const IID *piid;    // interface ID
+    DWORD_PTR dw;
+    PFNINTERFACEMAPHELPER pFunc; // NULL: end of array, 1: offset based map entry, other: function pointer
+};
+
+#define COM_SIMPLEMAPENTRY ((ATL::PFNINTERFACEMAPHELPER)1)
+
+#define DECLARE_CLASSFACTORY_EX(c) typedef ATL::CComCreator<ATL::CComObjectNoLock<c> > _ClassFactoryCreatorClass;
+#define DECLARE_CLASSFACTORY() DECLARE_CLASSFACTORY_EX(ATL::CComClassFactory)
+#define DECLARE_CLASSFACTORY_SINGLETON(o) DECLARE_CLASSFACTORY_EX(ATL::CComClassFactorySingleton<o>)
+#define DECLARE_AGGREGATABLE(c) \
+public: \
+    typedef ATL::CComCreator2<ATL::CComCreator<ATL::CComObject<c> >, ATL::CComCreator<ATL::CComAggObject<c> > > _CreatorClass;
+#define DECLARE_NOT_AGGREGATABLE(c) \
+public: \
+    typedef ATL::CComCreator2<ATL::CComCreator<ATL::CComObject<c> >, ATL::CComFailCreator<CLASS_E_NOAGGREGATION> > _CreatorClass;
+
+#define DECLARE_PROTECT_FINAL_CONSTRUCT() \
+    void InternalFinalConstructAddRef() \
+    { \
+        InternalAddRef(); \
+    } \
+    void InternalFinalConstructRelease() \
+    { \
+        InternalRelease(); \
+    }
+
+#define BEGIN_COM_MAP(c) \
+public: \
+    typedef c _ComClass; \
+    HRESULT _InternalQueryInterface(REFIID iid, void **ppvObj) throw() \
+    { \
+        return InternalQueryInterface(this, _GetEntries(), iid, ppvObj); \
+    } \
+    const static ATL::_ATL_INTMAP_ENTRY *WINAPI _GetEntries() throw() \
+    { \
+        static const ATL::_ATL_INTMAP_ENTRY _aInterfaces[] = \
+        {
+
+#define COM_INTERFACE_ENTRY(c) \
+    { &__uuidof(c), (DWORD_PTR)(static_cast<c *>((_ComClass *)8))-8, COM_SIMPLEMAPENTRY },
+
+#define COM_INTERFACE_ENTRY2(c, c2) \
+    { &__uuidof(c), (DWORD_PTR)(static_cast<c *>(static_cast<c2 *>((_ComClass *)8)))-8, COM_SIMPLEMAPENTRY },
+
+#define COM_INTERFACE_ENTRY_AGGREGATE(iid, pUnk) \
+    { &iid, (DWORD_PTR)RT_OFFSETOF(_ComClass, pUnk), _Delegate},
+
+#define END_COM_MAP() \
+            { NULL, 0, NULL} \
+        }; \
+        return _aInterfaces; \
+    } \
+    virtual ULONG STDMETHODCALLTYPE AddRef(void) throw() = 0; \
+    virtual ULONG STDMETHODCALLTYPE Release(void) throw() = 0; \
+    STDMETHOD(QueryInterface)(REFIID, void **) throw() = 0;
+
+struct _ATL_OBJMAP_ENTRY
+{
+    const CLSID *pclsid;
+    PFNCREATEINSTANCE pfnGetClassObject;
+    PFNCREATEINSTANCE pfnCreateInstance;
+    IUnknown *pCF;
+    DWORD dwRegister;
+};
+
+#define BEGIN_OBJECT_MAP(o) static ATL::_ATL_OBJMAP_ENTRY o[] = {
+#define END_OBJECT_MAP()   {NULL, NULL, NULL, NULL, 0}};
+#define OBJECT_ENTRY(clsid, c) {&clsid, c::_ClassFactoryCreatorClass::CreateInstance, c::_CreatorClass::CreateInstance, NULL, 0 },
+
+
+class CComCriticalSection
+{
+public:
+    CComCriticalSection() throw()
+    {
+        memset(&m_CritSect, 0, sizeof(m_CritSect));
+    }
+    ~CComCriticalSection()
+    {
+    }
+    HRESULT Lock() throw()
+    {
+        RTCritSectEnter(&m_CritSect);
+        return S_OK;
+    }
+    HRESULT Unlock() throw()
+    {
+        RTCritSectLeave(&m_CritSect);
+        return S_OK;
+    }
+    HRESULT Init() throw()
+    {
+        HRESULT hrc = S_OK;
+        if (RT_FAILURE(RTCritSectInit(&m_CritSect)))
+            hrc = E_FAIL;
+        return hrc;
+    }
+
+    HRESULT Term() throw()
+    {
+        RTCritSectDelete(&m_CritSect);
+        return S_OK;
+    }
+
+    RTCRITSECT m_CritSect;
+};
+
+template <class TLock> class CComCritSectLock
+{
+public:
+    CComCritSectLock(CComCriticalSection &cs, bool fInitialLock = true) :
+        m_cs(cs),
+        m_fLocked(false)
+    {
+        if (fInitialLock)
+        {
+            HRESULT hrc = Lock();
+            if (FAILED(hrc))
+            throw hrc;
+        }
+    }
+
+    ~CComCritSectLock() throw()
+    {
+        if (m_fLocked)
+            Unlock();
+    }
+
+    HRESULT Lock()
+    {
+        Assert(!m_fLocked);
+        HRESULT hrc = m_cs.Lock();
+        if (FAILED(hrc))
+            return hrc;
+        m_fLocked = true;
+        return S_OK;
+    }
+
+    void Unlock() throw()
+    {
+        Assert(m_fLocked);
+        m_cs.Unlock();
+        m_fLocked = false;
+    }
+
+
+private:
+    TLock &m_cs;
+    bool m_fLocked;
+
+    CComCritSectLock(const CComCritSectLock&) throw(); // Do not call.
+    CComCritSectLock &operator=(const CComCritSectLock &) throw(); // Do not call.
+};
+
+class CComFakeCriticalSection
+{
+public:
+    HRESULT Lock() throw()
+    {
+        return S_OK;
+    }
+    HRESULT Unlock() throw()
+    {
+        return S_OK;
+    }
+    HRESULT Init() throw()
+    {
+        return S_OK;
+    }
+    HRESULT Term() throw()
+    {
+        return S_OK;
+    }
+};
+
+class CComAutoCriticalSection : public CComCriticalSection
+{
+public:
+    CComAutoCriticalSection()
+    {
+        HRESULT hrc = CComCriticalSection::Init();
+        if (FAILED(hrc))
+            throw hrc;
+    }
+    ~CComAutoCriticalSection() throw()
+    {
+        CComCriticalSection::Term();
+    }
+private :
+    HRESULT Init() throw(); // Do not call.
+    HRESULT Term() throw(); // Do not call.
+};
+
+class CComAutoDeleteCriticalSection : public CComCriticalSection
+{
+public:
+    CComAutoDeleteCriticalSection(): m_fInit(false)
+    {
+    }
+
+    ~CComAutoDeleteCriticalSection() throw()
+    {
+        if (!m_fInit)
+            return;
+        m_fInit = false;
+        CComCriticalSection::Term();
+    }
+
+    HRESULT Init() throw()
+    {
+        Assert(!m_fInit);
+        HRESULT hrc = CComCriticalSection::Init();
+        if (SUCCEEDED(hrc))
+            m_fInit = true;
+        return hrc;
+    }
+
+    HRESULT Lock()
+    {
+        Assert(m_fInit);
+        return CComCriticalSection::Lock();
+    }
+
+    HRESULT Unlock()
+    {
+        Assert(m_fInit);
+        return CComCriticalSection::Unlock();
+    }
+
+private:
+    HRESULT Term() throw();
+    bool m_fInit;
+};
+
+
+class CComMultiThreadModelNoCS
+{
+public:
+    static ULONG WINAPI Increment(LONG *pL) throw()
+    {
+        return InterlockedIncrement(pL);
+    }
+    static ULONG WINAPI Decrement(LONG *pL) throw()
+    {
+        return InterlockedDecrement(pL);
+    }
+    typedef CComFakeCriticalSection AutoCriticalSection;
+    typedef CComFakeCriticalSection AutoDeleteCriticalSection;
+    typedef CComMultiThreadModelNoCS ThreadModelNoCS;
+};
+
+class CComMultiThreadModel
+{
+public:
+    static ULONG WINAPI Increment(LONG *pL) throw()
+    {
+        return InterlockedIncrement(pL);
+    }
+    static ULONG WINAPI Decrement(LONG *pL) throw()
+    {
+        return InterlockedDecrement(pL);
+    }
+    typedef CComAutoCriticalSection AutoCriticalSection;
+    typedef CComAutoDeleteCriticalSection AutoDeleteCriticalSection;
+    typedef CComMultiThreadModelNoCS ThreadModelNoCS;
+};
+
+class ATL_NO_VTABLE CAtlModule
+{
+public:
+    static GUID m_LibID;
+    CComCriticalSection m_csStaticDataInitAndTypeInfo;
+
+    CAtlModule() throw()
+    {
+        // One instance only per linking namespace!
+        AssertMsg(!_pAtlModule, ("CAtlModule: trying to create more than one instance per linking namespace\n"));
+
+        fInit = false;
+
+        m_cLock = 0;
+	m_pTermFuncs = NULL;
+        _pAtlModule = this;
+
+        if (FAILED(m_csStaticDataInitAndTypeInfo.Init()))
+        {
+            AssertMsgFailed(("CAtlModule: failed to init critsect\n"));
+            return;
+        }
+        fInit = true;
+    }
+
+    void Term() throw()
+    {
+        if (!fInit)
+	    return;
+
+	// Call all term functions.
+	if (m_pTermFuncs)
+	{
+	    _ATL_TERMFUNC_ELEM *p = m_pTermFuncs;
+	    _ATL_TERMFUNC_ELEM *pNext;
+	    while (p)
+	    {
+	        p->pfn(p->pv);
+		pNext = p->pNext;
+		delete p;
+		p = pNext;
+	    }
+	    m_pTermFuncs = NULL;
+	}
+        m_csStaticDataInitAndTypeInfo.Term();
+        fInit = false;
+    }
+
+    virtual ~CAtlModule() throw()
+    {
+        Term();
+    }
+
+    virtual LONG Lock() throw()
+    {
+        return CComMultiThreadModel::Increment(&m_cLock);
+    }
+
+    virtual LONG Unlock() throw()
+    {
+        return CComMultiThreadModel::Decrement(&m_cLock);
+    }
+
+    virtual LONG GetLockCount() throw()
+    {
+        return m_cLock;
+    }
+
+    HRESULT AddTermFunc(PFNATLTERMFUNC pfn, void *pv)
+    {
+	HRESULT hrc = S_OK;
+	_ATL_TERMFUNC_ELEM *pNew = NULL;
+        try
+        {
+            pNew = new _ATL_TERMFUNC_ELEM;
+        }
+        catch (...)
+        {
+        }
+	if (!pNew)
+	    return E_OUTOFMEMORY;
+	pNew->pfn = pfn;
+	pNew->pv = pv;
+        CComCritSectLock<CComCriticalSection> lock(m_csStaticDataInitAndTypeInfo, false);
+        hrc = lock.Lock();
+	if (SUCCEEDED(hrc))
+	{
+	    pNew->pNext = m_pTermFuncs;
+	    m_pTermFuncs = pNew;
+	}
+	else
+	{
+	    delete pNew;
+            AssertMsgFailed(("CComModule::AddTermFunc: failed to lock critsect\n"));
+        }
+	return hrc;
+    }
+
+protected:
+    bool fInit;
+    LONG m_cLock;
+    _ATL_TERMFUNC_ELEM *m_pTermFuncs;
+};
+
+__declspec(selectany) GUID CAtlModule::m_LibID = {0x0, 0x0, 0x0, {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0} };
+
+struct _ATL_COM_MODULE
+{
+    HINSTANCE m_hInstTypeLib;
+    CComCriticalSection m_csObjMap;
+};
+
+#ifndef _delayimp_h
+extern "C" IMAGE_DOS_HEADER __ImageBase;
+#endif
+
+class CAtlComModule : public _ATL_COM_MODULE
+{
+public:
+    static bool m_fInitFailed;
+    CAtlComModule() throw()
+    {
+        m_hInstTypeLib = reinterpret_cast<HINSTANCE>(&__ImageBase);
+
+        if (FAILED(m_csObjMap.Init()))
+        {
+            AssertMsgFailed(("CAtlComModule: critsect init failed\n"));
+            m_fInitFailed = true;
+            return;
+        }
+    }
+
+    ~CAtlComModule()
+    {
+        Term();
+    }
+
+    void Term()
+    {
+        m_csObjMap.Term();
+    }
+};
+
+__declspec(selectany) bool CAtlComModule::m_fInitFailed = false;
+__declspec(selectany) CAtlComModule _AtlComModule;
+
+template <class T> class ATL_NO_VTABLE CAtlModuleT : public CAtlModule
+{
+public:
+    CAtlModuleT() throw()
+    {
+        T::InitLibId();
+    }
+
+    static void InitLibId() throw()
+    {
+    }
+};
+
+class CComModule : public CAtlModuleT<CComModule>
+{
+public:
+    CComModule()
+    {
+        // One instance only per linking namespace!
+        AssertMsg(!_pModule, ("CComModule: trying to create more than one instance per linking namespace\n"));
+        _pModule = this;
+        m_pObjMap = NULL;
+    }
+
+    ~CComModule()
+    {
+    }
+
+    _ATL_OBJMAP_ENTRY *m_pObjMap;
+    HRESULT Init(_ATL_OBJMAP_ENTRY *p, HINSTANCE h, const GUID *pLibID = NULL) throw()
+    {
+        if (pLibID)
+            m_LibID = *pLibID;
+
+        // Go over the object map to do some sanity checking, making things
+        // crash early if something is seriously busted.
+        _ATL_OBJMAP_ENTRY *pEntry;
+        if (p != (_ATL_OBJMAP_ENTRY *)-1)
+        {
+            m_pObjMap = p;
+            if (m_pObjMap)
+            {
+                pEntry = m_pObjMap;
+                while (pEntry->pclsid)
+                    pEntry++;
+            }
+        }
+        return S_OK;
+    }
+
+    void Term() throw()
+    {
+        _ATL_OBJMAP_ENTRY *pEntry;
+        if (m_pObjMap)
+        {
+            pEntry = m_pObjMap;
+            while (pEntry->pclsid)
+            {
+                if (pEntry->pCF)
+                    pEntry->pCF->Release();
+                pEntry->pCF = NULL;
+                pEntry++;
+            }
+        }
+
+        CAtlModuleT<CComModule>::Term();
+    }
+
+    HRESULT GetClassObject(REFCLSID rclsid, REFIID riid, void **ppv) throw()
+    {
+        *ppv = NULL;
+        HRESULT hrc = S_OK;
+
+        if (m_pObjMap)
+        {
+            const _ATL_OBJMAP_ENTRY *pEntry = m_pObjMap;
+
+            while (pEntry->pclsid)
+            {
+                if ((pEntry->pfnGetClassObject) && rclsid == *pEntry->pclsid)
+                {
+                    if (!pEntry->pCF)
+                    {
+                        CComCritSectLock<CComCriticalSection> lock(_AtlComModule.m_csObjMap, false);
+                        hrc = lock.Lock();
+                        if (FAILED(hrc))
+                        {
+                            AssertMsgFailed(("CComModule::GetClassObject: failed to lock critsect\n"));
+                            break;
+                        }
+
+                        if (!pEntry->pCF)
+                        {
+                            hrc = pEntry->pfnGetClassObject(pEntry->pfnCreateInstance, __uuidof(IUnknown), (void **)&pEntry->pCF);
+                        }
+                    }
+
+                    if (pEntry->pCF)
+                    {
+                        hrc = pEntry->pCF->QueryInterface(riid, ppv);
+                    }
+                    break;
+                }
+                pEntry++;
+            }
+        }
+
+        return hrc;
+    }
+
+    // For EXE only: register all class factories with COM.
+    HRESULT RegisterClassObjects(DWORD dwClsContext, DWORD dwFlags) throw()
+    {
+        HRESULT hrc = S_OK;
+        _ATL_OBJMAP_ENTRY *pEntry;
+        if (m_pObjMap)
+        {
+            pEntry = m_pObjMap;
+            while (pEntry->pclsid && SUCCEEDED(hrc))
+            {
+                if (pEntry->pfnGetClassObject)
+                {
+                    IUnknown *p;
+                    hrc = pEntry->pfnGetClassObject(pEntry->pfnCreateInstance, __uuidof(IUnknown), (void **)&p);
+                    if (SUCCEEDED(hrc))
+                        hrc = CoRegisterClassObject(*(pEntry->pclsid), p, dwClsContext, dwFlags, &pEntry->dwRegister);
+                    if (p)
+                        p->Release();
+                }
+                pEntry++;
+            }
+        }
+        return hrc;
+    }
+    // For EXE only: revoke all class factories with COM.
+    HRESULT RevokeClassObjects() throw()
+    {
+        HRESULT hrc = S_OK;
+        _ATL_OBJMAP_ENTRY *pEntry;
+        if (m_pObjMap != NULL)
+        {
+            pEntry = m_pObjMap;
+            while (pEntry->pclsid && SUCCEEDED(hrc))
+            {
+                if (pEntry->dwRegister)
+                    hrc = CoRevokeClassObject(pEntry->dwRegister);
+                pEntry++;
+            }
+        }
+        return hrc;
+    }
+};
+
+
+template <class T> class CComCreator
+{
+public:
+    static HRESULT WINAPI CreateInstance(void *pv, REFIID riid, void **ppv)
+    {
+        AssertReturn(ppv, E_POINTER);
+        *ppv = NULL;
+        HRESULT hrc = E_OUTOFMEMORY;
+        T *p = NULL;
+        try
+        {
+            p = new T(pv);
+        }
+        catch (...)
+        {
+        }
+        if (p)
+        {
+            p->SetVoid(pv);
+            p->InternalFinalConstructAddRef();
+            hrc = p->_AtlInitialConstruct();
+            if (SUCCEEDED(hrc))
+                hrc = p->FinalConstruct();
+            p->InternalFinalConstructRelease();
+            if (SUCCEEDED(hrc))
+                hrc = p->QueryInterface(riid, ppv);
+            if (FAILED(hrc))
+                delete p;
+        }
+        return hrc;
+    }
+};
+
+template <HRESULT hrc> class CComFailCreator
+{
+public:
+    static HRESULT WINAPI CreateInstance(void *, REFIID, void **ppv)
+    {
+        AssertReturn(ppv, E_POINTER);
+        *ppv = NULL;
+
+        return hrc;
+    }
+};
+
+template <class T1, class T2> class CComCreator2
+{
+public:
+    static HRESULT WINAPI CreateInstance(void *pv, REFIID riid, void **ppv)
+    {
+        AssertReturn(ppv, E_POINTER);
+
+        return !pv ? T1::CreateInstance(NULL, riid, ppv) : T2::CreateInstance(pv, riid, ppv);
+    }
+};
+
+template <class Base> class CComObjectCached : public Base
+{
+public:
+    CComObjectCached(void * = NULL)
+    {
+    }
+    virtual ~CComObjectCached()
+    {
+        // Catch refcount screwups by setting refcount to -(LONG_MAX/2).
+        m_iRef = -(LONG_MAX/2);
+        FinalRelease();
+#ifdef _ATL_DEBUG_INTERFACES
+        _AtlDebugInterfacesModule.DeleteNonAddRefThunk(_GetRawUnknown());
+#endif
+    }
+    STDMETHOD_(ULONG, AddRef)() throw()
+    {
+        // If you get errors about undefined InternalAddRef then Base does not
+        // derive from CComObjectRootEx.
+        ULONG l = InternalAddRef();
+        if (l == 2)
+        {
+            AssertMsg(_pAtlModule, ("ATL: referring to ATL module without having one declared in this linking namespace\n"));
+            _pAtlModule->Lock();
+        }
+        return l;
+    }
+    STDMETHOD_(ULONG, Release)() throw()
+    {
+        // If you get errors about undefined InternalRelease then Base does not
+        // derive from CComObjectRootEx.
+        ULONG l = InternalRelease();
+        if (l == 0)
+            delete this;
+        else if (l == 1)
+        {
+            AssertMsg(_pAtlModule, ("ATL: referring to ATL module without having one declared in this linking namespace\n"));
+            _pAtlModule->Unlock();
+        }
+        return l;
+    }
+    STDMETHOD(QueryInterface)(REFIID iid, void **ppvObj) throw()
+    {
+        // If you get errors about undefined _InternalQueryInterface then
+        // double check BEGIN_COM_MAP in the class definition.
+        return _InternalQueryInterface(iid, ppvObj);
+    }
+    static HRESULT WINAPI CreateInstance(CComObjectCached<Base> **pp) throw()
+    {
+        AssertReturn(pp, E_POINTER);
+        *pp = NULL;
+
+        HRESULT hrc = E_OUTOFMEMORY;
+        CComObjectCached<Base> *p = NULL;
+        try
+        {
+            p = new CComObjectCached<Base>();
+        }
+        catch (...)
+        {
+        }
+        if (p)
+        {
+            p->SetVoid(NULL);
+            p->InternalFinalConstructAddRef();
+            hrc = p->_AtlInitialConstruct();
+            if (SUCCEEDED(hrc))
+                hrc = p->FinalConstruct();
+            p->InternalFinalConstructRelease();
+            if (FAILED(hrc))
+                delete p;
+            else
+                *pp = p;
+        }
+        return hrc;
+    }
+};
+
+template <class Base> class CComObjectNoLock : public Base
+{
+public:
+    CComObjectNoLock(void * = NULL)
+    {
+    }
+    virtual ~CComObjectNoLock()
+    {
+        // Catch refcount screwups by setting refcount to -(LONG_MAX/2).
+        m_iRef = -(LONG_MAX/2);
+        FinalRelease();
+    }
+    STDMETHOD_(ULONG, AddRef)() throw()
+    {
+        // If you get errors about undefined InternalAddRef then Base does not
+        // derive from CComObjectRootEx.
+        return InternalAddRef();
+    }
+    STDMETHOD_(ULONG, Release)() throw()
+    {
+        // If you get errors about undefined InternalRelease then Base does not
+        // derive from CComObjectRootEx.
+        ULONG l = InternalRelease();
+        if (l == 0)
+            delete this;
+        return l;
+    }
+    STDMETHOD(QueryInterface)(REFIID iid, void **ppvObj) throw()
+    {
+        // If you get errors about undefined _InternalQueryInterface then
+        // double check BEGIN_COM_MAP in the class definition.
+        return _InternalQueryInterface(iid, ppvObj);
+    }
+};
+
+class CComTypeInfoHolder
+{
+    /** @todo implement type info caching, making stuff more efficient - would we benefit? */
+public:
+    const GUID *m_pGUID;
+    const GUID *m_pLibID;
+    WORD m_iMajor;
+    WORD m_iMinor;
+    ITypeInfo *m_pTInfo;
+
+    HRESULT GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
+    {
+        if (iTInfo != 0)
+            return DISP_E_BADINDEX;
+        return GetTI(lcid, ppTInfo);
+    }
+    HRESULT GetIDsOfNames(REFIID riid, LPOLESTR *pwszNames, UINT cNames, LCID lcid, DISPID *pDispID)
+    {
+        HRESULT hrc = FetchTI(lcid);
+        if (m_pTInfo)
+            hrc = m_pTInfo->GetIDsOfNames(pwszNames, cNames, pDispID);
+        return hrc;
+    }
+    HRESULT Invoke(IDispatch *p, DISPID DispID, REFIID riid, LCID lcid, WORD iFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
+    {
+        HRESULT hrc = FetchTI(lcid);
+        if (m_pTInfo)
+            hrc = m_pTInfo->Invoke(p, DispID, iFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
+        return hrc;
+    }
+private:
+    static void __stdcall Cleanup(void *pv)
+    {
+        AssertReturnVoid(pv);
+        CComTypeInfoHolder *p = (CComTypeInfoHolder *)pv;
+        if (p->m_pTInfo != NULL)
+            p->m_pTInfo->Release();
+        p->m_pTInfo = NULL;
+    }
+
+    HRESULT GetTI(LCID lcid)
+    {
+        AssertMsg(_pAtlModule, ("ATL: referring to ATL module without having one declared in this linking namespace\n"));
+        Assert(m_pLibID && m_pGUID);
+        if (m_pTInfo)
+            return S_OK;
+        CComCritSectLock<CComCriticalSection> lock(_pAtlModule->m_csStaticDataInitAndTypeInfo, false);
+        HRESULT hrc = lock.Lock();
+        ITypeLib *pTypeLib = NULL;
+        Assert(*m_pLibID != GUID_NULL);
+        hrc = LoadRegTypeLib(*m_pLibID, m_iMajor, m_iMinor, lcid, &pTypeLib);
+        if (SUCCEEDED(hrc))
+        {
+            ITypeInfo *pTypeInfo;
+            hrc = pTypeLib->GetTypeInfoOfGuid(*m_pGUID, &pTypeInfo);
+            if (SUCCEEDED(hrc))
+            {
+                ITypeInfo2 *pTypeInfo2;
+                if (SUCCEEDED(pTypeInfo->QueryInterface(__uuidof(ITypeInfo2), (void **)&pTypeInfo2)))
+                {
+                    pTypeInfo->Release();
+                    pTypeInfo = pTypeInfo2;
+                }
+                m_pTInfo = pTypeInfo;
+                _pAtlModule->AddTermFunc(Cleanup, (void *)this);
+            }
+            pTypeLib->Release();
+        }
+        return hrc;
+    }
+    HRESULT GetTI(LCID lcid, ITypeInfo **ppTInfo)
+    {
+        AssertReturn(ppTInfo, E_POINTER);
+        HRESULT hrc = S_OK;
+        if (!m_pTInfo)
+            hrc = GetTI(lcid);
+        if (m_pTInfo)
+        {
+            m_pTInfo->AddRef();
+            hrc = S_OK;
+        }
+        *ppTInfo = m_pTInfo;
+        return hrc;
+    }
+    HRESULT FetchTI(LCID lcid)
+    {
+        if (!m_pTInfo)
+            return GetTI(lcid);
+        return S_OK;
+    }
+};
+
+template <class ThreadModel> class CComObjectRootEx
+{
+public:
+    typedef ThreadModel _ThreadModel;
+    CComObjectRootEx()
+    {
+        m_iRef = 0L;
+    }
+    ~CComObjectRootEx()
+    {
+    }
+    ULONG InternalAddRef()
+    {
+        Assert(m_iRef != -1L);
+        return ThreadModel::Increment(&m_iRef);
+    }
+    ULONG InternalRelease()
+    {
+#ifdef DEBUG
+        LONG c = ThreadModel::Decrement(&m_iRef);
+        if (c < -(LONG_MAX / 2))
+        {
+            AssertMsgFailed(("Release called on object which has been already released\n"));
+        }
+        return c;
+#else
+        return ThreadModel::Decrement(&m_iRef);
+#endif
+    }
+    ULONG OuterAddRef()
+    {
+        return m_pOuterUnknown->AddRef();
+    }
+    ULONG OuterRelease()
+    {
+        return m_pOuterUnknown->Release();
+    }
+    HRESULT OuterQueryInterface(REFIID iid, void **ppvObject)
+    {
+        return m_pOuterUnknown->QueryInterface(iid, ppvObject);
+    }
+    HRESULT _AtlInitialConstruct()
+    {
+        return m_CritSect.Init();
+    }
+    void Lock()
+    {
+        m_CritSect.Lock();
+    }
+    void Unlock()
+    {
+        m_CritSect.Unlock();
+    }
+    void SetVoid(void *)
+    {
+    }
+    void InternalFinalConstructAddRef()
+    {
+    }
+    void InternalFinalConstructRelease()
+    {
+        Assert(m_iRef == 0);
+    }
+    HRESULT FinalConstruct()
+    {
+        return S_OK;
+    }
+    void FinalRelease()
+    {
+    }
+    static HRESULT WINAPI InternalQueryInterface(void *pThis, const _ATL_INTMAP_ENTRY *pEntries, REFIID iid, void **ppvObj)
+    {
+        AssertReturn(pThis, E_INVALIDARG);
+        AssertReturn(pEntries, E_INVALIDARG);
+        AssertReturn(ppvObj, E_POINTER);
+        *ppvObj = NULL;
+        if (iid == IID_IUnknown)
+        {
+            // For IUnknown use first interface, must be simple map entry.
+            Assert(pEntries->pFunc == COM_SIMPLEMAPENTRY);
+            IUnknown *pObj = (IUnknown *)((INT_PTR)pThis + pEntries->dw);
+            pObj->AddRef();
+            *ppvObj = pObj;
+            return S_OK;
+        }
+        while (pEntries->pFunc)
+        {
+            if (iid == *pEntries->piid)
+            {
+                if (pEntries->pFunc == COM_SIMPLEMAPENTRY)
+                {
+                    IUnknown *pObj = (IUnknown *)((INT_PTR)pThis + pEntries->dw);
+                    pObj->AddRef();
+                    *ppvObj = pObj;
+                    return S_OK;
+                }
+                else
+                    return pEntries->pFunc(pThis, iid, ppvObj, pEntries->dw);
+            }
+            pEntries++;
+        }
+        return E_NOINTERFACE;
+    }
+    static HRESULT WINAPI _Delegate(void *pThis, REFIID iid, void **ppvObj, DWORD_PTR dw)
+    {
+        AssertPtrReturn(pThis, E_NOINTERFACE);
+        IUnknown *pObj = *(IUnknown **)((DWORD_PTR)pThis + dw);
+	// If this assertion fails then the object has a delegation with a NULL
+	// object pointer, which is highly unusual often means that the pointer
+	// was not set up correctly. Check the COM interface map of the class
+	// for bugs with initializing.
+        AssertPtrReturn(pObj, E_NOINTERFACE);
+        return pObj->QueryInterface(iid, ppvObj);
+    }
+
+    union
+    {
+        LONG m_iRef;
+        IUnknown *m_pOuterUnknown;
+    };
+private:
+    typename ThreadModel::AutoDeleteCriticalSection m_CritSect;
+};
+
+template <class Base> class CComObject : public Base
+{
+public:
+    CComObject(void * = NULL) throw()
+    {
+        AssertMsg(_pAtlModule, ("ATL: referring to ATL module without having one declared in this linking namespace\n"));
+        _pAtlModule->Lock();
+    }
+    virtual ~CComObject() throw()
+    {
+        AssertMsg(_pAtlModule, ("ATL: referring to ATL module without having one declared in this linking namespace\n"));
+        // Catch refcount screwups by setting refcount to -(LONG_MAX/2).
+        m_iRef = -(LONG_MAX/2);
+        FinalRelease();
+        _pAtlModule->Unlock();
+    }
+    STDMETHOD_(ULONG, AddRef)()
+    {
+        // If you get errors about undefined InternalAddRef then Base does not
+        // derive from CComObjectRootEx.
+        return InternalAddRef();
+    }
+    STDMETHOD_(ULONG, Release)()
+    {
+        // If you get errors about undefined InternalRelease then Base does not
+        // derive from CComObjectRootEx.
+        ULONG l = InternalRelease();
+        if (l == 0)
+            delete this;
+        return l;
+    }
+    STDMETHOD(QueryInterface)(REFIID iid, void **ppvObj) throw()
+    {
+        // If you get errors about undefined _InternalQueryInterface then
+        // double check BEGIN_COM_MAP in the class definition.
+        return _InternalQueryInterface(iid, ppvObj);
+    }
+
+    static HRESULT WINAPI CreateInstance(CComObject<Base> **pp) throw()
+    {
+        AssertReturn(pp, E_POINTER);
+        *pp = NULL;
+
+        HRESULT hrc = E_OUTOFMEMORY;
+        CComObject<Base> *p = NULL;
+        try
+        {
+            p = new CComObject<Base>();
+        }
+        catch (...)
+        {
+        }
+        if (p)
+        {
+            p->InternalFinalConstructAddRef();
+            hrc = p->_AtlInitialConstruct();
+            if (SUCCEEDED(hrc))
+                hrc = p->FinalConstruct();
+            p->InternalFinalConstructRelease();
+            if (FAILED(hrc))
+            {
+                delete p;
+                p = NULL;
+            }
+        }
+        *pp = p;
+        return hrc;
+    }
+};
+
+template <class T, const IID *piid, const GUID *pLibID, WORD iMajor = 1, WORD iMinor = 0> class ATL_NO_VTABLE IDispatchImpl : public T
+{
+public:
+    // IDispatch
+    STDMETHOD(GetTypeInfoCount)(UINT *pcTInfo)
+    {
+        if (!pcTInfo)
+            return E_POINTER;
+        *pcTInfo = 1;
+        return S_OK;
+    }
+    STDMETHOD(GetTypeInfo)(UINT cTInfo, LCID lcid, ITypeInfo **ppTInfo)
+    {
+        return tih.GetTypeInfo(cTInfo, lcid, ppTInfo);
+    }
+    STDMETHOD(GetIDsOfNames)(REFIID riid, LPOLESTR *pwszNames, UINT cNames, LCID lcid, DISPID *pDispID)
+    {
+        return tih.GetIDsOfNames(riid, pwszNames, cNames, lcid, pDispID);
+    }
+    STDMETHOD(Invoke)(DISPID DispID, REFIID riid, LCID lcid, WORD iFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
+    {
+        return tih.Invoke((IDispatch *)this, DispID, riid, lcid, iFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
+    }
+protected:
+    static CComTypeInfoHolder tih;
+    static HRESULT GetTI(LCID lcid, ITypeInfo **ppTInfo)
+    {
+        return tih.GetTI(lcid, ppTInfo);
+    }
+};
+
+template <class T, const IID *piid, const GUID *pLibID, WORD iMajor, WORD iMinor> CComTypeInfoHolder IDispatchImpl<T, piid, pLibID, iMajor, iMinor>::tih = { piid, pLibID, iMajor, iMinor, NULL };
+
+
+template <class Base> class CComContainedObject : public Base
+{
+public:
+    CComContainedObject(void *pv)
+    {
+        m_pOuterUnknown = (IUnknown *)pv;
+    }
+
+    STDMETHOD_(ULONG, AddRef)() throw()
+    {
+        return OuterAddRef();
+    }
+    STDMETHOD_(ULONG, Release)() throw()
+    {
+        return OuterRelease();
+    }
+    STDMETHOD(QueryInterface)(REFIID iid, void **ppvObj) throw()
+    {
+        return OuterQueryInterface(iid, ppvObj);
+    }
+};
+
+template <class Aggregated> class CComAggObject :
+    public IUnknown,
+    public CComObjectRootEx<typename Aggregated::_ThreadModel::ThreadModelNoCS>
+{
+public:
+    CComAggObject(void *pv) :
+        m_Aggregated(pv)
+    {
+        AssertMsg(_pAtlModule, ("ATL: referring to ATL module without having one declared in this linking namespace\n"));
+        _pAtlModule->Lock();
+    }
+    virtual ~CComAggObject()
+    {
+        AssertMsg(_pAtlModule, ("ATL: referring to ATL module without having one declared in this linking namespace\n"));
+        // Catch refcount screwups by setting refcount to -(LONG_MAX/2).
+        m_iRef = -(LONG_MAX/2);
+        FinalRelease();
+        _pAtlModule->Unlock();
+    }
+    HRESULT _AtlInitialConstruct()
+    {
+        HRESULT hrc = m_Aggregated._AtlInitialConstruct();
+        if (SUCCEEDED(hrc))
+        {
+            hrc = CComObjectRootEx<typename Aggregated::_ThreadModel::ThreadModelNoCS>::_AtlInitialConstruct();
+        }
+        return hrc;
+    }
+    HRESULT FinalConstruct()
+    {
+        CComObjectRootEx<Aggregated::_ThreadModel::ThreadModelNoCS>::FinalConstruct();
+        return m_Aggregated.FinalConstruct();
+    }
+    void FinalRelease()
+    {
+        CComObjectRootEx<Aggregated::_ThreadModel::ThreadModelNoCS>::FinalRelease();
+        m_Aggregated.FinalRelease();
+    }
+
+    STDMETHOD_(ULONG, AddRef)()
+    {
+        return InternalAddRef();
+    }
+    STDMETHOD_(ULONG, Release)()
+    {
+        ULONG l = InternalRelease();
+        if (l == 0)
+            delete this;
+        return l;
+    }
+    STDMETHOD(QueryInterface)(REFIID iid, void **ppvObj)
+    {
+        AssertReturn(ppvObj, E_POINTER);
+        *ppvObj = NULL;
+
+        HRESULT hrc = S_OK;
+        if (iid == __uuidof(IUnknown))
+        {
+            *ppvObj = (void *)(IUnknown *)this;
+            AddRef();
+        }
+        else
+            hrc = m_Aggregated._InternalQueryInterface(iid, ppvObj);
+        return hrc;
+    }
+    static HRESULT WINAPI CreateInstance(LPUNKNOWN pUnkOuter, CComAggObject<Aggregated> **pp)
+    {
+        AssertReturn(pp, E_POINTER);
+        *pp = NULL;
+
+        HRESULT hrc = E_OUTOFMEMORY;
+        CComAggObject<Aggregated> *p = NULL;
+        try
+        {
+            p = new CComAggObject<Aggregated>(pUnkOuter);
+        }
+        catch (...)
+        {
+        }
+        if (p)
+        {
+            p->SetVoid(NULL);
+            p->InternalFinalConstructAddRef();
+            hrc = p->_AtlInitialConstruct();
+            if (SUCCEEDED(hrc))
+                hrc = p->FinalConstruct();
+            p->InternalFinalConstructRelease();
+            if (FAILED(hrc))
+                delete p;
+            else
+                *pp = p;
+        }
+        return hrc;
+    }
+
+    CComContainedObject<Aggregated> m_Aggregated;
+};
+
+class CComClassFactory:
+    public IClassFactory,
+    public CComObjectRootEx<CComMultiThreadModel>
+{
+public:
+    BEGIN_COM_MAP(CComClassFactory)
+        COM_INTERFACE_ENTRY(IClassFactory)
+    END_COM_MAP()
+
+    virtual ~CComClassFactory()
+    {
+    }
+
+    // IClassFactory
+    STDMETHOD(CreateInstance)(LPUNKNOWN pUnkOuter, REFIID riid, void **ppvObj)
+    {
+        Assert(m_pfnCreateInstance);
+        HRESULT hrc = E_POINTER;
+        if (ppvObj)
+        {
+            *ppvObj = NULL;
+            if (pUnkOuter && riid != __uuidof(IUnknown))
+            {
+                AssertMsgFailed(("CComClassFactory: cannot create an aggregated object other than IUnknown\n"));
+                hrc = CLASS_E_NOAGGREGATION;
+            }
+            else
+                hrc = m_pfnCreateInstance(pUnkOuter, riid, ppvObj);
+        }
+        return hrc;
+    }
+
+    STDMETHOD(LockServer)(BOOL fLock)
+    {
+        AssertMsg(_pAtlModule, ("ATL: referring to ATL module without having one declared in this linking namespace\n"));
+        if (fLock)
+            _pAtlModule->Lock();
+        else
+            _pAtlModule->Unlock();
+        return S_OK;
+    }
+
+    // Set creator for use by the factory.
+    void SetVoid(void *pv)
+    {
+        m_pfnCreateInstance = (PFNCREATEINSTANCE)pv;
+    }
+
+    PFNCREATEINSTANCE m_pfnCreateInstance;
+};
+
+template <class T> class CComClassFactorySingleton : public CComClassFactory
+{
+public:
+    CComClassFactorySingleton() :
+        m_hrc(S_OK),
+        m_pObj(NULL)
+    {
+    }
+    virtual ~CComClassFactorySingleton()
+    {
+        if (m_pObj)
+            m_pObj->Release();
+    }
+    // IClassFactory
+    STDMETHOD(CreateInstance)(LPUNKNOWN pUnkOuter, REFIID riid, void **pvObj)
+    {
+        HRESULT hrc = E_POINTER;
+        if (ppvObj)
+        {
+            *ppvObj = NULL;
+            // Singleton factories do not support aggregation.
+            AssertReturn(!pUnkOuter, CLASS_E_NOAGGREGATION);
+
+            // Test if singleton is already created. Do it outside the lock,
+            // relying on atomic checks. Remember the inherent race!
+            if (SUCCEEDED(m_hrc) && !m_pObj)
+            {
+                    Lock();
+                    // Make sure that the module is in use, otherwise the
+                    // module can terminate while we're creating a new
+                    // instance, which leads to strange errors.
+                    LockServer(true);
+                __try
+                {
+                    // Repeat above test to avoid races when multiple threads
+                    // want to create a singleton simultaneously.
+                    if (SUCCEEDED(m_hrc) && !m_pObj)
+                    {
+                        CComObjectCached<T> *p;
+                        m_hrc = CComObjectCached<T>::CreateInstance(&p);
+                        if (SUCCEEDED(m_hrc))
+                        {
+                            m_hrc = p->QueryInterface(IID_IUnknown, (void **)&m_pObj);
+                            if (FAILED(m_hrc))
+                            {
+                                delete p;
+                            }
+                        }
+                    }
+                }
+                __finally
+                {
+                    Unlock();
+                    LockServer(false);
+                }
+            }
+            if (SUCCEEDED(m_hrc))
+            {
+                hrc = m_pObj->QueryInterface(riid, ppvObj);
+            }
+            else
+            {
+                hrc = m_hrc;
+            }
+        }
+        return hrc;
+    }
+    HRESULT m_hrc;
+    IUnknown *m_pObj;
+};
+
+
+template <class T, const CLSID *pClsID = &CLSID_NULL> class CComCoClass
+{
+public:
+    DECLARE_CLASSFACTORY()
+    DECLARE_AGGREGATABLE(T)
+    static const CLSID& WINAPI GetObjectCLSID()
+    {
+        return *pClsID;
+    }
+    template <class Q>
+    static HRESULT CreateInstance(Q **pp)
+    {
+        return T::_CreatorClass::CreateInstance(NULL, __uuidof(Q), (void **)pp);
+    }
+};
+
+} /* namespace ATL */
+
+#endif /* !___VBox_com_microatl_h */
+
Index: /trunk/src/VBox/HostDrivers/VBoxNetFlt/Makefile.kmk
===================================================================
--- /trunk/src/VBox/HostDrivers/VBoxNetFlt/Makefile.kmk	(revision 60764)
+++ /trunk/src/VBox/HostDrivers/VBoxNetFlt/Makefile.kmk	(revision 60765)
@@ -5,5 +5,5 @@
 
 #
-# Copyright (C) 2008-2015 Oracle Corporation
+# Copyright (C) 2008-2016 Oracle Corporation
 #
 # This file is part of VirtualBox Open Source Edition (OSE), as
@@ -365,5 +365,5 @@
  	win/nobj/VBoxNetFltNobj.rc
  VBoxNetFltNobj_LIBS       = \
- 	$(PATH_TOOL_$(VBOX_VCC_TOOL)_ATLMFC_LIB)/atls$(VBOX_VCC_CRT_TYPE).lib
+ 	$(LIB_RUNTIME)
  #VBoxNetFltNobj_INTERMEDIATES =
  VBoxNetFltNobj_DEPS       = \
Index: /trunk/src/VBox/HostDrivers/VBoxNetFlt/win/nobj/VBoxNetFltNobj.cpp
===================================================================
--- /trunk/src/VBox/HostDrivers/VBoxNetFlt/win/nobj/VBoxNetFltNobj.cpp	(revision 60764)
+++ /trunk/src/VBox/HostDrivers/VBoxNetFlt/win/nobj/VBoxNetFltNobj.cpp	(revision 60765)
@@ -5,5 +5,5 @@
  */
 /*
- * Copyright (C) 2011-2015 Oracle Corporation
+ * Copyright (C) 2011-2016 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -542,5 +542,5 @@
 
 
-CComModule _Module;
+static ATL::CComModule _Module;
 
 BEGIN_OBJECT_MAP(ObjectMap)
@@ -575,9 +575,19 @@
 STDAPI DllRegisterServer()
 {
+// this is a "just in case" conditional, which is not defined
+#ifdef VBOX_FORCE_REGISTER_SERVER
     return _Module.RegisterServer(TRUE);
+#else
+    return S_OK;
+#endif
 }
 
 STDAPI DllUnregisterServer()
 {
+// this is a "just in case" conditional, which is not defined
+#ifdef VBOX_FORCE_REGISTER_SERVER
     return _Module.UnregisterServer(TRUE);
-}
+#else
+    return S_OK;
+#endif
+}
Index: /trunk/src/VBox/HostDrivers/VBoxNetFlt/win/nobj/VBoxNetFltNobj.h
===================================================================
--- /trunk/src/VBox/HostDrivers/VBoxNetFlt/win/nobj/VBoxNetFltNobj.h	(revision 60764)
+++ /trunk/src/VBox/HostDrivers/VBoxNetFlt/win/nobj/VBoxNetFltNobj.h	(revision 60765)
@@ -5,5 +5,5 @@
  */
 /*
- * Copyright (C) 2011-2015 Oracle Corporation
+ * Copyright (C) 2011-2016 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -19,9 +19,6 @@
 
 #include <windows.h>
-/* atl stuff */
-#include <atlbase.h>
-extern CComModule _Module;
-#include <atlcom.h>
 
+#include "VBox/com/defs.h"
 #include "VBoxNetFltNobjT.h"
 #include "VBoxNetFltNobjRc.h"
@@ -34,6 +31,6 @@
  */
 class ATL_NO_VTABLE VBoxNetFltNobj :
-    public CComObjectRootEx<CComObjectThreadModel>,
-    public CComCoClass<VBoxNetFltNobj, &CLSID_VBoxNetFltNobj>,
+    public ATL::CComObjectRootEx<ATL::CComMultiThreadModel>,
+    public ATL::CComCoClass<VBoxNetFltNobj, &CLSID_VBoxNetFltNobj>,
     public INetCfgComponentControl,
     public INetCfgComponentNotifyBinding
@@ -48,5 +45,8 @@
     END_COM_MAP()
 
+    // this is a "just in case" conditional, which is not defined
+#ifdef VBOX_FORCE_REGISTER_SERVER
     DECLARE_REGISTRY_RESOURCEID(IDR_VBOXNETFLT_NOBJ)
+#endif
 
     /* INetCfgComponentControl methods */
Index: /trunk/src/VBox/HostServices/DragAndDrop/Makefile.kmk
===================================================================
--- /trunk/src/VBox/HostServices/DragAndDrop/Makefile.kmk	(revision 60764)
+++ /trunk/src/VBox/HostServices/DragAndDrop/Makefile.kmk	(revision 60765)
@@ -5,5 +5,5 @@
 
 #
-# Copyright (C) 2011-2015 Oracle Corporation
+# Copyright (C) 2011-2016 Oracle Corporation
 #
 # This file is part of VirtualBox Open Source Edition (OSE), as
@@ -33,5 +33,4 @@
 VBoxDragAndDropSvc_INCS      = $(PATH_ROOT)/src/VBox/Main/include ./
 VBoxDragAndDropSvc_INCS.win  = \
-	$(PATH_TOOL_$(VBOX_VCC_TOOL)_ATLMFC_INC) \
 	$(VBOX_PATH_SDK)
 
Index: /trunk/src/VBox/HostServices/GuestControl/Makefile.kmk
===================================================================
--- /trunk/src/VBox/HostServices/GuestControl/Makefile.kmk	(revision 60764)
+++ /trunk/src/VBox/HostServices/GuestControl/Makefile.kmk	(revision 60765)
@@ -5,5 +5,5 @@
 
 #
-# Copyright (C) 2011-2015 Oracle Corporation
+# Copyright (C) 2011-2016 Oracle Corporation
 #
 # This file is part of VirtualBox Open Source Edition (OSE), as
@@ -31,5 +31,4 @@
 VBoxGuestControlSvc_INCS      = $(PATH_ROOT)/src/VBox/Main/include
 VBoxGuestControlSvc_INCS.win  = \
-	$(PATH_TOOL_$(VBOX_VCC_TOOL)_ATLMFC_INC) \
 	$(VBOX_PATH_SDK)
 
Index: /trunk/src/VBox/HostServices/GuestProperties/Makefile.kmk
===================================================================
--- /trunk/src/VBox/HostServices/GuestProperties/Makefile.kmk	(revision 60764)
+++ /trunk/src/VBox/HostServices/GuestProperties/Makefile.kmk	(revision 60765)
@@ -5,5 +5,5 @@
 
 #
-# Copyright (C) 2006-2015 Oracle Corporation
+# Copyright (C) 2006-2016 Oracle Corporation
 #
 # This file is part of VirtualBox Open Source Edition (OSE), as
@@ -31,5 +31,4 @@
 VBoxGuestPropSvc_INCS      = $(PATH_ROOT)/src/VBox/Main/include
 VBoxGuestPropSvc_INCS.win  = \
-	$(PATH_TOOL_$(VBOX_VCC_TOOL)_ATLMFC_INC) \
 	$(VBOX_PATH_SDK)
 
Index: /trunk/src/VBox/HostServices/SharedFolders/Makefile.kmk
===================================================================
--- /trunk/src/VBox/HostServices/SharedFolders/Makefile.kmk	(revision 60764)
+++ /trunk/src/VBox/HostServices/SharedFolders/Makefile.kmk	(revision 60765)
@@ -5,5 +5,5 @@
 
 #
-# Copyright (C) 2006-2015 Oracle Corporation
+# Copyright (C) 2006-2016 Oracle Corporation
 #
 # This file is part of VirtualBox Open Source Edition (OSE), as
@@ -30,5 +30,4 @@
 VBoxSharedFolders_DEFS      = VBOX_WITH_HGCM RTSHFL
 VBoxSharedFolders_INCS.win  = \
-	$(PATH_TOOL_$(VBOX_VCC_TOOL)_ATLMFC_INC) \
 	$(VBOX_PATH_SDK)
 
Index: /trunk/src/VBox/HostServices/SharedOpenGL/Makefile.kmk
===================================================================
--- /trunk/src/VBox/HostServices/SharedOpenGL/Makefile.kmk	(revision 60764)
+++ /trunk/src/VBox/HostServices/SharedOpenGL/Makefile.kmk	(revision 60765)
@@ -5,5 +5,5 @@
 
 #
-# Copyright (C) 2008-2015 Oracle Corporation
+# Copyright (C) 2008-2016 Oracle Corporation
 #
 # This file is part of VirtualBox Open Source Edition (OSE), as
@@ -60,5 +60,4 @@
 	$(TEMPLATE_VBOXMAINEXE_INTERMEDIATES)
 VBoxSharedCrOpenGL_INCS.win = \
-	$(PATH_TOOL_$(VBOX_VCC_TOOL)_ATLMFC_INC) \
 	$(VBOX_PATH_SDK)/bindings/mscom/include
 ifdef VBOX_WITH_XPCOM
@@ -85,6 +84,5 @@
 ifeq ($(KBUILD_TARGET),win)
  VBoxSharedCrOpenGL_LIBS += \
-	$(PATH_OBJ)/VBoxOGLrenderspu/VBoxOGLrenderspu$(VBOX_SUFF_LIB) \
-	$(PATH_TOOL_$(VBOX_VCC_TOOL)_ATLMFC_LIB)/atls$(VBOX_SUFF_LIB)
+	$(PATH_OBJ)/VBoxOGLrenderspu/VBoxOGLrenderspu$(VBOX_SUFF_LIB)
 else
  VBoxSharedCrOpenGL_LIBS += \
Index: /trunk/src/VBox/Main/idl/apiwrap-server.xsl
===================================================================
--- /trunk/src/VBox/Main/idl/apiwrap-server.xsl	(revision 60764)
+++ /trunk/src/VBox/Main/idl/apiwrap-server.xsl	(revision 60765)
@@ -6,5 +6,5 @@
         VirtualBox.xidl.
 
-    Copyright (C) 2010-2015 Oracle Corporation
+    Copyright (C) 2010-2016 Oracle Corporation
 
     This file is part of VirtualBox Open Source Edition (OSE), as
@@ -170,6 +170,6 @@
         </xsl:for-each>
     </xsl:if>
-    <xsl:text>        COM_INTERFACE_ENTRY_AGGREGATE(IID_IMarshal, m_pUnkMarshaler.p)
-    END_COM_MAP()
+    <xsl:value-of select="concat('        VBOX_TWEAK_INTERFACE_ENTRY(', @name, ')', $G_sNewLine)"/>
+    <xsl:text>    END_COM_MAP()
 
 </xsl:text>
Index: /trunk/src/VBox/Main/idl/comimpl.xsl
===================================================================
--- /trunk/src/VBox/Main/idl/comimpl.xsl	(revision 60764)
+++ /trunk/src/VBox/Main/idl/comimpl.xsl	(revision 60765)
@@ -14,5 +14,5 @@
         Further extension to other interfaces is possible and anticipated.
 
-    Copyright (C) 2010-2015 Oracle Corporation
+    Copyright (C) 2010-2016 Oracle Corporation
 
     This file is part of VirtualBox Open Source Edition (OSE), as
@@ -58,5 +58,5 @@
 
 /*
- * Copyright (C) 2010-2015 Oracle Corporation
+ * Copyright (C) 2010-2016 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -488,5 +488,8 @@
   <xsl:value-of select="       '    DECLARE_PROTECT_FINAL_CONSTRUCT()&#10;'" />
   <xsl:value-of select="concat('    BEGIN_COM_MAP(', $implName, ')&#10;')" />
-  <xsl:value-of select="concat('        VBOX_DEFAULT_INTERFACE_ENTRIES(', @name, ')&#10;')" />
+  <xsl:value-of select="       '        COM_INTERFACE_ENTRY(ISupportErrorInfo)&#10;'" />
+  <xsl:value-of select="concat('        COM_INTERFACE_ENTRY(', @name, ')&#10;')" />
+  <xsl:value-of select="concat('        COM_INTERFACE_ENTRY2(IDispatch, ', @name, ')&#10;')" />
+  <xsl:value-of select="concat('        VBOX_TWEAK_INTERFACE_ENTRY(', @name, ')&#10;')" />
 
   <xsl:call-template name="genComEntry">
Index: /trunk/src/VBox/Main/include/MachineImpl.h
===================================================================
--- /trunk/src/VBox/Main/include/MachineImpl.h	(revision 60764)
+++ /trunk/src/VBox/Main/include/MachineImpl.h	(revision 60765)
@@ -5,5 +5,5 @@
 
 /*
- * Copyright (C) 2006-2015 Oracle Corporation
+ * Copyright (C) 2006-2016 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -1294,6 +1294,9 @@
 
     BEGIN_COM_MAP(SessionMachine)
-        VBOX_DEFAULT_INTERFACE_ENTRIES(IMachine)
+        COM_INTERFACE_ENTRY(ISupportErrorInfo)
+        COM_INTERFACE_ENTRY(IMachine)
+        COM_INTERFACE_ENTRY2(IDispatch, IMachine)
         COM_INTERFACE_ENTRY(IInternalMachineControl)
+        VBOX_TWEAK_INTERFACE_ENTRY(IMachine)
     END_COM_MAP()
 
@@ -1548,5 +1551,8 @@
 
     BEGIN_COM_MAP(SnapshotMachine)
-        VBOX_DEFAULT_INTERFACE_ENTRIES(IMachine)
+        COM_INTERFACE_ENTRY(ISupportErrorInfo)
+        COM_INTERFACE_ENTRY(IMachine)
+        COM_INTERFACE_ENTRY2(IDispatch, IMachine)
+        VBOX_TWEAK_INTERFACE_ENTRY(IMachine)
     END_COM_MAP()
 
Index: /trunk/src/VBox/Main/include/ProgressProxyImpl.h
===================================================================
--- /trunk/src/VBox/Main/include/ProgressProxyImpl.h	(revision 60764)
+++ /trunk/src/VBox/Main/include/ProgressProxyImpl.h	(revision 60765)
@@ -1,9 +1,9 @@
 /* $Id$ */
 /** @file
- * IProgress implementation for Machine::openRemoteSession in VBoxSVC.
+ * IProgress implementation for Machine::LaunchVMProcess in VBoxSVC.
  */
 
 /*
- * Copyright (C) 2006-2012 Oracle Corporation
+ * Copyright (C) 2006-2016 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -37,5 +37,8 @@
 
     BEGIN_COM_MAP(ProgressProxy)
-        VBOX_DEFAULT_INTERFACE_ENTRIES (IProgress)
+        COM_INTERFACE_ENTRY(ISupportErrorInfo)
+        COM_INTERFACE_ENTRY(IProgress)
+        COM_INTERFACE_ENTRY2(IDispatch, IProgress)
+        VBOX_TWEAK_INTERFACE_ENTRY(IProgress)
     END_COM_MAP()
 
@@ -43,5 +46,5 @@
     void    FinalRelease();
     HRESULT init(
-#if !defined (VBOX_COM_INPROC)
+#ifndef VBOX_COM_INPROC
                  VirtualBox *pParent,
 #endif
@@ -50,5 +53,5 @@
                  BOOL fCancelable);
     HRESULT init(
-#if !defined (VBOX_COM_INPROC)
+#ifndef VBOX_COM_INPROC
                  VirtualBox *pParent,
 #endif
Index: /trunk/src/VBox/Main/include/SessionImpl.h
===================================================================
--- /trunk/src/VBox/Main/include/SessionImpl.h	(revision 60764)
+++ /trunk/src/VBox/Main/include/SessionImpl.h	(revision 60765)
@@ -5,5 +5,5 @@
 
 /*
- * Copyright (C) 2006-2015 Oracle Corporation
+ * Copyright (C) 2006-2016 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -32,5 +32,5 @@
     public SessionWrap
 #ifdef RT_OS_WINDOWS
-    , public CComCoClass<Session, &CLSID_Session>
+    , public ATL::CComCoClass<Session, &CLSID_Session>
 #endif
 {
@@ -39,5 +39,6 @@
     DECLARE_CLASSFACTORY()
 
-    DECLARE_REGISTRY_RESOURCEID(IDR_VIRTUALBOX)
+    // Do not use any ATL registry support.
+    //DECLARE_REGISTRY_RESOURCEID(IDR_VIRTUALBOX)
 
     DECLARE_NOT_AGGREGATABLE(Session)
Index: /trunk/src/VBox/Main/include/VirtualBoxBase.h
===================================================================
--- /trunk/src/VBox/Main/include/VirtualBoxBase.h	(revision 60764)
+++ /trunk/src/VBox/Main/include/VirtualBoxBase.h	(revision 60765)
@@ -5,5 +5,5 @@
 
 /*
- * Copyright (C) 2006-2015 Oracle Corporation
+ * Copyright (C) 2006-2016 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -62,6 +62,4 @@
 #if !defined(VBOX_WITH_XPCOM)
 
-#include <atlcom.h>
-
 /* use a special version of the singleton class factory,
  * see KB811591 in msdn for more info. */
@@ -71,9 +69,16 @@
 
 template <class T>
-class CMyComClassFactorySingleton : public CComClassFactory
+class CMyComClassFactorySingleton : public ATL::CComClassFactory
 {
 public:
-    CMyComClassFactorySingleton() : m_hrCreate(S_OK){}
-    virtual ~CMyComClassFactorySingleton(){}
+    CMyComClassFactorySingleton() :
+        m_hrCreate(S_OK), m_spObj(NULL)
+    {
+    }
+    virtual ~CMyComClassFactorySingleton()
+    {
+        if (m_spObj)
+            m_spObj->Release();
+    }
     // IClassFactory
     STDMETHOD(CreateInstance)(LPUNKNOWN pUnkOuter, REFIID riid, void** ppvObj)
@@ -83,49 +88,44 @@
         {
             *ppvObj = NULL;
-            // Aggregation is not supported in singleton objects.
-            ATLASSERT(pUnkOuter == NULL);
-            if (pUnkOuter != NULL)
-                hRes = CLASS_E_NOAGGREGATION;
+            // no aggregation for singletons
+            AssertReturn(pUnkOuter == NULL, CLASS_E_NOAGGREGATION);
+            if (m_hrCreate == S_OK && m_spObj == NULL)
+            {
+                Lock();
+                __try
+                {
+                    // Fix:  The following If statement was moved inside the __try statement.
+                    // Did another thread arrive here first?
+                    if (m_hrCreate == S_OK && m_spObj == NULL)
+                    {
+                        // lock the module to indicate activity
+                        // (necessary for the monitor shutdown thread to correctly
+                        // terminate the module in case when CreateInstance() fails)
+                        ATL::_pAtlModule->Lock();
+                        ATL::CComObjectCached<T> *p;
+                        m_hrCreate = ATL::CComObjectCached<T>::CreateInstance(&p);
+                        if (SUCCEEDED(m_hrCreate))
+                        {
+                            m_hrCreate = p->QueryInterface(IID_IUnknown, (void **)&m_spObj);
+                            if (FAILED(m_hrCreate))
+                            {
+                                delete p;
+                            }
+                        }
+                        ATL::_pAtlModule->Unlock();
+                    }
+                }
+                __finally
+                {
+                    Unlock();
+                }
+            }
+            if (m_hrCreate == S_OK)
+            {
+                hRes = m_spObj->QueryInterface(riid, ppvObj);
+            }
             else
             {
-                if (m_hrCreate == S_OK && m_spObj == NULL)
-                {
-                    Lock();
-                    __try
-                    {
-                        // Fix:  The following If statement was moved inside the __try statement.
-                        // Did another thread arrive here first?
-                        if (m_hrCreate == S_OK && m_spObj == NULL)
-                        {
-                            // lock the module to indicate activity
-                            // (necessary for the monitor shutdown thread to correctly
-                            // terminate the module in case when CreateInstance() fails)
-                            _pAtlModule->Lock();
-                            CComObjectCached<T> *p;
-                            m_hrCreate = CComObjectCached<T>::CreateInstance(&p);
-                            if (SUCCEEDED(m_hrCreate))
-                            {
-                                m_hrCreate = p->QueryInterface(IID_IUnknown, (void**)&m_spObj);
-                                if (FAILED(m_hrCreate))
-                                {
-                                    delete p;
-                                }
-                            }
-                            _pAtlModule->Unlock();
-                        }
-                    }
-                    __finally
-                    {
-                        Unlock();
-                    }
-                }
-                if (m_hrCreate == S_OK)
-                {
-                    hRes = m_spObj->QueryInterface(riid, ppvObj);
-                }
-                else
-                {
-                    hRes = m_hrCreate;
-                }
+                hRes = m_hrCreate;
             }
         }
@@ -133,5 +133,5 @@
     }
     HRESULT m_hrCreate;
-    CComPtr<IUnknown> m_spObj;
+    IUnknown *m_spObj;
 };
 
@@ -642,10 +642,10 @@
   #define VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(cls, iface) \
     VIRTUALBOXBASE_ADD_VIRTUAL_COMPONENT_METHODS(cls, iface)
-#else // #ifdef VBOX_WITH_XPCOM
+#else // !VBOX_WITH_XPCOM
   #define VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(cls, iface) \
     VIRTUALBOXBASE_ADD_VIRTUAL_COMPONENT_METHODS(cls, iface) \
     STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid) \
     { \
-        const _ATL_INTMAP_ENTRY* pEntries = cls::_GetEntries(); \
+        const ATL::_ATL_INTMAP_ENTRY* pEntries = cls::_GetEntries(); \
         Assert(pEntries); \
         if (!pEntries) \
@@ -664,5 +664,18 @@
         return bSupports ? S_OK : S_FALSE; \
     }
-#endif // #ifdef VBOX_WITH_XPCOM
+#endif // !VBOX_WITH_XPCOM
+
+/**
+ * VBOX_TWEAK_INTERFACE_ENTRY:
+ * Macro for defining magic interface entries needed for all interfaces
+ * implemented by any subclass of VirtualBoxBase.
+ */
+#ifdef VBOX_WITH_XPCOM
+#define VBOX_TWEAK_INTERFACE_ENTRY(iface)
+#else // !VBOX_WITH_XPCOM
+#define VBOX_TWEAK_INTERFACE_ENTRY(iface)                                   \
+        COM_INTERFACE_ENTRY_AGGREGATE(IID_IMarshal, m_pUnkMarshaler.m_p)
+#endif // !VBOX_WITH_XPCOM
+
 
 /**
@@ -676,5 +689,5 @@
 class ATL_NO_VTABLE VirtualBoxBase
     : public VirtualBoxTranslatable,
-      public CComObjectRootEx<CComMultiThreadModel>
+      public ATL::CComObjectRootEx<ATL::CComMultiThreadModel>
 #if !defined (VBOX_WITH_XPCOM)
     , public ISupportErrorInfo
@@ -683,12 +696,12 @@
 protected:
 #ifdef RT_OS_WINDOWS
-     CComPtr <IUnknown>   m_pUnkMarshaler;
+     ComPtr<IUnknown> m_pUnkMarshaler;
 #endif
 
-     HRESULT   BaseFinalConstruct()
+     HRESULT BaseFinalConstruct()
      {
 #ifdef RT_OS_WINDOWS
         return CoCreateFreeThreadedMarshaler(this, //GetControllingUnknown(),
-                                             &m_pUnkMarshaler.p);
+                                             m_pUnkMarshaler.asOutParam());
 #else
         return S_OK;
@@ -696,8 +709,8 @@
      }
 
-     void   BaseFinalRelease()
+     void BaseFinalRelease()
      {
 #ifdef RT_OS_WINDOWS
-         m_pUnkMarshaler.Release();
+         m_pUnkMarshaler.setNull();
 #endif
      }
Index: /trunk/src/VBox/Main/include/VirtualBoxClientImpl.h
===================================================================
--- /trunk/src/VBox/Main/include/VirtualBoxClientImpl.h	(revision 60764)
+++ /trunk/src/VBox/Main/include/VirtualBoxClientImpl.h	(revision 60765)
@@ -6,5 +6,5 @@
 
 /*
- * Copyright (C) 2010-2014 Oracle Corporation
+ * Copyright (C) 2010-2016 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -30,5 +30,5 @@
     public VirtualBoxClientWrap
 #ifdef RT_OS_WINDOWS
-    , public CComCoClass<VirtualBoxClient, &CLSID_VirtualBoxClient>
+    , public ATL::CComCoClass<VirtualBoxClient, &CLSID_VirtualBoxClient>
 #endif
 {
@@ -36,5 +36,6 @@
     DECLARE_CLASSFACTORY()
 
-    DECLARE_REGISTRY_RESOURCEID(IDR_VIRTUALBOX)
+    // Do not use any ATL registry support.
+    //DECLARE_REGISTRY_RESOURCEID(IDR_VIRTUALBOX)
 
     DECLARE_NOT_AGGREGATABLE(VirtualBoxClient)
Index: /trunk/src/VBox/Main/include/VirtualBoxErrorInfoImpl.h
===================================================================
--- /trunk/src/VBox/Main/include/VirtualBoxErrorInfoImpl.h	(revision 60764)
+++ /trunk/src/VBox/Main/include/VirtualBoxErrorInfoImpl.h	(revision 60765)
@@ -24,5 +24,5 @@
 
 class ATL_NO_VTABLE VirtualBoxErrorInfo
-    : public CComObjectRootEx<CComMultiThreadModel>
+    : public ATL::CComObjectRootEx<ATL::CComMultiThreadModel>
     , VBOX_SCRIPTABLE_IMPL(IVirtualBoxErrorInfo)
 #ifndef VBOX_WITH_XPCOM /* IErrorInfo doesn't inherit from IDispatch, ugly 3am hack: */
Index: /trunk/src/VBox/Main/include/VirtualBoxImpl.h
===================================================================
--- /trunk/src/VBox/Main/include/VirtualBoxImpl.h	(revision 60764)
+++ /trunk/src/VBox/Main/include/VirtualBoxImpl.h	(revision 60765)
@@ -5,5 +5,5 @@
 
 /*
- * Copyright (C) 2006-2015 Oracle Corporation
+ * Copyright (C) 2006-2016 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -61,5 +61,5 @@
     public VirtualBoxWrap
 #ifdef RT_OS_WINDOWS
-     , public CComCoClass<VirtualBox, &CLSID_VirtualBox>
+     , public ATL::CComCoClass<VirtualBox, &CLSID_VirtualBox>
 #endif
 {
@@ -75,5 +75,6 @@
     DECLARE_CLASSFACTORY_SINGLETON(VirtualBox)
 
-    DECLARE_REGISTRY_RESOURCEID(IDR_VIRTUALBOX)
+    // Do not use any ATL registry support.
+    //DECLARE_REGISTRY_RESOURCEID(IDR_VIRTUALBOX)
 
     // Kind of redundant (VirtualBoxWrap declares itself not aggregatable and
Index: /trunk/src/VBox/Main/src-all/EventImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/src-all/EventImpl.cpp	(revision 60764)
+++ /trunk/src/VBox/Main/src-all/EventImpl.cpp	(revision 60765)
@@ -1261,5 +1261,8 @@
 
     BEGIN_COM_MAP(PassiveEventListener)
-        VBOX_DEFAULT_INTERFACE_ENTRIES(IEventListener)
+        COM_INTERFACE_ENTRY(ISupportErrorInfo)
+        COM_INTERFACE_ENTRY(IEventListener)
+        COM_INTERFACE_ENTRY2(IDispatch, IEventListener)
+        VBOX_TWEAK_INTERFACE_ENTRY(IEventListener)
     END_COM_MAP()
 
@@ -1301,5 +1304,8 @@
 
     BEGIN_COM_MAP(ProxyEventListener)
-        VBOX_DEFAULT_INTERFACE_ENTRIES(IEventListener)
+        COM_INTERFACE_ENTRY(ISupportErrorInfo)
+        COM_INTERFACE_ENTRY(IEventListener)
+        COM_INTERFACE_ENTRY2(IDispatch, IEventListener)
+        VBOX_TWEAK_INTERFACE_ENTRY(IEventListener)
     END_COM_MAP()
 
@@ -1356,5 +1362,8 @@
 
     BEGIN_COM_MAP(EventSourceAggregator)
-        VBOX_DEFAULT_INTERFACE_ENTRIES(IEventSource)
+        COM_INTERFACE_ENTRY(ISupportErrorInfo)
+        COM_INTERFACE_ENTRY(IEventSource)
+        COM_INTERFACE_ENTRY2(IDispatch, IEventSource)
+        VBOX_TWEAK_INTERFACE_ENTRY(IEventSource)
     END_COM_MAP()
 
Index: /trunk/src/VBox/Main/src-client/win/dllmain.cpp
===================================================================
--- /trunk/src/VBox/Main/src-client/win/dllmain.cpp	(revision 60764)
+++ /trunk/src/VBox/Main/src-client/win/dllmain.cpp	(revision 60765)
@@ -25,7 +25,4 @@
 #include <VirtualBoxClientImpl.h>
 
-#include <atlbase.h>
-#include <atlcom.h>
-
 #include <iprt/initterm.h>
 
@@ -34,5 +31,5 @@
 *   Global Variables                                                                                                             *
 *********************************************************************************************************************************/
-CComModule _Module;
+static ATL::CComModule _Module;
 
 BEGIN_OBJECT_MAP(ObjectMap)
@@ -50,9 +47,9 @@
     if (dwReason == DLL_PROCESS_ATTACH)
     {
+        // idempotent, so doesn't harm, and needed for COM embedding scenario
+        RTR3InitDll(RTR3INIT_FLAGS_UNOBTRUSIVE);
+
         _Module.Init(ObjectMap, hInstance, &LIBID_VirtualBox);
         DisableThreadLibraryCalls(hInstance);
-
-        // idempotent, so doesn't harm, and needed for COM embedding scenario
-        RTR3InitDll(RTR3INIT_FLAGS_UNOBTRUSIVE);
     }
     else if (dwReason == DLL_PROCESS_DETACH)
@@ -68,5 +65,5 @@
 STDAPI DllCanUnloadNow(void)
 {
-    return (_Module.GetLockCount()==0) ? S_OK : S_FALSE;
+    return (_Module.GetLockCount() == 0) ? S_OK : S_FALSE;
 }
 
@@ -74,5 +71,5 @@
 // Returns a class factory to create an object of the requested type
 
-STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
+STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
 {
     return _Module.GetClassObject(rclsid, riid, ppv);
Index: /trunk/src/VBox/Main/src-server/USBDeviceFiltersImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/src-server/USBDeviceFiltersImpl.cpp	(revision 60764)
+++ /trunk/src/VBox/Main/src-server/USBDeviceFiltersImpl.cpp	(revision 60765)
@@ -5,5 +5,5 @@
 
 /*
- * Copyright (C) 2005-2015 Oracle Corporation
+ * Copyright (C) 2005-2016 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -258,5 +258,8 @@
     DECLARE_PROTECT_FINAL_CONSTRUCT()
     BEGIN_COM_MAP(USBDeviceFilter)
-        VBOX_DEFAULT_INTERFACE_ENTRIES(IUSBDeviceFilter)
+        COM_INTERFACE_ENTRY(ISupportErrorInfo)
+        COM_INTERFACE_ENTRY(IUSBDeviceFilter)
+        COM_INTERFACE_ENTRY2(IDispatch, IUSBDeviceFilter)
+        VBOX_TWEAK_INTERFACE_ENTRY(IUSBDeviceFilter)
     END_COM_MAP()
 
Index: /trunk/src/VBox/Main/src-server/win/svcmain.cpp
===================================================================
--- /trunk/src/VBox/Main/src-server/win/svcmain.cpp	(revision 60764)
+++ /trunk/src/VBox/Main/src-server/win/svcmain.cpp	(revision 60765)
@@ -6,5 +6,5 @@
 
 /*
- * Copyright (C) 2004-2013 Oracle Corporation
+ * Copyright (C) 2004-2016 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -20,4 +20,5 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <tchar.h>
 
 #include "VBox/com/defs.h"
@@ -41,10 +42,5 @@
 #include <iprt/message.h>
 
-#include <atlbase.h>
-#include <atlcom.h>
-
-#define _ATL_FREE_THREADED
-
-class CExeModule : public CComModule
+class CExeModule : public ATL::CComModule
 {
 public:
@@ -70,5 +66,5 @@
 LONG CExeModule::Unlock()
 {
-    LONG l = CComModule::Unlock();
+    LONG l = ATL::CComModule::Unlock();
     if (l == 0)
     {
@@ -92,5 +88,5 @@
         } while (dwWait == WAIT_OBJECT_0);
         /* timed out */
-        if (!bActivity && m_nLockCnt == 0) /* if no activity let's really bail */
+        if (!bActivity && GetLockCount() == 0) /* if no activity let's really bail */
         {
             /* Disable log rotation at this point, worst case a log file
@@ -114,7 +110,7 @@
                 }
             }
-#if _WIN32_WINNT >= 0x0400 & defined(_ATL_FREE_THREADED)
+#if _WIN32_WINNT >= 0x0400
             CoSuspendClassObjects();
-            if (!bActivity && m_nLockCnt == 0)
+            if (!bActivity && GetLockCount() == 0)
 #endif
                 break;
@@ -135,5 +131,5 @@
 }
 
-CExeModule _Module;
+static CExeModule _Module;
 
 BEGIN_OBJECT_MAP(ObjectMap)
@@ -384,5 +380,5 @@
     {
         _Module.StartMonitor();
-#if _WIN32_WINNT >= 0x0400 & defined(_ATL_FREE_THREADED)
+#if _WIN32_WINNT >= 0x0400
         hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED);
         _ASSERTE(SUCCEEDED(hRes));
Index: /trunk/src/VBox/Main/testcase/makefile.tstVBoxAPIWin
===================================================================
--- /trunk/src/VBox/Main/testcase/makefile.tstVBoxAPIWin	(revision 60764)
+++ /trunk/src/VBox/Main/testcase/makefile.tstVBoxAPIWin	(revision 60765)
@@ -3,5 +3,5 @@
 #
 #
-# Copyright (C) 2006-2013 Oracle Corporation
+# Copyright (C) 2006-2016 Oracle Corporation
 #
 # This file is part of VirtualBox Open Source Edition (OSE), as
@@ -32,5 +32,4 @@
 # VS_INSTALL_PATH = "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\"
 # VS_INCLUDE_PATH = "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include"
-# VS_ATLMFC_INCLUDE_PATH = "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\atlmfc\include"
 # WIN_SDK_INCLUDE_PATH = "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include"
 # WIN_SDK_LIB_PATH = "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib\x64\"
