【发布时间】:2023-03-04 10:14:01
【问题描述】:
我正在使用来自this stackoverflow post 的以下 C# 代码。
ManagementScope oScope = new ManagementScope("\\\\localhost\\root\\default");
ManagementPath oPath = new ManagementPath("SystemRestore");
ObjectGetOptions oGetOp = new ObjectGetOptions();
ManagementClass oProcess = new ManagementClass(oScope, oPath, oGetOp);
ManagementBaseObject oInParams =
oProcess.GetMethodParameters("CreateRestorePoint");
oInParams["Description"] = "Setting restore point";
oInParams["RestorePointType"] = 12; // MODIFY_SETTINGS
oInParams["EventType"] = 100;
ManagementBaseObject oOutParams =
oProcess.InvokeMethod("CreateRestorePoint", oInParams, null);
错误来自声明oOutParams的行。
我已将此行包装在 try catch 中,并打印出错误代码 -2147023838,我无法在here 列出的任何 COM 错误代码列表中找到该代码(假设打印的错误代码应转换为十六进制2 的补码 -> 0x80070422)。
没有捕获异常(所以基本上是上面的确切代码),堆栈跟踪是:
Unhandled Exception: System.Runtime.InteropServices.COMException
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at System.Management.ManagementObject.InvokeMethod(String methodName, ManagementBaseObject inParameters, InvokeMethodOptions options)
at WindowsUpdatesDev.Program.Main(String[] args)
将 try/catch 包裹在声明 oOutParams 的行上,我打印堆栈跟踪:
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at System.Management.ManagementObject.InvokeMethod(String methodName, ManagementBaseObject inParameters, InvokeMethodOptions options)
at WindowsUpdatesDev.Program.Main(String[] args)
第二个堆栈跟踪没有消息。
【问题讨论】:
-
你有完整的堆栈跟踪吗?让我们看看吧。
-
以及内部异常的堆栈跟踪,如果有的话。
-
@RobertHarvey 我添加了运行上述代码时打印的堆栈跟踪,以及内部 try/catch 的堆栈跟踪。
-
关于错误代码的信息是here:服务无法启动,要么是因为它被禁用,要么是因为它没有与之关联的启用设备。
标签: c# wmi windows-10-iot-enterprise