Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically start a process independent of platform

Tags:

c#

.net

dism

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.

like image 324
Ben Aaronson Avatar asked May 15 '15 13:05

Ben Aaronson


2 Answers

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)

like image 53
Damien_The_Unbeliever Avatar answered Sep 21 '22 23:09

Damien_The_Unbeliever


While it's not answering your question about starting a 64 bit process from a 32 bit, an alternative approach to your underlying problem would be to query WMI to obtain the information you require. You can list optional features or list Server Features

This answer gives general information about performing a WMI query from C#.

You can also check and install windows features from powershell, which you might be able to spawn from your program instead of starting DISM.

like image 45
Richard Avatar answered Sep 21 '22 23:09

Richard