I have a .NET exe compiled for "AnyCPU", so it can theoretically run in both 32-bit and 64-bit mode (on a 64-bit operating system). Since my exe strongly interacts with MS Office, I need to run it with the same bitness as the installed Office version. (Please take this requirement as granted, I will gladly provide details in the comments, if you are interested.)
Now, I have thought up a solution for this, but it seems more complicated than necessary:
- Compile two versions: MyExe32.exe and MyExe64.exe, targeting x86 and x64, respectively, instead of AnyCPU.
- Create a launcher application Launcher.exe, which determines the bitness of the locally installed Office version (I know how to do that) and then starts MyExe32.exe or MyExe64.exe.
This would work, but I'd rather have a single, simple MyExe.exe with something like the following pseudo-code during start-up:
if (Environment.Is64BitProcess && IsOffice32BitInstalled)
{
// restart myself in a 32-bit process instead of a 64-bit process
??? // <-- this is where I need help
}
Is that possible? If yes, how?
Notes: I know about the CorFlags utility, but I'd rather have a solution that does not require modifying the exe itself at runtime.