May 02, 2024, 05:44:47 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Turning on system restore

Started by Andy, May 23, 2011, 12:13:37 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andy

Hi Sapero,

I have tried the code you posted some time ago and it works fine, but why does it open a command prompt when completed?

is there a way to make it turn on system restore silently?

This was the code you posted:

$include "windowssdk.inc"
$include "wbemidl.inc"
$include "stdio.inc" ' printf, console application1

' begin example
EnableSystemRestore()
end
' end example

sub EnableSystemRestore(),BOOL

   BOOL success = FALSE

   IWbemLocator pLoc      = NULL
   IWbemServices pSvc     = NULL
   IWbemClassObject pClass= NULL
   IWbemClassObject pInParamsDefinition = NULL
   IWbemClassObject pClassInstance = NULL
   IWbemClassObject pOutParams = NULL


   BSTR bstrString = NULL
   BSTR bstrMethod = NULL
   BSTR bstrClass  = NULL

   CoInitializeEx(0, COINIT_MULTITHREADED)
   CoInitializeSecurity(NULL, -1,NULL,NULL,RPC_C_AUTHN_LEVEL_DEFAULT,RPC_C_IMP_LEVEL_IMPERSONATE,NULL,EOAC_NONE,NULL)

   int a
   for a=0 to 0

      if (CoCreateInstance(_CLSID_WbemLocator,0, CLSCTX_INPROC_SERVER, _IID_IWbemLocator, &pLoc))
         printf("Failed to connect to WMI object IWbemLocator\n")
         breakfor
      endif
      bstrString = SysAllocString(L"ROOT\Default")
      if (pLoc->ConnectServer(bstrString,NULL,NULL,0,NULL,0,0,&pSvc))
         printf("Failed to open WMI namespace %s\n", "ROOT\Default")
         breakfor
      endif
      CoSetProxyBlanket(pSvc,RPC_C_AUTHN_WINNT,RPC_C_AUTHZ_NONE,NULL,RPC_C_AUTHN_LEVEL_CALL,RPC_C_IMP_LEVEL_IMPERSONATE,NULL,EOAC_NONE)

      bstrClass = SysAllocString(L"SystemRestore")
      if (pSvc->GetObject(bstrClass, 0, NULL, &pClass, NULL))
         printf("Failed to open WMI object %S\n", bstrClass)
         breakfor
      endif
      bstrMethod = SysAllocString(L"Enable")
      if (pClass->GetMethod(bstrMethod, 0, &pInParamsDefinition, NULL))
         printf("%s::GetMethod('%S') Failed\n", "IWbemClassObject", bstrMethod)
         breakfor
      endif
      if (pInParamsDefinition->SpawnInstance(0, &pClassInstance))
         printf("%s::SpawnInstance Failed\n", "IWbemClassObject")
         breakfor
      endif
      ' Create the values for the in parameters
      VARIANT varCommand
      varCommand.vt = wbemCimtypeString
      varCommand.bstrVal = SysAllocString(L"")

      if (pClassInstance->_Put(L"Drive", 0, &varCommand, 0))
         printf("%s::Put('%s') Failed\n", "IWbemClassObject", "Drive")
         SysFreeString(varCommand.bstrVal)
         breakfor
      endif
      SysFreeString(varCommand.bstrVal)

      DWORD hr = pSvc->ExecMethod(bstrClass, bstrMethod, 0, NULL, pClassInstance, &pOutParams, NULL)
      if (hr&0x80000000)
         printf("%s::ExecMethod('%S') Failed: 0x%X\n", "IWbemServices", bstrMethod, hr)
         breakfor
      endif
      VARIANT varReturnValue
      SysFreeString(bstrString)
      bstrString = SysAllocString(L"ReturnValue")
      if (pOutParams->_Get(bstrString, 0, &varReturnValue, NULL, 0))
         printf("%s::Get('%S') Failed\n", "IWbemClassObject", bstrString)
         breakfor
      endif
'      printf("vt     = %d\n", varReturnValue.vt)
'      printf("intVal = %d\n", varReturnValue.intVal)

      success = (varReturnValue.intVal = 0)
      if (!success and (varReturnValue.intVal <> ERROR_SERVICE_ALREADY_RUNNING))
         printf(" - EnableSystemRestore returned error %d\n", varReturnValue.intVal)
      else
         puts("success")
      endif
   next a
   if (bstrString) then SysFreeString(bstrString)
   if (bstrMethod) then SysFreeString(bstrMethod)
   if (bstrClass) then SysFreeString(bstrClass)

   if (pOutParams) then pOutParams->Release()
   if (pClassInstance) then pClassInstance->Release()
   if (pInParamsDefinition) then pInParamsDefinition->Release()
   if (pClass) then pClass->Release()
   if (pSvc) then pSvc->Release()
   if (pLoc) then pLoc->Release()
   CoUninitialize()
   return success
endsub

Thanks,
Andy.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

sapero

There is no OPENCONSOLE, so it must be your fault (somewhere else, or you still compile it for console).
Please use CODE tag.

Andy

Hi sapero,

yes, compiling as a window target did the trick sometimes you cannot see the wood for the trees!

Thanks,
Andy.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.