我正在使用此 stackoverflow 帖子中的以下 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,我在此处列出的任何 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)
第二个堆栈跟踪没有消息。