Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Upgrading to Visual Studio 2022 breaks the x86 kernel compile due to 64-bit MSBuild #109

Open
FrankRay78 opened this issue Nov 21, 2023 · 0 comments

Comments

@FrankRay78
Copy link

FrankRay78 commented Nov 21, 2023

By default, Visual Studio 2022 uses a 64-bit MSBuild, unlike earlier versions that used a 32-bit MSBuild. When I follow the instructions here, step 6. 'The OS should now build.' fails with the following message:

11>D:\Source\Repos\FlingOS\Kernel\Kernel\bin\Debug\Kernel.dll : error LDR0003: Library Loader encountered an unexpected error. Could not load file or assembly 'file:///D:\Source\Repos\FlingOS\Kernel\Kernel\bin\Debug\Kernel.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.
11>D:\Source\Repos\FlingOS\Kernel\Kernel\bin\Debug\Kernel.dll : error LDR0003:    at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
11>D:\Source\Repos\FlingOS\Kernel\Kernel\bin\Debug\Kernel.dll : error LDR0003:    at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
11>D:\Source\Repos\FlingOS\Kernel\Kernel\bin\Debug\Kernel.dll : error LDR0003:    at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
11>D:\Source\Repos\FlingOS\Kernel\Kernel\bin\Debug\Kernel.dll : error LDR0003:    at System.Reflection.Assembly.LoadFrom(String assemblyFile)
11>D:\Source\Repos\FlingOS\Kernel\Kernel\bin\Debug\Kernel.dll : error LDR0003:    at Drivers.Compiler.LibraryLoader.LoadILLibraryFromFile(String FilePath) in D:\Source\Repos\FlingOS\Drivers\Compiler\LibraryLoader.cs:line 74

An attempt was made to load a program with an incorrect format.

What is happening is the MSBuild task, built under configuration Any CPU, is being run as a 64-bit process by Visual Studio. However, the Kernel binary, built under configuration x86, is a 32-bit file. Hence the 'incorrect format' exception.

This exact issue was written about in a Microsoft blog, here: https://devblogs.microsoft.com/dotnet/msbuild-and-64-bit-visual-studio-2022/

The solution here is to run the 32-bit MSBuild task instead. Several ways exist to do that, in my case I added Architecture="x86" to the following lines in Kernel.csproj ie.

<UsingTask Architecture="x86" Condition="'$(Configuration)' == 'Debug'" AssemblyFile="$(SolutionDir)Drivers\Compiler\MSBuildTask\bin\Debug\Drivers.Compiler.MSBuildTask.dll" TaskName="MSBuildTask.BuildTask" />
<UsingTask Architecture="x86" Condition="'$(Configuration)' == 'Release'" AssemblyFile="$(SolutionDir)Drivers\Compiler\MSBuildTask\bin\Release\Drivers.Compiler.MSBuildTask.dll" TaskName="MSBuildTask.BuildTask" />

Microsoft guidance specifically says, 'This change is backward compatible... we recommend making this change unconditionally and not trying to detect Visual Studio 2022'

The x86/32-bit kernel now compiles successfully.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant