Search code examples
c#.netdism

Programmatically start a process independent of platform


Situation

I am trying to run a command-line tool, DISM.exe, programmatically. When I run it manually it works, but when I try to spawn it with the following:

var systemPath = Environment.GetFolderPath(Environment.SpecialFolder.System);

var dism = new Process();
dism.StartInfo.FileName = Path.Combine(systemPath, "Dism.exe");
dism.StartInfo.Arguments = "/Online /Get-Features /Format:Table";
dism.StartInfo.Verb = "runas";
dism.StartInfo.UseShellExecute = false;
dism.StartInfo.RedirectStandardOutput = true;

dism.Start();
var result = dism.StandardOutput.ReadToEnd();
dism.WaitForExit();

Then my result comes out as:

Error: 11

You cannot service a running 64-bit operating system with a 32-bit version of DISM. Please use the version of DISM that corresponds to your computer's architecture.

Problem

I'm actually already aware of what causes this: my project is set up to compile for an x86 platform. (See this question for example, although none of the answers mention this). However, unfortunately it is a requirement at the moment that we continue targeting this platform, I am not able to fix this by switching to Any CPU.

So my question is how to programmatically spawn a process in a way which is independent of the platform of its parent- i.e. keep my project targeting x86, but start a process which will target the correct platform for the machine it is on.


Solution

  • even though I'm running the correct DSIM.exe in System32

    But you're not. That's the point. The file system redirector lies to 32-bit processes and so when you ask for System32 from an x86 process, you actually get the file from SysWow64. If you want to access the 64-bit exe, you need to ask for it via %windir%\sysnative

    (%windir% being SpecialFolder.Windows)