📄 wmiquery.cpp
字号:
#include "stdafx.h"
#include "Test_WMISample.h"
#include "Test_WMISampleDlg.h"
#include <wbemidl.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
extern IWbemLocator *pLocator;
extern IWbemServices *pNamespace;
extern HANDLE m_hEvent1;
extern volatile BOOL KeepRunning;
void Queryinfo()
{ //查询信息
CTest_WMISampleDlg* pDlg=(CTest_WMISampleDlg*)AfxGetMainWnd();
CString msg;
IEnumWbemClassObject * pClassEnumerator = NULL;
IWbemClassObject * pClassInstance = NULL;
// create a class enumerator
BSTR ClassPath = SysAllocString(L"SampleActivity");
BSTR PropName = SysAllocString(L"ClickCount");
ULONG BytesReturned=1;
VARIANT PropValue;
HRESULT hr = pNamespace->CreateInstanceEnum(
ClassPath,
WBEM_FLAG_SHALLOW,
NULL,
&pClassEnumerator
);
SysFreeString(ClassPath);
if ( !SUCCEEDED(hr) )
{
msg.Format("Failed to create instance, hr=%x\n", hr);
Print_Event::SendEvent(msg);
}
else
{ //显示当前计数值
// Enumerate all instances of class Activity, and display properties of each.
while ( SUCCEEDED( hr=pClassEnumerator->Next(WBEM_INFINITE, 1, &pClassInstance, &BytesReturned) ) && (BytesReturned != 0))
{
hr = pClassInstance->Get(PropName, 0, &PropValue, NULL, NULL);
if ( !SUCCEEDED(hr) )
{
msg.Format("Get (property) failed, hr=%d %x\n", hr,hr);
Print_Event::SendEvent(msg);
}
else
{
pDlg->SetDlgItemInt(St_Data,V_I4(&PropValue));
}
VariantClear(&PropValue);
if (pClassInstance) pClassInstance->Release();
}
SysFreeString(PropName);
if (pClassEnumerator) pClassEnumerator->Release();
}
// create a class enumerator
ClassPath = SysAllocString(L"SampleControl");
PropName = SysAllocString(L"XEventThreshold");
hr = pNamespace->CreateInstanceEnum(
ClassPath,
WBEM_FLAG_SHALLOW,
NULL,
&pClassEnumerator
);
SysFreeString(ClassPath);
if ( !SUCCEEDED(hr) )
{
msg.Format("Failed to create instance, hr=%x\n", hr);
Print_Event::SendEvent(msg);
}
else
{ //显示事件门限值
while ( SUCCEEDED( hr=pClassEnumerator->Next(WBEM_INFINITE, 1, &pClassInstance, &BytesReturned) ) && (BytesReturned != 0))
{
hr = pClassInstance->Get(PropName, 0, &PropValue, NULL, NULL);
if ( !SUCCEEDED(hr) )
{
msg.Format("Get (property) failed, hr=%d %x\n", hr,hr);
Print_Event::SendEvent(msg);
}
else
{
pDlg->SetDlgItemInt(Threshold_Now,V_I4(&PropValue));
}
VariantClear(&PropValue);
if (pClassInstance) pClassInstance->Release();
}
SysFreeString(PropName);
if (pClassEnumerator) pClassEnumerator->Release();
}
// create a class enumerator
ClassPath = SysAllocString(L"SamplePdoInfo");
BSTR XdPropName = SysAllocString(L"PdoDeviceName");
BSTR YdPropName = SysAllocString(L"PdoRegistryPath");
TCHAR msgbuf[257];
hr = pNamespace->CreateInstanceEnum(
ClassPath,
WBEM_FLAG_SHALLOW,
NULL,
&pClassEnumerator
);
SysFreeString(ClassPath);
if ( !SUCCEEDED(hr) )
{
msg.Format("Failed to create instance, hr=%x\n", hr);
Print_Event::SendEvent(msg);
}
else
{ //显示字符串信息--PDO设备名和注册表路径
while ( SUCCEEDED( hr=pClassEnumerator->Next(WBEM_INFINITE, 1, &pClassInstance, &BytesReturned) ) && (BytesReturned != 0))
{
hr = pClassInstance->Get(XdPropName, 0, &PropValue, NULL, NULL);
if ( !SUCCEEDED(hr) )
{
msg.Format("Get (property) failed, hr=%d %x\n", hr,hr);
Print_Event::SendEvent(msg);
}
else
{
wsprintf( msgbuf, "%ws",PropValue.bstrVal);
pDlg->SetDlgItemText(St_DeviceName,msgbuf);
}
VariantClear(&PropValue);
hr = pClassInstance->Get(YdPropName, 0, &PropValue, NULL, NULL);
if ( !SUCCEEDED(hr) )
{
msg.Format("Get (property) failed, hr=%d %x\n", hr,hr);
Print_Event::SendEvent(msg);
}
else
{
wsprintf( msgbuf, "%ws",PropValue.bstrVal);
pDlg->SetDlgItemText(St_RegistryPath,msgbuf);
}
VariantClear(&PropValue);
if (pClassInstance) pClassInstance->Release();
}
SysFreeString(XdPropName);
SysFreeString(YdPropName);
if (pClassEnumerator) pClassEnumerator->Release();
}
return;
}
DWORD WINAPI CollectData(PVOID pParam)
{ //WMI数据采集
CTest_WMISampleDlg* pDlg=(CTest_WMISampleDlg*)AfxGetMainWnd();
CString msg;
IEnumWbemClassObject * pClassEnumerator = NULL;
IWbemClassObject * pClassInstance = NULL;
// create a class enumerator
while(TRUE)
{
BSTR ClassPath = SysAllocString(L"SampleActivity");
HRESULT hr = pNamespace->CreateInstanceEnum(
ClassPath,
WBEM_FLAG_SHALLOW,
NULL,
&pClassEnumerator
);
SysFreeString(ClassPath);
if ( !SUCCEEDED(hr) )
{
msg.Format("Failed to create instance, hr=%x\n", hr);
Print_Event::SendEvent(msg);
return 1;
}
ULONG BytesReturned=1;
BSTR XdPropName = SysAllocString(L"ClickCount");
VARIANT PropValue;
// Enumerate all instances of class Activity, and display properties of each.
while ( SUCCEEDED( hr=pClassEnumerator->Next(WBEM_INFINITE, 1, &pClassInstance, &BytesReturned) ) && (BytesReturned != 0))
{
hr = pClassInstance->Get(XdPropName, 0, &PropValue, NULL, NULL);
if ( !SUCCEEDED(hr) )
{
msg.Format("Get (property) failed, hr=%d %x\n", hr,hr);
Print_Event::SendEvent(msg);
}
else
{
if (KeepRunning) pDlg->SetDlgItemInt(St_Data,V_I4(&PropValue));
}
VariantClear(&PropValue);
if (pClassInstance) pClassInstance->Release();
}
SysFreeString(XdPropName);
if (pClassEnumerator) pClassEnumerator->Release();
if (WaitForSingleObject(m_hEvent1, 100)==WAIT_OBJECT_0) goto EXIT;
}
EXIT: return 0;
}
void Reset()
{ //设置事件计数值为0
CTest_WMISampleDlg* pDlg=(CTest_WMISampleDlg*)AfxGetMainWnd();
CString msg;
IEnumWbemClassObject * pClassEnumerator = NULL;
IWbemClassObject * pClassInstance = NULL;
// create a class enumerator
BSTR ClassPath = SysAllocString(L"SampleActivity");
HRESULT hr = pNamespace->CreateInstanceEnum(
ClassPath,
WBEM_FLAG_SHALLOW,
NULL,
&pClassEnumerator
);
SysFreeString(ClassPath);
if ( !SUCCEEDED(hr) )
{
msg.Format("Failed to create instance, hr=%x\n", hr);
Print_Event::SendEvent(msg);
return;
}
ULONG BytesReturned=1;
BSTR PathPropName = SysAllocString(L"__Path");
BSTR ResetMethodName = SysAllocString(L"ResetStatistics");
VARIANT PropValue;
while ( SUCCEEDED( hr=pClassEnumerator->Next(WBEM_INFINITE, 1, &pClassInstance, &BytesReturned) ) && (BytesReturned != 0))
{
hr = pClassInstance->Get(PathPropName, 0, &PropValue, NULL, NULL);
if ( !SUCCEEDED(hr) )
{
msg.Format("failed to get path name\n");
Print_Event::SendEvent(msg);
}
else
{
hr = pNamespace->ExecMethod(V_BSTR(&PropValue), ResetMethodName, 0, NULL, NULL, NULL, NULL);
//执行SampleActivity类中的ResetStatistics方法调用
msg.Format("Result of exec method call=%d %x\n", hr,hr);
Print_Event::SendEvent(msg);
}
VariantClear(&PropValue);
if (pClassInstance) pClassInstance->Release();
}
SysFreeString(PathPropName);
SysFreeString(ResetMethodName);
if (pClassEnumerator) pClassEnumerator->Release();
return;
}
void Setthreshold()
{ //设置事件门限
CTest_WMISampleDlg* pDlg=(CTest_WMISampleDlg*)AfxGetMainWnd();
CString msg;
IEnumWbemClassObject * pClassEnumerator = NULL;
IWbemClassObject * pClassInstance = NULL;
// create a class enumerator
BSTR ClassPath = SysAllocString(L"SampleControl");
HRESULT hr = pNamespace->CreateInstanceEnum(
ClassPath,
WBEM_FLAG_SHALLOW,
NULL,
&pClassEnumerator
);
SysFreeString(ClassPath);
if ( !SUCCEEDED(hr) )
{
msg.Format("Failed to create instance, hr=%x\n", hr);
Print_Event::SendEvent(msg);
return;
}
ULONG BytesReturned=1;
BSTR PropName = SysAllocString(L"XEventThreshold");
VARIANT PropValue;
while ( SUCCEEDED( hr=pClassEnumerator->Next(WBEM_INFINITE, 1, &pClassInstance, &BytesReturned) ) && (BytesReturned != 0))
{
hr = pClassInstance->Get(PropName, 0, &PropValue, NULL, NULL);
if ( !SUCCEEDED(hr) )
{
msg.Format("Get (property) failed, hr=%d %x\n", hr,hr);
Print_Event::SendEvent(msg);
}
PropValue.lVal=pDlg->GetDlgItemInt(Threshold_Edit);
hr = pClassInstance->Put(PropName, 0, &PropValue, 0);
//设置数据项--事件门限
if ( !SUCCEEDED(hr) )
{
msg.Format("Put (property) failed, hr=%d %x\n", hr,hr);
Print_Event::SendEvent(msg);
}
else
{
hr = pNamespace->PutInstance(pClassInstance, WBEM_FLAG_UPDATE_ONLY, NULL, NULL);
//更新WMI数据库中数据项--事件门限
}
VariantClear(&PropValue);
hr = pClassInstance->Get(PropName, 0, &PropValue, NULL, NULL);
if ( !SUCCEEDED(hr) )
{
msg.Format("Get (property) failed, hr=%d %x\n", hr,hr);
Print_Event::SendEvent(msg);
}
else
{
pDlg->SetDlgItemInt(Threshold_Now,V_I4(&PropValue));
}
VariantClear(&PropValue);
if (pClassInstance) pClassInstance->Release();
}
SysFreeString(PropName);
if (pClassEnumerator) pClassEnumerator->Release();
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -