6

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.

Community
  • 1
  • 1
Ben Aaronson
  • 6,955
  • 2
  • 23
  • 38
  • Without knowing why you can't target Any CPU this suggestion risks sounding stupid, but can you write a small program that sits in the middle and spawns DISM.exe by targeting Any CPU and your main program calls it? – Crowcoder May 15 '15 at 13:59
  • @Crowcoder It may be an option, but I'm not actually sure that will work. – Ben Aaronson May 15 '15 at 14:03
  • I have just found the opposite question (for some reason) and one of the answers points to a tool which by the sounds of it can allow you to run 32 bit processes on a 64 bit operating system. Here is the question: http://stackoverflow.com/questions/8489871/64-bits-application-starting-32-bits-process and here is the tool they suggest http://www.techsupportalert.com/content/how-windows7-vista64-support-32bit-applications.htm – Keithin8a May 15 '15 at 14:03
  • If my comment helps you let me know and I will make it an answer. I didn't want to make it an answer because this is the first time I have seen this so I cannot verify if it would work, it just sounds like it would. – Keithin8a May 15 '15 at 14:04
  • @Keithin8a For reasons I don't understand, DSIM.exe is extremely sniffy about 32 vs 64 bit processes. So even though I'm running the correct DSIM.exe in System32 (not SysWOW64), it's still giving me the message in the question. I'm not exactly sure what your comment is suggesting beyond what I'm already trying as described in the question. – Ben Aaronson May 15 '15 at 14:07
  • @Richard Try deselecting "Prefer 32-bit" inside the project Build options. – Ben Aaronson May 15 '15 at 14:09
  • @BenAaronson I was only able to have a quick look at that web page (because I am in work) and thought it was a sort of emulator that you could download and have running to allow you to run a 32 bit version. But on closer inspection it isn't. – Keithin8a May 15 '15 at 14:10
  • Maybe its worth to try to not specify runas as verb but to actually start runas as process and give dism as argument to runas. – Ralf May 15 '15 at 14:13

2 Answers2

4

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)

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
2

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.

Community
  • 1
  • 1
Richard
  • 29,854
  • 11
  • 77
  • 120
  • Just tried this and it works great, but I'm slightly leery of accepting it because it's so unrelated to this question. Would you be okay if I created a new question more applicable to this and you moved the answer across? – Ben Aaronson May 15 '15 at 14:22
  • Don't worry about the additional question, there are others already answering it, such as http://stackoverflow.com/a/7039227/163495 – Richard May 15 '15 at 14:24