wmiquery.cpp

来自「WindowsXP WDM驱动程序开发实例」· C++ 代码 · 共 387 行

CPP
387
字号
#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;
int    iniwmi();
void   retwmi();
extern HANDLE m_hEvent1;
extern BOOL KeepRunning;

int queryinfo()
{
	CTest_WMISampleDlg* pDlg=(CTest_WMISampleDlg*)AfxGetMainWnd();
	CString msg;
	HRESULT hr;

	IEnumWbemClassObject * pClassEnumerator = NULL;
	IWbemClassObject * pClassInstance = NULL;
  
	// create a class enumerator

    BSTR ClassPath = SysAllocString(L"SamplePdoInformation");
	
	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"PdoDeviceName");
	BSTR YdPropName = SysAllocString(L"PdoRegistryPath");
	VARIANT PropValue;
	TCHAR msgbuf[257];

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

	// create a class enumerator

    ClassPath = SysAllocString(L"SampleActivity");
	
	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;
	}

	BSTR PropName = SysAllocString(L"ClickCount");

	// 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
		{
			if (KeepRunning) 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");
	
	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;
	}

	PropName = SysAllocString(L"XEventThreshold");

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

    return 0;
}

DWORD WINAPI collectdata(PVOID pParam)
{
	CTest_WMISampleDlg* pDlg=(CTest_WMISampleDlg*)AfxGetMainWnd();
	CString msg;
	HRESULT hr;

	IEnumWbemClassObject * pClassEnumerator = NULL;
	IWbemClassObject * pClassInstance = NULL;
  
	// create a class enumerator


	while(TRUE)
	{
	    BSTR ClassPath = SysAllocString(L"SampleActivity");
		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;
}
int reset()
{
	CTest_WMISampleDlg* pDlg=(CTest_WMISampleDlg*)AfxGetMainWnd();
	CString msg;
	HRESULT hr;

	IEnumWbemClassObject * pClassEnumerator = NULL;
	IWbemClassObject * pClassInstance = NULL;
  
	// create a class enumerator

    BSTR ClassPath = SysAllocString(L"SampleActivity");

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

void setthreshold()
{
	CTest_WMISampleDlg* pDlg=(CTest_WMISampleDlg*)AfxGetMainWnd();
	CString msg;
	HRESULT hr;

	IEnumWbemClassObject * pClassEnumerator = NULL;
	IWbemClassObject * pClassInstance = NULL;
  
	// create a class enumerator
    BSTR ClassPath = SysAllocString(L"SampleControl");

	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);
		}
		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 + =
减小字号Ctrl + -
显示快捷键?