简体   繁体   中英

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM