本文介绍了当从控制台应用程序调用cmd.exe时,查询会话不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查询会话在使用任何CPU或X86目标平台时不起作用,但在使用X64平台时可正常工作。

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();
}

有人可以解释为什么会发生这种情况吗?有没有办法,我可以使这个工作,当我设置为任何CPU与默认处理器架构设置为X86?

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?

推荐答案

使用pinvoke.net可以轻松解决这个问题。这里是解决方案

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);
}

可在任何CPU,X86和X64目标平台上正常工作

Works perfectly on Any CPU, X86 and X64 target platforms

这篇关于当从控制台应用程序调用cmd.exe时,查询会话不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 19:18