Search code examples
visual-studio64-bitdebuggervisualizer

Visual Studio custom data visualizer throws BadImageFormatException


In our c# code base (hence managed code), we have a class that we make extensive use of throughout the code. Given its ubiquity, I decided to write a custom debugger visualizer so that we could easily examine such objects when debugging. But, I hit a snag - when I try to run the visualizer in the IDE, I get a BadImageFormatException.

I am posting this to help others who come across the same error. I know what the issue and solution is and will post.


Solution

  • Currently (as of Visual Studio 2019) it's possible to split the visualizer into two:

    1. debuggee-side DLL -- gets injected into the target process, and
    2. debugger-side DLL -- loaded into Visual Studio.

    The two halves pass data between each other using serialization/deserialization.

    This architecture is required for visualizers to target multiple frameworks -- the debugger side is loaded into Visual Studio, so it must target .NET Framework; the debuggee side is injected into the target process, which might target .NET Core or .NET 5+. (I refer you to this repo for a minimal visualizer with this structure; and to other visualizers I've written (1 2) which also use a similar architecture.)

    The same architecture works for bit-ness. Visual Studio is a 32-bit application, so the debugger side cannot be 64-bit; it must be 32-bit or AnyCPU. But if the target process might be 64-bit, the debuggee side has to match the target process and must be 64-bit or AnyCPU.

    Per the docs:

    Typically, it is best if both the debugger-side DLL and the debuggee-side DLL specify Any CPU as the target platform. The debugger-side DLL must be either Any CPU or 32-bit. The target platform for the debuggee-side DLL should correspond to the debugee process.