query session does not work when using Any CPU or X86 target Platform , but works when using X64 platform.
[TestMethod]
public void TestMethod()
{
ProcessStartInfo info = new ProcessStartInfo("cmd.exe","/k query session");
Process proc = new Process();
proc.StartInfo = info;
proc.Start();
}
Can someone explain why this is happening? Is there a way I can make this work when I set it to Any CPU with Default Processor architecture set to X86?
This problem can be easily overcome by using pinvoke.net. Here is the solution
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);
[TestMethod]
public void TestMethod3()
{
IntPtr ptr = new IntPtr();
Wow64DisableWow64FsRedirection(ref ptr);
ProcessStartInfo info = new ProcessStartInfo(@"cmd.exe", "/k query session");
Process proc = new Process();
proc.StartInfo = info;
proc.Start();
Wow64RevertWow64FsRedirection(ptr);
}
Works perfectly on Any CPU, X86 and X64 target platforms