AQEMU is my Qt front-end for QEMU on Windows (x64 and ARM64) and Linux. On Windows I ship it through the Microsoft Store as two MSIX packages, one per architecture, and this week those packages taught me a lesson about building both from the same machine.
The symptom was the classic Windows loader error 0xc000007b, STATUS_INVALID_IMAGE_FORMAT. If you've never met it: it usually means a process tried to load a DLL built for a different CPU architecture. Nothing crashes in your code, because your code never gets to run.
How the wrong DLLs got in
My dev machine is a Snapdragon laptop running Windows on Arm, with MSYS2 installed. MSYS2 on Arm has more than one environment side by side: clangarm64 gives native ARM64 toolchains and libraries, and ucrt64 gives x64 ones (which Windows on Arm happily runs under emulation). That's great for building both packages from one box.
It's less great when a packaging script goes looking for runtime DLLs. AQEMU's MSIX staging step collects Qt, SPICE and the rest of the runtime dependencies by searching a list of directories. On a host where both clangarm64\bin and ucrt64\bin exist, a DLL with the right name can be found in the wrong architecture's folder. Same filename, different machine type, and the first match wins.
The result: ARM64 Qt/SPICE DLLs ended up inside the x64 package, and some x64 runtimes ended up in the ARM64 one. Both packages looked complete. File lists matched. Only the loader knew.
Reading the machine type out of the PE header
Every Windows .exe and .dll is a PE file, and the PE header says which CPU it's for. You don't need dumpbin for this; it's a couple of seeks:
- The file starts with
MZ(0x5A4D). - At offset
0x3Cthere's a 32-bit pointer to the PE header. - The PE header starts with
PE\0\0, and the 16-bit Machine field comes right after it.
The values that matter here are 0x8664 (x64), 0xAA64 (ARM64) and 0x14C (x86). In PowerShell that's:
function Get-PeMachineType {
param([Parameter(Mandatory = $true)][string] $Path)
try {
$fs = [IO.File]::OpenRead($Path)
try {
$br = New-Object IO.BinaryReader $fs
if ($fs.Length -lt 0x40) { return "unknown" }
if ($br.ReadUInt16() -ne 0x5A4D) { return "not-PE" }
$fs.Seek(0x3C, 'Begin') | Out-Null
$pe = $br.ReadInt32()
if (($pe + 6) -gt $fs.Length -or $pe -lt 0) { return "bad-PE" }
$fs.Seek($pe + 4, 'Begin') | Out-Null
switch ($br.ReadUInt16()) {
0x8664 { return "x64" }
0xAA64 { return "ARM64" }
0x14c { return "x86" }
default { return "other" }
}
} finally { $fs.Close() }
} catch { return "unreadable" }
}
Three layers of defence
With that function in hand, the fix in installer/build-msix.ps1 has three parts:
- Filter the search paths. When the script looks for a dependency, a candidate whose machine type doesn't match the target architecture is skipped with a warning instead of copied.
- Refuse wrong-arch copies. Every copy into the staging layout goes through a helper that checks the source file first, so a wrong-arch DLL can't slip in through some other code path.
-
Gate the whole layout before packing. Right before
makeappxruns, the script walks every staged.exeand.dlland throws if any of them don't match. The error lists the first 25 offenders with their actual architecture, so you can see at a glance where they came from.
The last one is the important one. The first two stop the specific bug I hit; the gate stops the next one I haven't thought of yet. A package that would fail with 0xc000007b now never gets built at all, and you get a readable list of bad files instead of a mystery on someone else's machine.
(One exception in the gate: the scan skips the WSL helper folder, which isn't something AQEMU loads as part of its own process.)
Takeaways if you ship x64 and ARM64 from one Windows on Arm box
- Treat "same DLL name" as meaningless. Check the machine type.
- Make your packaging script fail loudly at the end, not just pick carefully at the start.
- 0xc000007b almost always means an architecture mix. Before you start debugging your code, look at the DLLs next to it.
What else is in the next update
The packaging fix is part of a bigger AQEMU update that's on its way to the Store. Among the things now in the repo: WHPX detection based on the host AQEMU is actually running on (x64 and ARM64), WinDbg kernel-debug modes per VM (pipe, TCP and KDNET), a fuller qemu-img Tools window (convert, resize, info/check, internal snapshots), importing VMware and VirtualBox VMs, a boot-into-firmware-setup action for UEFI and SeaBIOS, and new VMs defaulting to Documents\Virtual Machines\AQEMU.
AQEMU is GPL-2.0 and the source, including the full build-msix.ps1, is on GitHub: https://github.com/chronic8000/aqemu
If you'd rather not build it yourself, it's on the Microsoft Store as a one-time purchase with a 7-day free trial: https://apps.microsoft.com/detail/9P0HGKHQ9W19?cid=devto
If you've hit 0xc000007b in a different way, I'd like to hear how in the comments.
Top comments (0)