Index: /trunk/src/VBox/Additions/WINNT/Installer/VBoxGuestAdditions.nsi
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Installer/VBoxGuestAdditions.nsi	(revision 50420)
+++ /trunk/src/VBox/Additions/WINNT/Installer/VBoxGuestAdditions.nsi	(revision 50421)
@@ -787,39 +787,4 @@
 SectionEnd
 
-; Prepares the access rights for replacing
-; a WRP (Windows Resource Protection) protected file
-Function PrepareWRPFile
-
-  Pop $0
-
-  ${IfNot} ${FileExists} "$0"
-    ${LogVerbose} "WRP: File $0 does not exist, skipping"
-    Return
-  ${EndIf}
-
-  ${If} ${FileExists} "$g_strSystemDir\takeown.exe"
-    ${CmdExecute} "$\"$g_strSystemDir\takeown.exe$\" /F $\"$0$\"" "true"
-  ${Else}
-    ${LogVerbose} "WRP: Warning: takeown.exe not found, skipping"
-  ${EndIf}
-
-  AccessControl::SetFileOwner "$0" "(S-1-5-32-545)"
-  Pop $1
-  ${LogVerbose} "WRP: Setting file owner for $0 returned: $1"
-
-  AccessControl::GrantOnFile "$0" "(S-1-5-32-545)" "FullAccess"
-  Pop $1
-  ${LogVerbose} "WRP: Setting access rights for $0 returned: $1"
-
-!if $%VBOX_WITH_GUEST_INSTALL_HELPER% == "1"
-  !ifdef WFP_FILE_EXCEPTION
-    VBoxGuestInstallHelper::DisableWFP "$0"
-    Pop $1 ; Get return value (ignored for now)
-    ${LogVerbose} "WRP: Setting WFP exception for $0 returned: $1"
-  !endif
-!endif
-
-FunctionEnd
-
 ; Direct3D support
 Section /o $(VBOX_COMPONENT_D3D) SEC03
@@ -827,4 +792,6 @@
 !if $%VBOX_WITH_WDDM% == "1"
   ${If} $g_bWithWDDM == "true"
+    ${LogVerbose} "Installing WDDM Direct3D support ..."
+
     ; Do we need to restore the original d3d8.dll/d3d9.dll files because the guest
     ; installation was upgraded from XPDM to WDDM driver? In a XPDM installation
@@ -835,4 +802,5 @@
 !endif
 
+  Call SetAppMode64
   SetOverwrite on
 
@@ -842,5 +810,5 @@
 
   SetOutPath $g_strSystemDir
-  ${LogVerbose} "Installing Direct3D support ..."
+  ${LogVerbose} "Installing XPDM Direct3D support ..."
   FILE "$%PATH_OUT%\bin\additions\VBoxD3D8.dll"
   FILE "$%PATH_OUT%\bin\additions\VBoxD3D9.dll"
@@ -856,12 +824,8 @@
       ${CopyFileEx} "" "$g_strSystemDir\dllcache\d3d9.dll" "$g_strSystemDir\dllcache\msd3d9.dll" "Microsoft Corporation" "$%BUILD_TARGET_ARCH%"
 
-      Push "$g_strSystemDir\dllcache\d3d8.dll"
-      Call PrepareWRPFile
-
-      Push "$g_strSystemDir\dllcache\d3d9.dll"
-      Call PrepareWRPFile
-
       ; Exchange DLLs
+      ${PrepareWRPFileEx} "" "$g_strSystemDir\dllcache\d3d8.dll"
       ${InstallFileEx} "" "$%PATH_OUT%\bin\additions\d3d8.dll" "$g_strSystemDir\dllcache\d3d8.dll" "$TEMP"
+      ${PrepareWRPFileEx} "" "$g_strSystemDir\dllcache\d3d9.dll"
       ${InstallFileEx} "" "$%PATH_OUT%\bin\additions\d3d9.dll" "$g_strSystemDir\dllcache\d3d9.dll" "$TEMP"
     ${Else}
@@ -877,12 +841,8 @@
   ${CopyFileEx} "" "$g_strSystemDir\d3d9.dll" "$g_strSystemDir\msd3d9.dll" "Microsoft Corporation" "$%BUILD_TARGET_ARCH%"
 
-  Push "$g_strSystemDir\d3d8.dll"
-  Call PrepareWRPFile
-
-  Push "$g_strSystemDir\d3d9.dll"
-  Call PrepareWRPFile
-
   ; Exchange DLLs
