在32位

时间:2015-05-29 15:53:00

标签: c#

我试图运行quser并获取结果字符串。当我运行下面的代码时,我看到以下消息,结果字符串为空:

  

' QUSER'不被识别为内部或外部命令,可操作程序或批处理文件。

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c quser /server:SomeServer";
p.Start();

string output = p.StandardOutput.ReadToEnd();

p.WaitForExit();
Console.WriteLine(output);

所以运行的完整命令是

cmd.exe /c quser /ser:SomeServer

直接执行时运行正常,但C#失败。

我在这里找到了一个类似的问题没有答案:Executing Quser windows command in C#; Returning result to String。这个问题虽然没有quser not recognized消息。

为什么从代码运行时无法识别命令?

我尝试直接运行quser命令,但是我找不到找到的文件......很奇怪

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = @"c:\Windows\System32\quser.exe";
p.StartInfo.Arguments = @"/server:SomeServer";
p.Start();

string output = p.StandardOutput.ReadToEnd();

p.WaitForExit();
Console.WriteLine(output);

我们发现以64位运行它,它找到了它。当以AnyCPU或32位运行时,它似乎正在查看SysWOW64,即使我直接告诉它查看System32

1 个答案:

答案 0 :(得分:4)

好的,所以我找到了解决方案。

基本上,正如我们在chat中发现的那样,System32文件夹会在某些版本上重定向到SysWOW64,导致quser显示为而不是在那里。

我已成功应用解决方法。

解决方法:

  1. 使用以下命名空间:

    using System.Runtime.InteropServices;
    
  2. 在类文件的顶部添加以下内容。

    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern int Wow64DisableWow64FsRedirection(ref IntPtr ptr);
    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern int Wow64EnableWow64FsRedirection(ref IntPtr ptr);
    
  3. 在进行quser来电之前,请进行以下调用:

    IntPtr val = IntPtr.Zero;
    Wow64DisableWow64FsRedirection(ref val);
    
  4. 进行quser来电后,请还原更改:

    Wow64EnableWow64FsRedirection(ref val);
    
  5. 完整样本:

    using System.Runtime.InteropServices;
    
    ...
    namespace CSharpTests
    {
        public class Program
        {
            [DllImport("kernel32.dll", SetLastError = true)]
            public static extern int Wow64DisableWow64FsRedirection(ref IntPtr ptr);
            [DllImport("kernel32.dll", SetLastError = true)]
            public static extern int Wow64EnableWow64FsRedirection(ref IntPtr ptr);
            [DllImport("kernel32.dll", SetLastError = true)]
            public static extern int Wow64RevertWow64FsRedirection(ref IntPtr ptr);
    
            static void Main(string[] args)
            {
                IntPtr val = IntPtr.Zero;
                Wow64DisableWow64FsRedirection(ref val);
                Process p = new Process();
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.Arguments = "/c quser";
                p.Start();
    
                string output = p.StandardOutput.ReadToEnd();
    
                p.WaitForExit();
                Console.WriteLine(output);
                Wow64RevertWow64FsRedirection(ref val);
                p = new Process();
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.Arguments = "/c quser";
                p.Start();
    
                output = p.StandardOutput.ReadToEnd();
    
                p.WaitForExit();
                Console.WriteLine(output);
            }
        }
    }
    

    结果:

    USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
    ebrown                console             1  Active      none   05/18/2015 09:2
    1
    'quser' is not recognized as an internal or external command,
    operable program or batch file.
    

    正如您所看到的,第一个quser调用成功,因为我们告诉操作系统停止重定向到SysWOW64,一旦我们重新启用它,呼叫就失败了。

    我确定此保护原因,但有时您不需要它。

    其他注意事项:

    实施此模式的人首先要检测是否需要解决方法,这是明智的。可以使用以下布尔值来完成这种检测:

    Environment.GetFolderPath(Environment.SpecialFolder.SystemX86).Contains("System32")
    

    如果是假布尔值,那么你需要检查:

    File.Exists(@"c:\windows\System32\FILENAMEHERE")
    

    在这种情况下:

    File.Exists(@"c:\windows\System32\qdisk.exe")
    

    改编自:

    http://blog.airesoft.co.uk/2010/09/wow-disabling-wow64-fs-redirection-can-cause-problems-who-knew/

    https://msdn.microsoft.com/en-us/library/windows/desktop/aa384187%28v=vs.85%29.aspx