# Unhandled Exception: System.BadImageFormatException: Could not load file or assembly 'MSBuild.exe' or one of its dependencies. An attempt was made to load a program with an incorrect format.

**URL:** https://discourse.cmake.org/t/unhandled-exception-system-badimageformatexception-could-not-load-file-or-assembly-msbuild-exe-or-one-of-its-dependencies-an-attempt-was-made-to-load-a-program-with-an-incorrect-format/11244
**Category:** Usage
**Tags:** os:windows
**Created:** [July 14, 2024, 9:02am UTC](https://discourse.cmake.org/t/unhandled-exception-system-badimageformatexception-could-not-load-file-or-assembly-msbuild-exe-or-one-of-its-dependencies-an-attempt-was-made-to-load-a-program-with-an-incorrect-format/11244 "2024-07-14T09:02:40Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Zorono](https://discourse.cmake.org/user_avatar/discourse.cmake.org/zorono/32/4762_2.png) [@Zorono](https://discourse.cmake.org/u/Zorono)
#### Post date: [July 14, 2024, 9:02am UTC](https://discourse.cmake.org/t/unhandled-exception-system-badimageformatexception-could-not-load-file-or-assembly-msbuild-exe-or-one-of-its-dependencies-an-attempt-was-made-to-load-a-program-with-an-incorrect-format/11244/1 "2024-07-14T09:02:40Z")

</div>

when i try to build this [samp-gps-plugin/CMakeLists.txt at master · kristoisberg/samp-gps-plugin · GitHub](https://github.com/kristoisberg/samp-gps-plugin/blob/master/CMakeLists.txt) which is built normally with everyone without any problem

```auto
CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):
  Compatibility with CMake < 3.5 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.

-- Selecting Windows SDK version 10.0.22621.0 to target Windows 10.0.19045.
CMake Error at CMakeLists.txt:2 (project):
  Failed to run MSBuild command:

    Z:/Installations/Visual Studio Build Tools/MSBuild/Current/Bin/amd64/MSBuild.exe

  to get the value of VCTargetsPath:

    Unhandled Exception: System.BadImageFormatException: Could not load file or assembly 'MSBuild.exe' or one of its dependencies. An attempt was made to load a program with an incorrect format.

  Exit code: -532462766

-- Configuring incomplete, errors occurred!

```

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [July 14, 2024, 11:13pm UTC](https://discourse.cmake.org/t/unhandled-exception-system-badimageformatexception-could-not-load-file-or-assembly-msbuild-exe-or-one-of-its-dependencies-an-attempt-was-made-to-load-a-program-with-an-incorrect-format/11244/2 "2024-07-14T23:13:44Z")

</div>

What is the command line you’re using to run CMake? If relevant, have you loaded the toolchain environment with vcvars.bat or equivalent? What version(s) of Visual Studio do you have installed?

---

<div class="post-metadata">

### Author: ![Zorono](https://discourse.cmake.org/user_avatar/discourse.cmake.org/zorono/32/4762_2.png) [@Zorono](https://discourse.cmake.org/u/Zorono)
#### Post date: [July 15, 2024, 6:11am UTC](https://discourse.cmake.org/t/unhandled-exception-system-badimageformatexception-could-not-load-file-or-assembly-msbuild-exe-or-one-of-its-dependencies-an-attempt-was-made-to-load-a-program-with-an-incorrect-format/11244/3 "2024-07-15T06:11:56Z")

</div>

**What is the: command line you’re using to run CMake?**

```auto
git clone ...
cd ...
mkdir ./build
cmake -B ./build -DCMAKE_BUILD_TYPE=Release -A Win32

```

**have you loaded the toolchain environment with vcvars.bat or equivalent?**  
No.

**What version(s) of Visual Studio do you have installed?**  
Visual Studio Community 2022 (v17.10.4)

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [July 15, 2024, 10:22pm UTC](https://discourse.cmake.org/t/unhandled-exception-system-badimageformatexception-could-not-load-file-or-assembly-msbuild-exe-or-one-of-its-dependencies-an-attempt-was-made-to-load-a-program-with-an-incorrect-format/11244/4 "2024-07-15T22:22:55Z")

</div>

If you are using the Visual Studio toolchain, you need to load the appropriate toolchain environment with vcvars.bat or equivalent.