+  ${PrepareWRPFileEx} "" "$g_strSystemDir\d3d8.dll"
   ${InstallFileEx} "" "$%PATH_OUT%\bin\additions\d3d8.dll" "$g_strSystemDir\d3d8.dll" "$TEMP"
+  ${PrepareWRPFileEx} "" "$g_strSystemDir\d3d9.dll"
   ${InstallFileEx} "" "$%PATH_OUT%\bin\additions\d3d9.dll" "$g_strSystemDir\d3d9.dll" "$TEMP"
 
@@ -905,12 +865,8 @@
         ${CopyFileEx} "" "$g_strSysWow64\dllcache\d3d9.dll" "$g_strSysWow64\dllcache\msd3d9.dll" "Microsoft Corporation" "x86"
 
-        Push "$g_strSysWow64\dllcache\d3d8.dll"
-        Call PrepareWRPFile
-
-        Push "$g_strSysWow64\dllcache\d3d9.dll"
-        Call PrepareWRPFile
-
         ; Exchange DLLs
+        ${PrepareWRPFileEx} "" "$g_strSysWow64\dllcache\d3d8.dll"
         ${InstallFileEx} "" "$%VBOX_PATH_ADDITIONS_WIN_X86%\d3d8.dll" "$g_strSysWow64\dllcache\d3d8.dll" "$TEMP"
+        ${PrepareWRPFileEx} "" "$g_strSysWow64\dllcache\d3d9.dll"
         ${InstallFileEx} "" "$%VBOX_PATH_ADDITIONS_WIN_X86%\d3d9.dll" "$g_strSysWow64\dllcache\d3d9.dll" "$TEMP"
       ${Else}
@@ -927,12 +883,7 @@
     ${CopyFileEx} "" "$g_strSysWow64\d3d9.dll" "$g_strSysWow64\msd3d9.dll" "Microsoft Corporation" "x86"
 
-    Push "$g_strSysWow64\d3d8.dll"
-    Call PrepareWRPFile
-
-    Push "$g_strSysWow64\d3d9.dll"
-    Call PrepareWRPFile
-
-    ; Exchange DLLs
+    ${PrepareWRPFileEx} "" "$g_strSysWow64\d3d8.dll"
     ${InstallFileEx} "" "$%VBOX_PATH_ADDITIONS_WIN_X86%\d3d8.dll" "$g_strSysWow64\d3d8.dll" "$TEMP"
+    ${PrepareWRPFileEx} "" "$g_strSysWow64\d3d9.dll"
     ${InstallFileEx} "" "$%VBOX_PATH_ADDITIONS_WIN_X86%\d3d9.dll" "$g_strSysWow64\d3d9.dll" "$TEMP"
 
Index: /trunk/src/VBox/Additions/WINNT/Installer/VBoxGuestAdditionsCommon.nsh
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Installer/VBoxGuestAdditionsCommon.nsh	(revision 50420)
+++ /trunk/src/VBox/Additions/WINNT/Installer/VBoxGuestAdditionsCommon.nsh	(revision 50421)
@@ -823,6 +823,6 @@
 ; @return  Stack: "0" if copied, "1" if not, "2" on error / not found.
 ; @param   Un/Installer prefix; either "" or "un".
-; @param   Name of lib/DLL to verify and copy to destination.
-; @param   Destination name to copy verified file to.
+; @param   Name of lib/DLL to copy to destination.
+; @param   Destination name to copy the source file to.
 ; @param   Temporary folder used for exchanging the (locked) lib/DLL after a reboot.
 ;
@@ -1041,22 +1041,31 @@
   ;         in SysWOW64 (or in system32 on 32-bit systems).
 
+  ${LogVerbose} "Restoring original D3D files ..."
 !if $%BUILD_TARGET_ARCH% == "x86"
+  $PrepareWRPFileEx} "${un}" "$SYSDIR\d3d8.dll"
   ${CopyFileEx} "${un}" "$SYSDIR\msd3d8.dll" "$SYSDIR\d3d8.dll" "Microsoft Corporation" "$%BUILD_TARGET_ARCH%"
 !endif
+  $PrepareWRPFileEx} "${un}" "$SYSDIR\d3d9.dll"
   ${CopyFileEx} "${un}" "$SYSDIR\msd3d9.dll" "$SYSDIR\d3d9.dll" "Microsoft Corporation" "$%BUILD_TARGET_ARCH%"
 
   ${If} $g_bCapDllCache == "true"
 !if $%BUILD_TARGET_ARCH% == "x86"
