Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we create a VC++ executable which will work natively on both 32 bit and 64 bit Windows?

Is there any way to build a VC++ project so that the dll/exe created by it will work as a 32 bit application on a 32 bit Windows OS and as a 64 bit application on a 64 bit Windows OS (not in WOW64).

I know that is possible for C# applications using the /ANYCPU option.

like image 378
Ganesh R. Avatar asked Aug 28 '10 13:08

Ganesh R.


People also ask

How do I change my Visual Studio from 32 bit to 64-bit?

From the BUILD menu in Visual Studio, select Configuration Manager. From the Active solution platform drop-down list, select New. The New Solution Platform dialog displays. In the Type or select new platform combination box, select x64.

How do I run Visual Studio in 64-bit mode?

From the Visual Studio menu, choose Test, then choose Processor Architecture for AnyCPU projects. Choose x64 to run the tests as a 64-bit process.

Is Visual Studio 2022 64-bit only?

Visual Studio 2022 is 64-bit Visual Studio 2022 on Windows is now a 64-bit application. This means you can open, edit, run, and debug even the biggest and most complex solutions without running out of memory.


2 Answers

The CLR has special loader support for the /ANYCPU option.

If you really want to do this for native, the best way to do it is to:

  1. Build your binary for both 32- and 64-bit
  2. As part of building the 32-bit binary, include the 64-bit binary as a resource
  3. On 32-bit machines, just run the 32-bit binary
  4. On 64-bit machines, when the 32-bit binary runs, unpack the 64-bit binary resource, write it to disk, and run it from there

This is how the Sysinternals tools work (download Process Explorer onto a 64-bit machine and run it: you'll see that it writes procexp64.exe to disk and then runs it from there). It's a hack, but it works.

like image 147
Chris Schmich Avatar answered Oct 02 '22 15:10

Chris Schmich


Not AFAIK - the problem is that

  1. you'll need separate code; this works for C# because you're generating .NET IL which gets converted to native code on the target system
  2. the windows PE format only has one image header and no way to chain through to another header later on to put both sets of code in the same library.

The best you could do for an .EXE was to ship a 32-bit .exe that checks if it's running WOW64 then spawns the 64-bit version instead. I can't think of an equivalent trick for libraries, though - it has to match the bits of the host process to load in the first place.

like image 33
Rup Avatar answered Oct 02 '22 17:10

Rup