本人想通过 WMI 的 Win32_NTEventlogFile 的 ClearEventLog 方法清除应用程序日志,在XP系统编译通过并运行,没有错误提示,可是发现日志并未清除,请高人指教!谢谢!急!代码如下:#define _WIN32_DCOM
#include <iostream>
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");
// BSTR Text; // 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); std::wcout << desc << L"\n";
} // 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); std::wcout << desc << L"\n";
} // 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); std::wcout << desc << L"\n";
} hr = pInClass->SpawnInstance(0, &pInInst);
if SUCCEEDED(hr)
{
printf("SpawnInstance OK!\n");
}
else
{
IErrorInfo *pIErrorInfo; GetErrorInfo(0,&pIErrorInfo); BSTR desc; pIErrorInfo->GetDescription(&desc); std::wcout << desc << L"\n";
} // 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");
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); std::wcout << desc << L"\n";
} VariantClear(&var); // Call the method.// hr = pNamespace->ExecMethod(ClassPath, MethodName, 0, NULL,
// pInInst, &pOutInst, NULL); hr = pNamespace->ExecMethod(ClassPath,MethodName,WBEM_FLAG_RETURN_IMMEDIATELY,NULL,pInInst,NULL,&pCallRes);
// hr = pNamespace->ExecMethod(ClassPath,MethodName,NULL,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); std::wcout << desc << L"\n";
} 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);std::wcout << desc << L"\n"; // 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);
// SysFreeString(Text);
pCallRes->Release(); pClass->Release();
pInInst->Release();
pInClass->Release();
//pOutInst->Release();
pLocator->Release();
pNamespace->Release();
CoUninitialize();
printf("Terminating normally\n"); return 0;
// Program successfully completed.
}
#include <iostream>
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");
// BSTR Text; // 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); std::wcout << desc << L"\n";
} // 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); std::wcout << desc << L"\n";
} // 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); std::wcout << desc << L"\n";
} hr = pInClass->SpawnInstance(0, &pInInst);
if SUCCEEDED(hr)
{
printf("SpawnInstance OK!\n");
}
else
{
IErrorInfo *pIErrorInfo; GetErrorInfo(0,&pIErrorInfo); BSTR desc; pIErrorInfo->GetDescription(&desc); std::wcout << desc << L"\n";
} // 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");
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); std::wcout << desc << L"\n";
} VariantClear(&var); // Call the method.// hr = pNamespace->ExecMethod(ClassPath, MethodName, 0, NULL,
// pInInst, &pOutInst, NULL); hr = pNamespace->ExecMethod(ClassPath,MethodName,WBEM_FLAG_RETURN_IMMEDIATELY,NULL,pInInst,NULL,&pCallRes);
// hr = pNamespace->ExecMethod(ClassPath,MethodName,NULL,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); std::wcout << desc << L"\n";
} 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);std::wcout << desc << L"\n"; // 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);
// SysFreeString(Text);
pCallRes->Release(); pClass->Release();
pInInst->Release();
pInClass->Release();
//pOutInst->Release();
pLocator->Release();
pNamespace->Release();
CoUninitialize();
printf("Terminating normally\n"); return 0;
// Program successfully completed.
}
hr = CoCreateInstance(CLSID_WbemLocator,0,CLSCTX_INPROC_SERVER,IID_IWbemLocator,(LPVOID *) &pLocator);
if SUCCEEDED(hr)
{
printf("WbemLocator OK!\n");
}执行结果中有显示啊!先谢谢一楼的先生!!!