| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 
 |  
#include "stdafx.h"
 
#include <iostream>
#define _WIN32_DCOM 
 
using namespace std ; 
 
#include <windows.h> 
#include <comdef.h> 
#include <wbemcli.h> 
 
#pragma comment(lib, "Wbemuuid") 
 
int main(int argc,char**argv) 
{ 
  IWbemLocator *pLocator = NULL; 
  IWbemServices *pNamespace = 0; 
  IWbemClassObject * pClass = NULL; 
//    IWbemClassObject * pOutInst = NULL; 
  IWbemClassObject * pInClass = NULL; 
  IWbemClassObject * pInInst = NULL; 
 
  IWbemCallResult *pCallRes = NULL; 
  IWbemClassObject *pObj = NULL; 
 
  BSTR path = SysAllocString(L"\\\\.\\ROOT\\CIMV2"); 
  BSTR ClassPath = SysAllocString(L"Win32_NTEventlogFile"); 
  BSTR MethodName = SysAllocString(L"ClearEventLog"); 
  BSTR ArgName = SysAllocString(L"ArchiveFileName"); 
 
  // Initialize COM and connect to WMI. 
  HRESULT hr = CoInitialize(0); 
  hr = CoCreateInstance(CLSID_WbemLocator,0,CLSCTX_INPROC_SERVER,IID_IWbemLocator,(LPVOID *) &pLocator); 
  if SUCCEEDED(hr) 
  { 
    printf("WbemLocator OK!\n"); 
  } 
 
  hr = pLocator->ConnectServer(path, NULL, NULL, NULL, 0, NULL, NULL, &pNamespace); 
  if SUCCEEDED(hr) 
  { 
    printf("ConnectServer OK!\n"); 
  } 
  else 
  { 
    IErrorInfo *pIErrorInfo; 
    GetErrorInfo(0,&pIErrorInfo); 
    BSTR desc; 
    pIErrorInfo->GetDescription(&desc); 
  } 
 
  // Get the class object for the method definition. 
  hr = pNamespace->GetObject(ClassPath, 0, NULL, &pClass, NULL); 
  if SUCCEEDED(hr) 
  { 
    printf("GetObject OK!\n"); 
  } 
  else 
  { 
    IErrorInfo *pIErrorInfo; 
    GetErrorInfo(0,&pIErrorInfo); 
    BSTR desc; 
    pIErrorInfo->GetDescription(&desc); 
  } 
 
  // Get the input-argument class object and create an instance. 
  hr = pClass->GetMethod(MethodName, 0, &pInClass, NULL); 
  if SUCCEEDED(hr) 
  { 
    printf("GetMethod OK!\n"); 
  } 
  else 
  {  
    IErrorInfo *pIErrorInfo; 
    GetErrorInfo(0,&pIErrorInfo); 
    BSTR desc; 
    pIErrorInfo->GetDescription(&desc); 
  } 
 
  hr = pInClass->SpawnInstance(0, &pInInst); 
  if SUCCEEDED(hr) 
  { 
    printf("SpawnInstance OK!\n"); 
  } 
  else 
  { 
    IErrorInfo *pIErrorInfo; 
    GetErrorInfo(0,&pIErrorInfo); 
    BSTR desc; 
    pIErrorInfo->GetDescription(&desc); 
  } 
 
  // Set the property. 
// BSTR Filename=L"C:\WINDOWS\system32\config\OSession.evt"; 
  VARIANT var; 
  var.vt = VT_BSTR; 
  //var.bstrVal= SysAllocString(L"C:\\WINNT\\system32\\config\\AppEvent.Evt"); 
  var.bstrVal= SysAllocString(L"C:\\WINNT\\system32\\config\\SysEvent.Evt"); 
  //var.bstrVal= SysAllocString(L"C:\\WINNT\\system32\\config\\SecEvent.Evt"); 
  hr = pInInst->Put(ArgName, 0, &var, 0); 
  if SUCCEEDED(hr) 
  { 
    printf("Put OK!\n"); 
  } 
  else 
  { 
    IErrorInfo *pIErrorInfo; 
    GetErrorInfo(0,&pIErrorInfo); 
    BSTR desc; 
    pIErrorInfo->GetDescription(&desc); 
  } 
 
  VariantClear(&var); 
 
  // Call the method. 
//    hr = pNamespace->ExecMethod(ClassPath, MethodName, 0, NULL, pInInst, &pOutInst, NULL); 
//    hr = pNamespace->ExecMethod(ClassPath,MethodName,NULL,NULL,pInInst,NULL,&pCallRes); 
  hr = pNamespace->ExecMethod(ClassPath,MethodName,WBEM_FLAG_RETURN_IMMEDIATELY,NULL,pInInst,NULL,&pCallRes); 
  if (WBEM_S_NO_ERROR==hr) 
  { 
    printf("ExecMethod OK!\n"); 
  } 
  else 
  {  
    IErrorInfo *pIErrorInfo; 
    GetErrorInfo(0,&pIErrorInfo); 
    BSTR desc; 
    pIErrorInfo->GetDescription(&desc); 
  } 
 
  while (true) 
  { 
    LONG lStatus = 0; 
    HRESULT hRes = pCallRes->GetCallStatus(0, &lStatus); 
    if ((hRes == WBEM_S_NO_ERROR)) 
         break; 
 
    if ((hRes == WBEM_S_TIMEDOUT)) 
        continue; 
  } 
 
  hr = pCallRes->GetResultObject(WBEM_INFINITE, &pObj); 
 
/*    if (hr) 
    { 
        pCallRes->Release(); 
        return 1; 
    } 
*/ 
/*    VARIANT varReturnValue; 
VariantInit(&varReturnValue); 
    hr = pObj->Get(_bstr_t(L"ReturnValue"),0,&varReturnValue,NULL,0); 
//    hr = pObj->Get(L"ReturnValue",0,&varReturnValue,NULL,0); 
 
    cout < < varReturnValue.lVal < < endl; 
*/ 
  IErrorInfo *pIErrorInfo; 
  GetErrorInfo(0,&pIErrorInfo); 
  BSTR desc; 
  pIErrorInfo->GetDescription(&desc); 
 
  // property "ReturnValue" and the returned string is in the 
  // property "sOutArg". 
  //hr = pOutInst->GetObjectText(0, &Text); 
  //printf("\nThe object text is:\n%S", Text); 
 
  // Free up resources. 
  SysFreeString(path); 
  SysFreeString(ClassPath); 
  SysFreeString(MethodName); 
  SysFreeString(ArgName); 
  pCallRes->Release(); 
  pClass->Release(); 
  pInInst->Release(); 
  pInClass->Release(); 
  //pOutInst->Release(); 
  pLocator->Release(); 
  pNamespace->Release(); 
  CoUninitialize(); 
  printf("Terminating normally\n"); 
 
  return 0; 
  // Program successfully completed. 
} |