【问题标题】:How to fix winmgmt error code 0x8007007E?如何修复 winmgmt 错误代码 0x8007007E?
【发布时间】:2019-12-14 13:20:27
【问题描述】:

我正在制作用于执行 winmgmt 的 C# 包装类,但命令一直失败。

我尝试将StartInfo.Verb 参数设置为"runas",但没有帮助。 C# 应用程序已被提升。

在提升的命令提示符下运行命令winmgmt /verifyrepository 工作正常。

public static void VerifyRepository()
{
var p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = @"winmgmt";
p.StartInfo.Arguments = @"/verifyrepository";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
WMIVerifyResultReceived?.Invoke(null, new WMIVerifyResultReceivedEventArgs(output));
}

在cmd中运行时输出为

WMI repository is consistent

但是当使用VerifyRepository() 方法运行时,我一直在同一台机器上得到这个输出:

WMI repository verification failed Error code: 0x8007007E

【问题讨论】:

    标签: c#


    【解决方案1】:

    问题是因为应用程序在WOW64(64位上的32位)中运行,因此winmgmt的路径在systemwow64文件夹中,并且服务在64位模式下运行。 通过取消选中构建选项中的首选 32 位来修复它。 在不禁用首选 32 位的情况下解决此问题的另一种方法是在 verifyrepository 方法的开头使用 wow64apiset 调用 Wow64DisableWow64FsRedirection,然后在结束时调用 Wow64RevertWow64FsRedirection 函数,如 here 所示:

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
    
    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);
    
    public static void VerifyRepository()
    {
    var wow64Value = IntPtr.Zero;
    Wow64DisableWow64FsRedirection(ref wow64Value);
    var p = new Process();
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.FileName = @"winmgmt";
    p.StartInfo.Arguments = @"/verifyrepository";
    p.Start();
    string output = p.StandardOutput.ReadToEnd();
    p.WaitForExit();
    Wow64RevertWow64FsRedirection(wow64Value);
    WMIVerifyResultReceived?.Invoke(null, new WMIVerifyResultReceivedEventArgs(output));
    }
    

    请注意,如果您更喜欢未选中的 32 位,则无需使用 wow64apiset 调用。

    【讨论】:

      猜你喜欢
      • 2018-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-09
      • 2017-02-06
      • 2019-07-02
      相关资源
      最近更新 更多