Index: /trunk/src/VBox/Frontends/VirtualBox/Makefile.kmk
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/Makefile.kmk	(revision 71790)
+++ /trunk/src/VBox/Frontends/VirtualBox/Makefile.kmk	(revision 71791)
@@ -280,4 +280,5 @@
 	$(VBOX_GUI_INC_DIRS) \
 	$(VirtualBox_0_OUTDIR)/include \
+	$(VBoxGlobal_0_OUTDIR)/include \
 	$(VBOX_GRAPHICS_INCS)
 
@@ -288,5 +289,5 @@
 VBoxGlobal_INCS = \
 	$(VBOX_GUI_INC_DIRS) \
-	$(VirtualBox_0_OUTDIR)/include
+	$(VBoxGlobal_0_OUTDIR)/include
 
 
@@ -1429,31 +1430,25 @@
 
 #
-# Generate the COM wrappers.
-#
-includedep $(PATH_TARGET)/VirtualBoxComWrapperFiles.dep
-VirtualBox_BLDDIRS       += $(VirtualBox_0_OUTDIR)/include
-VirtualBox_INTERMEDIATES += $(VirtualBox_0_OUTDIR)/include/COMWrappers
-VirtualBox_CLEAN         += $(wildcard $(VirtualBox_0_OUTDIR)/include/*) $(PATH_TARGET)/VirtualBoxComWrapperFiles.dep
-
-#
-# VirtualBox - GUI Application.
 # Build generated COM wrappers as part of app.
 #
-VirtualBox_SOURCES       += $(VirtualBox_0_OUTDIR)/include/COMWrappers.cpp
-
-#
-# VirtualBox - GUI Global Shared Library.
-# Build generated COM wrappers as part of app.
-#
-VBoxGlobal_SOURCES += $(VirtualBox_0_OUTDIR)/include/COMWrappers.cpp
-
-## @todo kmk doesn't remake optional outputs when they are missing...
-$$(VirtualBox_0_OUTDIR)/include/COMWrappers \
-+ $(PATH_TARGET)/VirtualBoxComWrapperFiles.dep \
-+| $(if-expr defined(VirtualBox_VBOX_COMWRAPPERS) \
-, $(VirtualBox_VBOX_COMWRAPPERS) \
-, $$(VirtualBox_0_OUTDIR)/include/COMEnums.h \
-  $$(VirtualBox_0_OUTDIR)/include/COMWrappers.cpp \
-): \
+ifndef VBOX_GUI_WITH_SHARED_LIBRARY
+
+ #
+ # VirtualBox - GUI Application.
+ #
+ includedep $(PATH_TARGET)/VirtualBoxComWrapperFiles.dep
+ VirtualBox_BLDDIRS       += $(VirtualBox_0_OUTDIR)/include
+ VirtualBox_INTERMEDIATES += $(VirtualBox_0_OUTDIR)/include/COMWrappers
+ VirtualBox_SOURCES       += $(VirtualBox_0_OUTDIR)/include/COMWrappers.cpp
+ VirtualBox_CLEAN         += $(wildcard $(VirtualBox_0_OUTDIR)/include/*) $(PATH_TARGET)/VirtualBoxComWrapperFiles.dep
+
+ ## @todo kmk doesn't remake optional outputs when they are missing...
+ $$(VirtualBox_0_OUTDIR)/include/COMWrappers \
+ + $(PATH_TARGET)/VirtualBoxComWrapperFiles.dep \
+ +| $(if-expr defined(VirtualBox_VBOX_COMWRAPPERS) \
+ , $(VirtualBox_VBOX_COMWRAPPERS) \
+ , $$(VirtualBox_0_OUTDIR)/include/COMEnums.h \
+   $$(VirtualBox_0_OUTDIR)/include/COMWrappers.cpp \
+ ): \
 		$(VBOX_XIDL_FILE) \
 		$(VBOX_VIRTUALBOX_SRC)/src/globals/COMWrappers.xsl \
@@ -1468,6 +1463,42 @@
 	$(APPEND) "$@"
 
-# alias for generating the COM Wrappers file.
-testwrappers:: $$(VirtualBox_0_OUTDIR)/include/COMWrappers
+ # alias for generating the COM Wrappers file.
+ testwrappers:: $$(VirtualBox_0_OUTDIR)/include/COMWrappers
+
+else
+
+ #
+ # VirtualBox - GUI Global Shared Library.
+ #
+ includedep $(PATH_TARGET)/VboxGlobalComWrapperFiles.dep
+ VBoxGlobal_BLDDIRS       += $(VBoxGlobal_0_OUTDIR)/include
+ VBoxGlobal_INTERMEDIATES += $(VBoxGlobal_0_OUTDIR)/include/COMWrappers
+ VBoxGlobal_SOURCES       += $(VBoxGlobal_0_OUTDIR)/include/COMWrappers.cpp
+ VBoxGlobal_CLEAN         += $(wildcard $(VBoxGlobal_0_OUTDIR)/include/*) $(PATH_TARGET)/VboxGlobalComWrapperFiles.dep
+
+ ## @todo kmk doesn't remake optional outputs when they are missing...
+ $$(VBoxGlobal_0_OUTDIR)/include/COMWrappers \
+ + $(PATH_TARGET)/VBoxGlobalComWrapperFiles.dep \
+ +| $(if-expr defined(VBoxGlobal_VBOX_COMWRAPPERS) \
+ , $(VBoxGlobal_VBOX_COMWRAPPERS) \
+ , $$(VBoxGlobal_0_OUTDIR)/include/COMEnums.h \
+   $$(VBoxGlobal_0_OUTDIR)/include/COMWrappers.cpp \
+ ): \
+		$(VBOX_XIDL_FILE) \
+		$(VBOX_VIRTUALBOX_SRC)/src/globals/COMWrappers.xsl \
+		$(VBOX_FILESPLIT) \
+		| $$(dir $$@)
+	$(call MSG_GENERATE,VBoxGlobal,$<,$@)
+	$(QUIET)$(RM) -f -- "$@" "$(PATH_TARGET)/VBoxGlobalComWrapperFiles.dep"
+	$(QUIET)$(VBOX_XSLTPROC) -o $@ \
+		$(VBOX_VIRTUALBOX_SRC)/src/globals/COMWrappers.xsl $<
+	$(QUIET)$(VBOX_FILESPLIT) $@ $(VBoxGlobal_0_OUTDIR)/include \
+		$(PATH_TARGET)/VBoxGlobalComWrapperFiles.dep VBoxGlobal_VBOX_COMWRAPPERS
+	$(APPEND) "$@"
+
+ # alias for generating the COM Wrappers file.
+ testwrappers:: $$(VBoxGlobal_0_OUTDIR)/include/COMWrappers
+
+endif
 
 
Index: /trunk/src/VBox/Frontends/VirtualBox/src/globals/COMDefs.h
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/globals/COMDefs.h	(revision 71790)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/globals/COMDefs.h	(revision 71791)
@@ -810,4 +810,199 @@
 };
 
+/**
+ * Partial specialization for CInterface template class above for a case when B == COMBase.
+ *
+ * We had to add it because on exporting template to a library at least on Windows there is
+ * an implicit instantiation of the createInstance() member (even if it's not used) which
+ * in case of base template uses API present in COMBaseWithEI class only, not in COMBase.
+ *
+ * @param  I  Brings the interface class (i.e. derived from IUnknown/nsISupports).
+ */
+template <class I>
+class CInterface<I, COMBase> : public COMBase
+{
+public:
+
+    typedef COMBase Base;
+    typedef I       Iface;
+
+    // constructors & destructor
+
+    CInterface()
+    {
+        clear();
+    }
+
+    CInterface(const CInterface &that) : COMBase(that)
+    {
+        clear();
+        mIface = that.mIface;
+        this->addref((IUnknown*)ptr());
+    }
+
+    CInterface(I *pIface)
+    {
+        clear();
+        setPtr(pIface);
+        this->addref((IUnknown*)pIface);
+    }
+
+    virtual ~CInterface()
+    {
+        detach();
+#ifdef RT_STRICT
+        mDead = true;
+#endif
+    }
+
+    // utility methods
+
+    void createInstance(const CLSID &clsId)
+    {
+        AssertMsg(ptr() == NULL, ("Instance is already non-NULL\n"));
+        if (ptr() == NULL)
+        {
+            I* pObj = NULL;
+#if !defined(VBOX_WITH_XPCOM)
+            COMBase::mRC = CoCreateInstance(clsId, NULL, CLSCTX_ALL,
+                                            COM_IIDOF(I), (void **)&pObj);
+#else
+            nsCOMPtr<nsIComponentManager> manager;
+            COMBase::mRC = NS_GetComponentManager(getter_AddRefs(manager));
+            if (SUCCEEDED(COMBase::mRC))
+                COMBase::mRC = manager->CreateInstance(clsId, nsnull, NS_GET_IID(I),
+                                                       (void **)&pObj);
+#endif
+
+            if (SUCCEEDED(COMBase::mRC))
+               setPtr(pObj);
+            else
+               setPtr(NULL);
+         }
+    }
+
+    /**
+     * Attaches to the given foreign interface pointer by querying the own
+     * interface on it. The operation may fail.
+     */
+    template <class OI>
+    void attach(OI *pIface)
+    {
+        Assert(!mDead);
+        /* Be aware of self assignment: */
+        I *pmIface = ptr();
+        this->addref((IUnknown*)pIface);
+        this->release((IUnknown*)pmIface);
+        if (pIface)
+        {
+            pmIface = NULL;
+            COMBase::mRC = pIface->QueryInterface(COM_IIDOF(I), (void **)&pmIface);
+            this->release((IUnknown*)pIface);
+            setPtr(pmIface);
+        }
+        else
+        {
+            setPtr(NULL);
+            COMBase::mRC = S_OK;
+        }
+    };
+
+    /** Specialization of attach() for our own interface I. Never fails. */
+    void attach(I *pIface)
+    {
+        Assert(!mDead);
+        /* Be aware of self assignment: */
+        this->addref((IUnknown*)pIface);
+        this->release((IUnknown*)ptr());
+        setPtr(pIface);
+        COMBase::mRC = S_OK;
+    };
+
+    /** Detaches from the underlying interface pointer. */
+    void detach()
+    {
+        Assert(!mDead);
+        this->release((IUnknown*)ptr());
+        setPtr(NULL);
+    }
+
+    /** Returns @c true if not attached to any interface pointer. */
+    bool isNull() const
+    {
+        Assert(!mDead);
+        return mIface == NULL;
+    }
+
+    /** Returns @c true if attached to an interface pointer. */
+    bool isNotNull() const
+    {
+        Assert(!mDead);
+        return mIface != NULL;
+    }
+
+    /** Returns @c true if the result code represents success (with or without warnings). */
+    bool isOk() const { return !isNull() && SUCCEEDED(COMBase::mRC); }
+
+    /** Returns @c true if the result code represents success with one or more warnings. */
+    bool isWarning() const { return !isNull() && SUCCEEDED_WARNING(COMBase::mRC); }
+
+    /** Returns @c true if the result code represents success with no warnings. */
+    bool isReallyOk() const { return !isNull() && COMBase::mRC == S_OK; }
+
+    // utility operators
+
+    CInterface &operator=(const CInterface &that)
+    {
+        attach(that.ptr());
+        COMBase::operator=(that);
+        return *this;
+    }
+
+    CInterface &operator=(I *pIface)
+    {
+        attach(pIface);
+        return *this;
+    }
+
+    /**
+     * Returns the raw interface pointer. Not intended to be used for anything
+     * else but in generated wrappers and for debugging. You've been warned.
+     */
+    I *raw() const
+    {
+       return ptr();
+    }
+
+    bool operator==(const CInterface &that) const { return ptr() == that.ptr(); }
+    bool operator!=(const CInterface &that) const { return ptr() != that.ptr(); }
+
+    I *ptr() const
+    {
+        Assert(!mDead);
+        return mIface;
+    }
+
+    void setPtr(I* aObj) const
+    {
+        Assert(!mDead);
+        mIface = aObj;
+    }
+
+private:
+
+#ifdef RT_STRICT
+    bool       mDead;
+#endif
+    mutable I *mIface;
+
+    void clear()
+    {
+       mIface = NULL;
+#ifdef RT_STRICT
+       mDead = false;
+#endif
+    }
+};
+
 /////////////////////////////////////////////////////////////////////////////
 
Index: /trunk/src/VBox/Frontends/VirtualBox/src/globals/COMWrappers.xsl
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/globals/COMWrappers.xsl	(revision 71790)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/globals/COMWrappers.xsl	(revision 71791)
@@ -256,10 +256,4 @@
     <xsl:text> * Generator : src/VBox/Frontends/VirtualBox/src/globals/COMWrappers.xsl&#x0A;</xsl:text>
     <xsl:text> */&#x0A;&#x0A;</xsl:text>
-    <xsl:text>/* VirtualBox interface declarations: */&#x0A;</xsl:text>
-    <xsl:text>#ifndef VBOX_WITH_XPCOM&#x0A;</xsl:text>
-    <xsl:text># include "VirtualBox.h"&#x0A;</xsl:text>
-    <xsl:text>#else /* !VBOX_WITH_XPCOM */&#x0A;</xsl:text>
-    <xsl:text># include "VirtualBox_XPCOM.h"&#x0A;</xsl:text>
-    <xsl:text>#endif /* VBOX_WITH_XPCOM */&#x0A;&#x0A;</xsl:text>
     <xsl:text>/* COM includes: */&#x0A;</xsl:text>
     <xsl:text>#include "COMEnums.h"&#x0A;</xsl:text>
@@ -303,5 +297,12 @@
     <xsl:text>_h__&#x0A;&#x0A;</xsl:text>
     <xsl:text>/* GUI includes: */&#x0A;</xsl:text>
-    <xsl:text>#include "COMDefs.h"&#x0A;&#x0A;</xsl:text>
+    <xsl:text>#include "COMDefs.h"&#x0A;</xsl:text>
+	<xsl:text>#include "UILibraryDefs.h"&#x0A;&#x0A;</xsl:text>
+	<xsl:text>/* VirtualBox interface declarations: */&#x0A;</xsl:text>
+    <xsl:text>#ifndef VBOX_WITH_XPCOM&#x0A;</xsl:text>
+    <xsl:text># include "VirtualBox.h"&#x0A;</xsl:text>
+    <xsl:text>#else /* !VBOX_WITH_XPCOM */&#x0A;</xsl:text>
+    <xsl:text># include "VirtualBox_XPCOM.h"&#x0A;</xsl:text>
+    <xsl:text>#endif /* VBOX_WITH_XPCOM */&#x0A;&#x0A;</xsl:text>
 
     <!-- Forward declarations: -->
@@ -322,5 +323,5 @@
     <!-- Interface wrapper declaration: -->
     <xsl:text>/* Interface wrapper declaration: */&#x0A;</xsl:text>
-    <xsl:text>class C</xsl:text>
+    <xsl:text>class SHARED_LIBRARY_STUFF C</xsl:text>
     <xsl:value-of select="substring(@name,2)"/>
     <xsl:text> : public CInterface&lt;</xsl:text>
