Calling dism.exe from System.Diagnostics.Process Fails
Asked Answered
F

4

11

For enabling Microsoft-Hyper-V and Microsoft-Hyper-V-Management in Windows 2008 R2 Server(64bit), I'm calling dism.exe as a process. The command I've used is

Dism.exe /online /Get-FeatureInfo /FeatureName:Microsoft-Hyper-V

Dism.exe /online /Get-FeatureInfo /FeatureName:Microsoft-Hyper-V-Management-Clients

This works fine when I execute this from the command line but it fails when I try to execute it through my code. I've tried the 64bit version of Dism.exe under the C:\Windows\SysWoW64 folder but it fails too. Here is the error message I get,

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.

What am I missing here?

UPDATE: SLaks was right, but the issue turned out to be that MS Visual studio express edition by default targets x86 which I had to manually edit in the .csproj file to AnyCPU to make it work.

Freda answered 9/5, 2011 at 12:13 Comment(1)
SysWoW64 contains 32-bit stubs, not 64-bit executables.Mckinney
M
30

Because you're running in a 32-bit process, you're getting redirected to the 32-bit version in SysWoW64

Run %WINDIR%\SysNative\dism.exe to prevent redirection.

Mckinney answered 9/5, 2011 at 12:15 Comment(3)
Tried it but still facing the same issue but no luck! Here are two lines from the log, DISM.EXE: Executing command line: C:\Windows\SysWow64\dism.exe /online /Get-FeatureInfo /FeatureName:Microsoft-Hyper-V DISM Provider Store: PID=892 Loading Provider from location C:\Windows\System32\Dism\FolderProvider.dll - CDISMProviderStore::Internal_GetProvider Although I've targeted the 64bit version of it, it's still looking up the 32bit processes' dism librariesFreda
This doesn't seem to work for me. In my server (same OS, also 64 bit) there is no C:\Windows\SysNative folder at all...Frogmouth
The C:\Windows\SysNative folder is only visible to 32bit processes.Dread
B
5

Create below content in a batch file, for example RunDism.bat

%WINDIR%\SysNative\dism.exe

Call the batch file in your program. SysNative is not a real folder, so you cannot call above code in your program directly, it must be call by system. This way is worked for me.

Burin answered 18/10, 2013 at 1:1 Comment(1)
I develop on Mac now so can't verify this but may be useful for someone else who faces this issue. ThanksFreda
C
2

The thing is you need to call the appropriate dism.exe dependng on the system architecture.

As @eric xu said, you need to resolve the path because it is not a real path. Below is the code that works for me. It basically detects the system architecture, resolves the path depending on the architecture and then calls the appropriate dism.exe.

string system32Directory = Path.Combine(Environment.ExpandEnvironmentVariables("%windir%"), "system32", "dism.exe");
if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess)
{
    // For 32-bit processes on 64-bit systems, %windir%\system32 folder
    // can only be accessed by specifying %windir%\sysnative folder.
    system32Directory = Path.Combine(Environment.ExpandEnvironmentVariables("%windir%"), "sysnative", "dism.exe");
}

Source: File System Redirector

Continuum answered 21/3, 2016 at 6:13 Comment(0)
B
0

I had to use "SysNative\dism.exe" If I added %WINDIR%\ it would fail, I'm using VS2017 installing on Server 2012R2. Tnhx!

Brantley answered 28/8, 2017 at 19:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.