+    $PrepareWRPFileEx} "${un}" "$SYSDIR\dllcache\d3d8.dll"
     ${CopyFileEx} "${un}" "$SYSDIR\dllcache\msd3d8.dll" "$SYSDIR\dllcache\d3d8.dll" "Microsoft Corporation" "$%BUILD_TARGET_ARCH%"
 !endif
+    $PrepareWRPFileEx} "${un}" "$SYSDIR\dllcache\d3d9.dll"
     ${CopyFileEx} "${un}" "$SYSDIR\dllcache\msd3d9.dll" "$SYSDIR\dllcache\d3d9.dll" "Microsoft Corporation" "$%BUILD_TARGET_ARCH%"
   ${EndIf}
 
 !if $%BUILD_TARGET_ARCH% == "amd64"
+  $PrepareWRPFileEx} "${un}" "$g_strSysWow64\d3d8.dll"
   ${CopyFileEx} "${un}" "$g_strSysWow64\msd3d8.dll" "$g_strSysWow64\d3d8.dll" "Microsoft Corporation" "x86"
+  $PrepareWRPFileEx} "${un}" "$g_strSysWow64\d3d9.dll"
   ${CopyFileEx} "${un}" "$g_strSysWow64\msd3d9.dll" "$g_strSysWow64\d3d9.dll" "Microsoft Corporation" "x86"
 
   ${If} $g_bCapDllCache == "true"
+    $PrepareWRPFileEx} "${un}" "$g_strSysWow64\dllcache\d3d8.dll"
     ${CopyFileEx} "${un}" "$g_strSysWow64\dllcache\msd3d8.dll" "$g_strSysWow64\dllcache\d3d8.dll" "Microsoft Corporation" "x86"
+    $PrepareWRPFileEx} "${un}" "$g_strSysWow64\dllcache\d3d9.dll"
     ${CopyFileEx} "${un}" "$g_strSysWow64\dllcache\msd3d9.dll" "$g_strSysWow64\dllcache\d3d9.dll" "Microsoft Corporation" "x86"
   ${EndIf}
@@ -1077,2 +1086,58 @@
 !insertmacro RestoreFilesDirect3D ""
 !insertmacro RestoreFilesDirect3D "un."
+
+; Prepares the access rights for replacing
+; a WRP (Windows Resource Protection) protected file
+!macro PrepareWRPFile un
+Function ${un}PrepareWRPFile
+
+  Pop $0
+  Push $1
+
+  ${IfNot} ${FileExists} "$0"
+    ${LogVerbose} "WRP: File $\"$0$\" does not exist, skipping"
+    Return
+  ${EndIf}
+
+  ${If} ${FileExists} "$g_strSystemDir\takeown.exe"
+    ${CmdExecute} "$\"$g_strSystemDir\takeown.exe$\" /F $\"$0$\"" "true"
+  ${Else}
+    ${LogVerbose} "WRP: Warning: takeown.exe not found, skipping"
+  ${EndIf}
+
+  AccessControl::SetFileOwner "$0" "(S-1-5-32-545)"
+  Pop $1
+  ${LogVerbose} "WRP: Setting file owner for $\"$0$\" returned: $1"
+
+  AccessControl::GrantOnFile "$0" "(S-1-5-32-545)" "FullAccess"
+  Pop $1
+  ${LogVerbose} "WRP: Setting access rights for $\"$0$\" returned: $1"
+
+!if $%VBOX_WITH_GUEST_INSTALL_HELPER% == "1"
+  !ifdef WFP_FILE_EXCEPTION
+    VBoxGuestInstallHelper::DisableWFP "$0"
+    Pop $1 ; Get return value (ignored for now)
+    ${LogVerbose} "WRP: Setting WFP exception for $\"$0$\" returned: $1"
+  !endif
+!endif
+
+  Pop $1
+
+FunctionEnd
+!macroend
+!insertmacro PrepareWRPFile ""
+!insertmacro PrepareWRPFile "un."
+
+;
+; Macro for preparing the access rights for replacing
+; a WRP (Windows Resource Protection) protected file.
+; @return  None.
+; @param   Path of file to prepare.
+;
+!macro PrepareWRPFileEx un FileSrc
+  Push $0
+  Push "${FileSrc}"
+  Call ${un}PrepareWRPFile
+  Pop $0
+!macroend
+!define PrepareWRPFileEx "!insertmacro PrepareWRPFileEx"
