⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_wmisampledlg.cpp

📁 windows 2000/xpWDM设备驱动程序开发光盘代码
💻 CPP
字号:
// Test_WMISampleDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Test_WMISample.h"
#include "Test_WMISampleDlg.h"
#include "..\WMISampleioctl.h"
#include "..\WMISampleDeviceinterface.h"	// Has class GUID definition
#include <winioctl.h>
#include <wbemidl.h> 

GUID ClassGuid = WMISampleDevice_CLASS_GUID;
// This function is found in module OpenByIntf.cpp
HANDLE OpenByInterface(GUID* pClassGuid, DWORD instance, PDWORD pError);

HWND Print_Event::ViewHwnd = NULL;

HANDLE m_hEvent1,m_hEvent2;
HANDLE Thread1,Thread2;
volatile BOOL KeepRunning;
extern IWbemLocator *pLocator;
extern IWbemServices *pNamespace;
extern HRESULT hr;

DWORD WINAPI Event(PVOID pParam);
DWORD WINAPI CollectData(PVOID pParam);

void Connect_WMI();
void Disconnect_WMI();

void Queryinfo();
void Reset();
void Setthreshold();

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTest_WMISampleDlg dialog

CTest_WMISampleDlg::CTest_WMISampleDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTest_WMISampleDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTest_WMISampleDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
CTest_WMISampleDlg::~CTest_WMISampleDlg()
{
}
void CTest_WMISampleDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTest_WMISampleDlg)
	DDX_Control(pDX, IDC_Set, m_Set);
	DDX_Control(pDX, IDC_Reset, m_Reset);
	DDX_Control(pDX, IDC_INC, m_Inc);
	DDX_Control(pDX, IDC_DEC, m_Dec);
	DDX_Control(pDX, IDC_BtEventStop, m_BtEventStop);
	DDX_Control(pDX, IDC_BtEventStart, m_BtEventStart);
	DDX_Control(pDX, IDC_BtDataStop, m_BtDataStop);
	DDX_Control(pDX, IDC_BtDataStart, m_BtDataStart);
	DDX_Control(pDX, IDC_LtEvent, m_LtEvent);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTest_WMISampleDlg, CDialog)
	//{{AFX_MSG_MAP(CTest_WMISampleDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_INC, OnInc)
	ON_BN_CLICKED(IDC_DEC, OnDec)
	ON_BN_CLICKED(IDC_BtDataStart, OnBtDataStart)
	ON_BN_CLICKED(IDC_BtDataStop, OnBtDataStop)
	ON_BN_CLICKED(IDC_BtEventStop, OnBtEventStop)
	ON_BN_CLICKED(IDC_BtEventStart, OnBtEventStart)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_Reset, OnReset)
	ON_BN_CLICKED(IDC_Set, OnSet)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_PRINTEVENT,OnPrintEvent)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTest_WMISampleDlg message handlers

BOOL CTest_WMISampleDlg::OnInitDialog()
{
	DWORD Error;
	CString msg;

	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	CWnd* m_cWnd=AfxGetMainWnd();
	Print_Event::ViewHwnd = m_cWnd->m_hWnd;

	m_hEvent1=CreateEvent(0,FALSE,FALSE,NULL);
	m_hEvent2=CreateEvent(0,FALSE,FALSE,NULL);
	m_hDevice = OpenByInterface( &ClassGuid, 0, &Error);
	if (m_hDevice == INVALID_HANDLE_VALUE)
	{
		msg.Format("ERROR opening device: (%0x) returned from CreateFile\n", GetLastError());
		m_LtEvent.AddString(msg);
	}
	else
	{
		Connect_WMI();	//联接WMI数据库
		Queryinfo();	//查询信息
		KeepRunning=TRUE;
		m_BtEventStart.EnableWindow(TRUE);
		m_BtDataStart.EnableWindow(TRUE);
		m_Reset.EnableWindow(TRUE);
		m_Set.EnableWindow(TRUE);
		m_Inc.EnableWindow(TRUE);
		m_Dec.EnableWindow(TRUE);
	}
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTest_WMISampleDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTest_WMISampleDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTest_WMISampleDlg::OnInc() 
{
	DWORD junk;
	if (m_hDevice == INVALID_HANDLE_VALUE) return;
	DeviceIoControl(m_hDevice, INCX, NULL, 0, NULL, 0, &junk, NULL);
	//强制事件计数值加1
}

void CTest_WMISampleDlg::OnDec() 
{
	DWORD junk;
	if (m_hDevice == INVALID_HANDLE_VALUE) return;
	DeviceIoControl(m_hDevice, DECX, NULL, 0, NULL, 0, &junk, NULL);
	//强制事件计数值减1
}
void CTest_WMISampleDlg::OnBtEventStart() 
{
	DWORD Tid;		  
	Thread2=CreateThread(0, 0x1000, Event, 0, 0, &Tid);	//可允许WMI事件发生
	//创建线程
	m_BtEventStart.EnableWindow(FALSE);
	m_BtEventStop.EnableWindow(TRUE);
}
void CTest_WMISampleDlg::OnBtEventStop() 
{
	SetEvent(m_hEvent2);	//可禁止WMI事件发生
	//设置事件
	m_BtEventStop.EnableWindow(FALSE);
	m_BtEventStart.EnableWindow(TRUE);
}
void CTest_WMISampleDlg::OnBtDataStart() 
{
	DWORD Tid;		  
	Thread1=CreateThread(0, 0x1000, CollectData, 0, 0, &Tid);
	//可允许WMI数据采集
	m_BtDataStart.EnableWindow(FALSE);
	m_BtDataStop.EnableWindow(TRUE);
}
void CTest_WMISampleDlg::OnBtDataStop() 
{
	SetEvent(m_hEvent1);	//可禁止WMI数据采集
	m_BtDataStop.EnableWindow(FALSE);
	m_BtDataStart.EnableWindow(TRUE);
}

void CTest_WMISampleDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	if (KeepRunning)
	{
		KeepRunning=FALSE;
		SetEvent(m_hEvent1);
		SetEvent(m_hEvent2);
		WaitForSingleObject(Thread1,INFINITE);	//等待线程结束
		WaitForSingleObject(Thread2,INFINITE);

		Disconnect_WMI();	//断开WMI数据库
	}
	
	CDialog::OnClose();
}

void CTest_WMISampleDlg::OnReset() 
{
	Reset();	//设置事件计数值为0
}

void CTest_WMISampleDlg::OnSet() 
{
	Setthreshold();	//设置事件门限
}

LONG CTest_WMISampleDlg::OnPrintEvent(UINT, LONG lParam)
{	//显示事件信息
	Print_Event* pEvent = (Print_Event*)lParam;
	if (pEvent) 
	{
		m_LtEvent.AddString(pEvent->Message);
		delete pEvent;
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -