4 rem ---------------------------------------------------------------------------
5 rem setup-windows-7-test-env.bat - prepare a Windows 7 VM as a test target for
6 rem native Windows software driven from the host by VBoxManage guestcontrol.
8 rem Copy this file into the guest and run it there. It is idempotent: run it
9 rem again after a rollback, or after any snapshot restore, to get the same box.
11 rem Split in two halves, like the other provisioning scripts here:
12 rem - the per-user half runs as you and needs no UAC prompt
13 rem - the machine-wide half needs elevation and is SKIPPED (with a notice)
14 rem when this runs unelevated, so an unattended run never blocks on a prompt
16 rem That split matters more than usual here: a guestcontrol-launched process gets
17 rem a UAC-filtered token even for an account in Administrators, so the elevated
18 rem half can only be done by running this from an interactive Administrator
19 rem prompt inside the guest. Everything the host harness actually needs is in
20 rem the per-user half.
21 rem ---------------------------------------------------------------------------
24 echo ============================================================
25 echo Windows 7 test-target setup
26 echo ============================================================
28 rem --- Are we elevated? "net session" is the cheapest reliable probe. ---
31 if "%ERRORLEVEL%"=="0" set "ELEVATED=1"
33 rem === PER-USER HALF (no UAC needed) =========================================
36 echo [user] Suppressing crash/hard-error dialogs
37 rem A modal Windows Error Reporting dialog in the guest blocks a guestcontrol
38 rem call until its timeout: the run looks like a hang, and a harness that scores
39 rem a timeout as a failure will invent bugs that are not there. DontShowUI makes
40 rem a crashing test process die immediately and return its exit code instead.
41 reg add "HKCU\Software\Microsoft\Windows\Windows Error Reporting" /v DontShowUI /t REG_DWORD /d 1 /f >nul
42 if errorlevel 1 echo WARN: could not set DontShowUI
44 echo [user] Disabling the screen saver and monitor blanking
45 rem The host watches this VM's screen with "VBoxManage controlvm screenshotpng".
46 rem A blanked screen makes every screenshot useless.
47 reg add "HKCU\Control Panel\Desktop" /v ScreenSaveActive /t REG_SZ /d 0 /f >nul
48 reg add "HKCU\Control Panel\Desktop" /v ScreenSaverIsSecure /t REG_SZ /d 0 /f >nul
49 reg add "HKCU\Control Panel\Desktop" /v SCRNSAVE.EXE /t REG_SZ /d "" /f >nul 2>&1
51 echo [user] Creating the staging directory C:\bb
52 if not exist "C:\bb" mkdir "C:\bb"
53 if not exist "C:\bb" echo WARN: could not create C:\bb
55 echo [user] Mapping Z: to the VirtualBox shared folder "Downloads"
56 rem Bulk file transfer over a shared folder is far faster than a copyto per file.
57 rem The mapping is per-user and must be re-made in each new interactive session,
58 rem which is exactly why it lives in this script rather than in a host-side note.
59 if exist Z:\ goto :zdone
60 net use Z: \vboxsvr\Downloads /persistent:yes >nul 2>&1
61 if errorlevel 1 echo WARN: could not map Z: - is the shared folder attached to this VM?
64 rem === MACHINE-WIDE HALF (needs elevation) ===================================
66 if "%ELEVATED%"=="0" goto :skipadmin
69 echo [admin] Suppressing the system hard-error dialog (ErrorMode=2)
70 reg add "HKLM\SYSTEM\CurrentControlSet\Control\Windows" /v ErrorMode /t REG_DWORD /d 2 /f >nul
71 if errorlevel 1 echo WARN: could not set ErrorMode
73 echo [admin] Disabling sleep and monitor timeout on AC
74 powercfg -change -monitor-timeout-ac 0 >nul 2>&1
75 powercfg -change -standby-timeout-ac 0 >nul 2>&1
76 powercfg -change -disk-timeout-ac 0 >nul 2>&1
77 powercfg -change -hibernate-timeout-ac 0 >nul 2>&1
79 echo [admin] Turning off Windows Update automatic install
80 rem An update reboot in the middle of a test run destroys the run and, worse,
81 rem silently changes the system under test between two comparable results.
82 reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update" /v AUOptions /t REG_DWORD /d 1 /f >nul 2>&1
87 echo [admin] SKIPPED - this process is not elevated.
88 echo Run this script from an Administrator prompt INSIDE the guest to
89 echo apply: ErrorMode=2, sleep/monitor timeouts, Windows Update policy.
90 echo A guestcontrol-launched process cannot elevate itself, so these
91 echo cannot be done from the host harness.
94 rem === READINESS REPORT ======================================================
95 rem Everything below only reports. A test that silently runs against a box
96 rem missing a device reads as "the software under test refused" when the truth is
97 rem "this VM never had one" - so the harness must know, up front, what is here.
100 echo ============================================================
102 echo ============================================================
106 echo arch=%PROCESSOR_ARCHITECTURE% user=%USERNAME% elevated=%ELEVATED%
109 echo -- Desktop Window Manager composition --
110 rem SetWindowDisplayAffinity requires DWM composition. With composition OFF it
111 rem fails with error 8 for every value, so any screen-capture protection under
112 rem test is a silent no-op and its checks fail for a reason that has nothing to
113 rem do with the code. Windows 7 runs the Basic theme (composition off) whenever
114 rem the install is not activated.
116 rem Ask the API, not a proxy for it. Two plausible-looking shortcuts are both
117 rem WRONG on this box, measured: dwm.exe keeps running with composition off, and
118 rem HKCU\...\DWM\Composition is the stored preference, not the live state. Both
119 rem say ENABLED while DwmIsCompositionEnabled returns false.
121 rem Two shapes to keep, both learned the hard way against guestcontrol:
122 rem - PowerShell PRINTS the verdict; the batch does not capture it. Redirecting
123 rem its stdout to a file, or capturing through for /f, hangs the whole run
124 rem until the host's timeout fires.
125 rem - the if/else is ONE line. Split across two, PowerShell treats the file as
126 rem an incomplete command, waits on stdin and never exits. The <nul below is
127 rem belt and braces for that whole class of hang.
128 set "DWMPS=%TEMP%\w7env-dwm.ps1"
129 > "%DWMPS%" echo Add-Type -TypeDefinition @^"
130 >>"%DWMPS%" echo using System;
131 >>"%DWMPS%" echo using System.Runtime.InteropServices;
132 >>"%DWMPS%" echo public class D { [DllImport("dwmapi.dll")] public static extern int DwmIsCompositionEnabled(out bool e); }
134 >>"%DWMPS%" echo $e = $false
135 >>"%DWMPS%" echo [void][D]::DwmIsCompositionEnabled([ref]$e)
136 >>"%DWMPS%" echo if ($e) { " composition = ENABLED - screen-capture affinity is testable." } else { " composition = DISABLED - SetWindowDisplayAffinity fails with error 8 for every value, so a screen-capture-protection test here is testing nothing. Windows 7 forces the Basic theme while the install is not activated; activate it to test that path." }
137 powershell -NoProfile -ExecutionPolicy Bypass -File "%DWMPS%" <nul
138 del "%DWMPS%" >nul 2>&1
141 echo -- Printers (a print test needs at least one) --
142 rem wmic, not PowerShell: no quoting to get wrong, and it is in the base install.
143 wmic printer get Name,Default /format:table 2>nul | findstr /R /V "^$"
144 if errorlevel 1 echo NONE - print tests will report "could not create a printer DC"
147 echo -- Audio capture devices (a microphone test needs at least one) --
148 wmic sounddev get Name,Status /format:table 2>nul | findstr /R /V "^$"
149 if errorlevel 1 echo NONE - enable audio for this VM in its VirtualBox settings
152 echo -- Shared folder --
153 if exist Z:\ (echo Z: mapped) else (echo Z: NOT mapped)
156 echo -- Staging directory --
157 if exist C:\bb (echo C:\bb present) else (echo C:\bb MISSING)
160 echo ============================================================
161 echo Done. Re-run this after any snapshot restore.
162 echo ============================================================