Index: /trunk/src/VBox/Installer/win/Makefile.kmk
===================================================================
--- /trunk/src/VBox/Installer/win/Makefile.kmk	(revision 59328)
+++ /trunk/src/VBox/Installer/win/Makefile.kmk	(revision 59329)
@@ -658,14 +658,38 @@
 		$(PATH_SUB_CURRENT)/VirtualBox_TypeLib.xsl \
 		$(VBOX_XIDL_FILE) \
+		$(MAKEFILE) \
+               $(if-expr defined(VBOX_WITH_MIDL_PROXY_STUB),$(PATH_STAGE_BIN)/VBoxProxyStub$(if-expr "$(KBUILD_TARGET_ARCH)" == "amd64",-x86,).dll,) \
 		| $$(dir $$@)
+ifndef VBOX_WITH_MIDL_PROXY_STUB
 	$(VBOX_XSLTPROC) -o $@ $< $(VBOX_XIDL_FILE)
+else
+	$(VBOX_XSLTPROC) -o "$@.xidl" $< $(VBOX_XIDL_FILE)
+	$(VBOX_PATH_WIX)/heat.exe \
+		file $(PATH_STAGE_BIN)/VBoxProxyStub$(if-expr "$(KBUILD_TARGET_ARCH)" == "amd64",-x86,).dll \
+		-nologo -o $@.heat
+	$(SED) -e '/\<Interface Id/!d' -e 's/^[[:space:]]*/    /' $@.heat --output $@.interfaces
+	$(SED) -e '/<\/AppId>/{' -e 'r $@.interfaces' -e '}' "$@.xidl" --output $@
+	$(RM) -f -- "$@.xidl" "$@.heat" "$@.interfaces"
+endif
 
 $(VBOX_WIN_INST_OUT_DIR)/VirtualBox_TypeLib_x86.wxi: \
 		$(PATH_SUB_CURRENT)/VirtualBox_TypeLib.xsl \
 		$(VBOX_XIDL_FILE) \
+               $(if-expr defined(VBOX_WITH_MIDL_PROXY_STUB),$(PATH_STAGE_BIN)/VBoxProxyStub-x86.dll,) \
 		| $$(dir $$@)
-	$(VBOX_XSLTPROC) --stringparam "a_sTarget" "VBoxClient-x86"  -o $@.tmp $< $(VBOX_XIDL_FILE)
+ifndef VBOX_WITH_MIDL_PROXY_STUB
+	$(VBOX_XSLTPROC) --stringparam "a_sTarget" "VBoxClient-x86" -o $@.tmp $< $(VBOX_XIDL_FILE)
 	$(SED) -e 's/VBoxC/VBoxClient_x86/g' --output $@ $@.tmp
 	$(RM) -f $@.tmp
+else
+	$(VBOX_XSLTPROC) --stringparam "a_sTarget" "VBoxClient-x86" -o "$@.xidl-pre" $< $(VBOX_XIDL_FILE)
+	$(SED) -e 's/VBoxC/VBoxClient_x86/g' --output "$@.xidl" "$@.xidl-pre"
+	$(VBOX_PATH_WIX)/heat.exe \
+		file $(PATH_STAGE_BIN)/VBoxProxyStub-x86.dll \
+		-nologo -o $@.heat
+	$(SED) -e '/\<Interface Id/!d' -e 's/^[[:space:]]*/    /' $@.heat --output $@.interfaces
+	$(SED) -e '/<\/AppId>/{' -e 'r $@.interfaces' -e '}' $@.xidl --output $@
+	$(RM) -f -- "$@.xidl-pre" "$@.xidl" "$@.tmp2" "$@.heat" "$@.interfaces"
+endif
 
 #
Index: /trunk/src/VBox/Installer/win/VBoxMergeApp.wxi
===================================================================
--- /trunk/src/VBox/Installer/win/VBoxMergeApp.wxi	(revision 59328)
+++ /trunk/src/VBox/Installer/win/VBoxMergeApp.wxi	(revision 59329)
@@ -64,11 +64,27 @@
         </File>
 
+        <!-- Note: This file ID *must not* be changed because of our typelib template generation file! -->
+<?if $(env.VBOX_WITH_MIDL_PROXY_STUB) != "yes" ?>
         <!-- We set KeyPath on this file to instruct TypeLib to read the TLB resource from it
              and create appropriate Interface registry entries. Note that the same TLB is present
              in VBoxSVC.exe - it's just a matter of choice which one to use -->
-        <!-- Note: This file ID *must not* be changed because of our typelib template generation file! -->
         <File Id="VBoxC" Name="VBoxC.dll"
               Source="$(env.PATH_OUT)\bin\VBoxC.dll" KeyPath="yes">
         </File>
+<?else?>
+        <File Id="VBoxC" Name="VBoxC.dll"
+              Source="$(env.PATH_OUT)\bin\VBoxC.dll" KeyPath="no">
+        </File>
+
+        <!-- We put the typelib in the proxy stub since it's always required to be loaded and is
+             significantly smaller than VBoxC.dll.  So, KeyPath for this element instead of the above. -->
+        <File Id="file_VBoxProxyStub" Name="VBoxProxyStub.dll" KeyPath="yes"
+              Source="$(env.PATH_OUT)\bin\VBoxProxyStub.dll">
+            <!-- TODO: figure how 327E3C00-EE61-462F-AED3-0DFF6CBF9904 is selected. -->
+            <Class Id="{327E3C00-EE61-462F-AED3-0DFF6CBF9904}" Context="InprocServer32"
+                Description="PSFactoryBuffer" ThreadingModel="both" >
+            </Class>
+        </File>
+<?endif?>
 
         <!-- Include the auto-generated TypeLib block -->
Index: /trunk/src/VBox/Installer/win/VBoxMergeAppSeq.wxi
===================================================================
--- /trunk/src/VBox/Installer/win/VBoxMergeAppSeq.wxi	(revision 59328)
+++ /trunk/src/VBox/Installer/win/VBoxMergeAppSeq.wxi	(revision 59329)
@@ -3,5 +3,5 @@
     VirtualBox Windows Installation Script (WiX)
 
-    Copyright (C) 2006-2015 Oracle Corporation
+    Copyright (C) 2006-2016 Oracle Corporation
 
     This file is part of VirtualBox Open Source Edition (OSE), as
@@ -17,3 +17,39 @@
          xmlns:difxapp="http://schemas.microsoft.com/wix/DifxAppExtension">
 
+<?if $(env.VBOX_WITH_MIDL_PROXY_STUB) = "yes" ?>
+    <!--
+        Reverse the typelib and registry writing order to make proxy stub work.
+
+        A few things are ganging up on us here:
+            - A Typelib element is translated into a typelib table entry.
+            - While a Interface element is translated into a registry table
+              entry.
+            - The typelib table is by default processed after writing the
+              registry table.
+            - The typelib processing will set the ProxyStubClsid32 to a
+              dynamic builtin one that uses the typelib, thus overwriting
+              that bit of intformation from our interface elements (the
+              NumMethods bit is kept, go figure).
+           (- If we set the Advertise attribute on the Typelib element to
+              no, we may get a bit futher with ProxyStubClsid32, but we
+              end up with VBoxC being using instead of VBoxSVC. At least
+              in one of the configurations attempted)
+
+        The whole point of the proxy stub DLLs is to not use the default
+        dynamic proxy stub from OLE that loads+parses the typelib each time
+        it creates proxies/stubs.  There are two workarounds:
+            1. Don't use the typelib and interface elements, but instead emit
+               plain registry entries (heat.exe can help).
+            2. Make the registry writing happen after the typelib registration.
+
+        Going with option two for now.
+
+        ASSUMES RegisterTypeLibraries has sequence number 5500 and
+        WriteRegistryValues 5000.  Modifies both.
+     -->
+    <RegisterTypeLibraries Sequence="4999"/>
+    <WriteRegistryValues Sequence="5501"/>
+<?endif?>
+
 </Include>
+
Index: /trunk/src/VBox/Installer/win/VBoxMergeCOM32On64.wxi
===================================================================
--- /trunk/src/VBox/Installer/win/VBoxMergeCOM32On64.wxi	(revision 59328)
+++ /trunk/src/VBox/Installer/win/VBoxMergeCOM32On64.wxi	(revision 59329)
@@ -19,11 +19,25 @@
 <?if $(env.VBOX_WITH_32_ON_64_MAIN_API) = "yes" ?>
     <Directory Id="dir_VBoxCOM32On64X86" Name="x86">
-        <!-- The 32-bit client COM component (see also cp_MainCom below) -->
+        <!-- The 32-bit client COM component (see also cp_MainCom in VBoxMergeApp.wxi) -->
         <Component Id="cp_MainCOM_x86" Guid="B600824E-4A25-2EB3-4B44-3D8CB7F9B92D" Win64="no">
             <!-- Note: This file ID *must not* be changed because of our typelib template generation file! -->
+    <?if $(env.VBOX_WITH_MIDL_PROXY_STUB) != "yes" ?>
             <File Id="VBoxClient_x86" Name="VBoxClient-x86.dll"
                   Source="$(env.PATH_OUT)\bin\x86\VBoxClient-x86.dll" KeyPath="yes"
                   DiskId="$(var.Property_DiskIdCommon)">
             </File>
+    <?else?>
+            <File Id="VBoxClient_x86" Name="VBoxClient-x86.dll"
+                  Source="$(env.PATH_OUT)\bin\x86\VBoxClient-x86.dll"
+                  DiskId="$(var.Property_DiskIdCommon)">
+            </File>
+            <!-- Our key path, see VBoxMergeApp.wxi. -->
+            <File Id="file_VBoxProxyStub_x86" Name="VBoxProxyStub-x86.dll" KeyPath="yes"
+                Source="$(env.PATH_OUT)\bin\VBoxProxyStub-x86.dll"
+                DiskId="$(var.Property_DiskIdCommon)">
+                <Class Id="{327E3C00-EE61-462F-AED3-0DFF6CBF9904}" Context="InprocServer32"
+                    Description="PSFactoryBuffer" ThreadingModel="both" />
+            </File>
+    <?endif?>
             <File Id="file_VBoxRT_x86" Name="VBoxRT-x86.dll"
                   Source="$(env.PATH_OUT)\bin\x86\VBoxRT-x86.dll"
