0

I'm using the following C# code from this stackoverflow post.

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

The error is coming from the line which declares oOutParams.

I've wrapped this line in a try catch, and printed out the error code which is -2147023838 which I cannot find in any list of COM error codes listed here (assuming the printed error code should be converted to hex 2's complement -> 0x80070422).

Without catching the exception (so basically the exact code above), the stack trace is:

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)

With the try/catch wrapped around the line that declares oOutParams I print the stack trace which is:

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)

There is no message for this second stack trace.

Ed.
  • 73
  • 12
  • Do you have the complete stack trace? Let's see it. – Robert Harvey Dec 05 '18 at 18:33
  • And the stack trace of the inner exception, if there is one. – Robert Harvey Dec 05 '18 at 18:33
  • @RobertHarvey I have added the stack trace that prints when running the code above, and also the stack trace for the inner try/catch. – Ed. Dec 05 '18 at 18:50
  • Information on the error code is [here](https://windows-hexerror.linestarve.com/0x80070422): *The service cannot be started, either because it is disabled or because it has no enabled devices associated with it.* – Robert Harvey Dec 05 '18 at 19:00

2 Answers2

1

Following steps here (specifically, all of step one and step two by setting 'Protection' to 'On' for my C: drive) allows the code above to run without errors.

Ed.
  • 73
  • 12
  • Thanks for this- I couldn't find a reason as to why I was getting a COMException when attempting to set a restore point, but it turns out my IT department had come in and changed the System Restore settings. – Aron Aug 09 '22 at 20:23
0

Just to follow up on my own experience here, and as Ed said in his answer:

Make sure that your system is configured to allow you to set a restore point.

You can do this by performing the following steps:

  1. Click the Windows key
  2. Type "Create a restore point" and open the application suggested
  3. Under the "System Protection" tab, make sure the protection is "On" for at least one of your drives
Aron
  • 372
  • 5
  • 18