4

I created a .NET Core 3.1 AnyCpu Console Application using Visual Studio 2019 with the latest patches. The only code it contains is the boilerplate Console.WriteLine("Hello World!") it was created with. I compile this and it runs fine on my Windows 10 x64 box.

I copy the Debug folder over to my 32-bit Windows 7 box and attempt to run the Console Application. I get the following message. (The .Net Core 3.1 runtime is installed on the Windows 7 box).

The version of this file is not compatible with the version of Windows you're running. Check your computer's system information to see whether you need an x86 (32-bit) or x64 (64-bit) version of the program, and then contact the software publisher.

However, if I compile the Console Application using x86 then it runs fine on the Windows 7 box. With VS2019 and .NET Core 3/3.1 has AnyCpu changed? I would have expected that code compiled for AnyCpu should have worked fine under 32- and 64-bit.

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
making
  • 408
  • 6
  • 21
  • Do you have .Net framework on the windows 7? –  Jul 07 '20 at 11:10
  • Yes, I said that in my original post ("The .Net Core 3.1 runtime is installed on the windows 7 box") and if I compile the project as x86 it works on Windows 7. – making Jul 07 '20 at 11:11
  • You can't just copy from one machine to another. Only when the same version of Net is on Build and Deploy will copying work. When you have different versions of Net you must deploy application which install/updates dlls on deploy machine to match the build machine. – jdweng Jul 07 '20 at 11:12
  • Up until I started using .Net core I've been compiling apps as AnyCpu and copying them no problems. I have made sure the same version of the .Net runtime is installed on both. As I said if I compile the app as x86 it works fine. I'll give deploy a go though. – making Jul 07 '20 at 11:15
  • Compiling and running a published version I still get the same error. It looks to me like it's being compiled for x64 maybe despite being set to AnyCpu. – making Jul 07 '20 at 11:21
  • I read last month the AnyCPU default so 32 bit mode. So I think you are really compiling for 32 and not 64. – jdweng Jul 07 '20 at 12:35
  • If that was the case why do I get the error on 32 bit Windows 7 but it runs fine on 64 but Windows 10. – making Jul 07 '20 at 12:37
  • Does this answer your question? ['Prefer 32-bit' in Visual Studio Generates 64-bit Code Under Console App (.NET Core)](https://stackoverflow.com/questions/60324529/prefer-32-bit-in-visual-studio-generates-64-bit-code-under-console-app-net-c). As it I understand it from reading that answer and a bit of the author's blog, targeting anything other than `x86` will produce a PE32+ `.exe` file, and a PE32+ `.exe` file will be started as a 64-bit process. Not quite sure how it relates, but I found that targeting the `win-x86` runtime when publishing produced an x86 `.exe`, even on 64-bit Windows 10. – Lance U. Matthews Jul 07 '20 at 23:10

1 Answers1

3

It looks like there has been a change in the way AnyCpu works in .Net Core 3. In .NET Framework the .exe was a managed exe so with AnyCpu it JIT'ed at runtime to whatever was required x86 or x64 **. In .Net Core using AnyCpu the .Exe is a plain unmanaged exe, compiled for x86 / x64 depending on what architecture your machine doing the compiling is. However, the dll that it creates contains managed code and can be run on a 32bit machine using DotNet.exe.

The solution is not to compile on a 32bit machine as this just launches the dll in 32bit mode. The solution is actually to not use the .exe and launch the dll with the local copy of DotNet.exe.

** source: https://mihai-albert.com/2020/03/08/startup-sequence-of-a-dotnet-core-app/

Mihai Albert
  • 1,288
  • 1
  • 12
  • 27
making
  • 408
  • 6
  • 21