views:

125

answers:

3

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.

+5  A: 

No. C# lets you get away with that because of it's JIT compilation. Your only option is to build to 32 bit and run under WOW64.

tholomew
It's NOT the only option for sure.
n0rd
+5  A: 

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.

Rup
+9  A: 

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.

Chris Schmich