wmisampledevice.cpp
来自「WindowsXP WDM驱动程序开发实例」· C++ 代码 · 共 351 行
CPP
351 行
// WMISampleDevice.cpp
// Implementation of WMISampleDevice device class
//
// Generated by DriverWizard version DriverStudio 2.6.0 (Build 336)
// Requires Compuware's DriverWorks classes
//
#pragma warning(disable:4065) // Allow switch statement with no cases
#include <vdw.h>
#include "..\WMISampleDeviceinterface.h"
#include "WMISample.h"
#include "WMISampleDevice.h"
#include "..\WMISampleioctl.h"
#pragma hdrstop("WMISample.pch")
extern KTrace t; // Global driver trace object
GUID WMISampleDevice_Guid = WMISampleDevice_CLASS_GUID;
KWmiDataBlock<SampleActivity, WMISampleDevice>::METHOD ActivityMethods[] = {
WMISampleDevice::ResetActivityStatistics,
};
WMISampleDevice::WMISampleDevice(PDEVICE_OBJECT Pdo, ULONG Unit) :
KPnpDevice(Pdo, &WMISampleDevice_Guid),
m_SamplePdoInfo(
m_Wmi,
&DRIVERWORKS_SAMPLE_PDO_INFORMATION
),
m_SampleControl(
m_Wmi,
&DRIVERWORKS_SAMPLE_CONTROL_GUID
),
m_SampleEvent(
m_Wmi,
&DRIVERWORKS_SAMPLE_EVENT_GUID,
WMIREG_FLAG_EVENT_ONLY_GUID
),
m_SampleActivity(
m_Wmi,
&DRIVERWORKS_SAMPLE_DATA_GUID,
WMIREG_FLAG_EXPENSIVE
)
{
// Check constructor status
if ( ! NT_SUCCESS(m_ConstructorStatus) )
{
return;
}
// Remember our unit number
m_Unit = Unit;
// Initialize the lower device
m_Lower.Initialize(this, Pdo);
// Inform the base class of the lower edge device object
SetLowerDevice(&m_Lower);
// Initialize the PnP Policy settings to the "standard" policy
SetPnpPolicy();
// TODO: Customize the PnP Policy for this device by setting
// flags in m_Policies.
((SamplePdoInfo*)m_SamplePdoInfo)->Initialize(Pdo);
}
WMISampleDevice::~WMISampleDevice()
{
}
NTSTATUS WMISampleDevice::DefaultPnp(KIrp I)
{
I.ForceReuseOfCurrentStackLocationInCalldown();
return m_Lower.PnpCall(this, I);
}
NTSTATUS WMISampleDevice::DefaultPower(KIrp I)
{
I.IndicatePowerIrpProcessed();
I.CopyParametersDown();
return m_Lower.PnpPowerCall(this, I);
}
////////////////////////////////////////////////////////////////////////////////
// WMISampleDevice::SystemControl
//
// Routine Description:
// Default handler for IRP_MJ_SYSTEM_CONTROL
//
// Parameters:
// I - Current IRP
//
// Return Value:
// NTSTATUS - Result returned from lower device
//
// Comments:
// This routine just passes the IRP through to the next device since this driver
// is not a WMI provider.
//
NTSTATUS WMISampleDevice::SystemControl(KIrp I)
{
ASSERT (m_Wmi != NULL);
if ( m_Wmi != NULL )
return m_Wmi->DispatchSystemControl(*this, I);
else
{
I.ForceReuseOfCurrentStackLocationInCalldown();
return m_Lower.PnpCall(this, I);
}
}
NTSTATUS WMISampleDevice::OnStartDevice(KIrp I)
{
NTSTATUS status = STATUS_SUCCESS;
xClicks=0;
I.Information() = 0;
ASSERT(m_Wmi != NULL);
// Perform WMI initialization
if ( m_Wmi != NULL )
{
// Here we set the method pointer array for the block which has associated
// methods. If we had other blocks for CIM classes with methods, those would
// also require a corresponding call to m_Wmi->SetMethods.
m_Wmi->SetMethods(1, ActivityMethods);
// Now register the blocks for this device with WMI.
status = m_Wmi->Register(*this, L"MofResource");
}
return status;
}
NTSTATUS WMISampleDevice::OnStopDevice(KIrp I)
{
NTSTATUS status = STATUS_SUCCESS;
ASSERT(m_Wmi != NULL);
if ( m_Wmi != NULL )
{
// This call tells WMI that the blocks for this device are unavailable.
m_Wmi->Deregister();
// This call to SetMethods releases the method pointer array.
m_Wmi->SetMethods(0, ActivityMethods);
}
return status;
}
NTSTATUS WMISampleDevice::OnRemoveDevice(KIrp I)
{
NTSTATUS status = STATUS_SUCCESS;
ASSERT(m_Wmi != NULL);
if ( m_Wmi != NULL )
{
// This call tells WMI that the blocks for this device are unavailable.
m_Wmi->Deregister();
// This call to SetMethods releases the method pointer array.
m_Wmi->SetMethods(0, ActivityMethods);
}
return status;
}
NTSTATUS WMISampleDevice::Create(KIrp I)
{
return I.PnpComplete(this, STATUS_SUCCESS, IO_NO_INCREMENT);
}
NTSTATUS WMISampleDevice::Close(KIrp I)
{
return I.PnpComplete(this, STATUS_SUCCESS, IO_NO_INCREMENT);
}
NTSTATUS WMISampleDevice::DeviceControl(KIrp I)
{
NTSTATUS status = STATUS_SUCCESS;
t << "DeviceControl\n";
switch (I.IoctlCode())
{
case INCX:
xClicks++;
break;
case DECX:
xClicks--;
break;
default:
// Unrecognized IOCTL request
status = STATUS_INVALID_PARAMETER;
break;
}
if ( m_SampleActivity.TestCollectionEnabled()) {
SampleActivity* pActivity=m_SampleActivity;
pActivity->m_xClicks=xClicks;
}
if ( m_SampleEvent.TestEventsEnabled()) {
SampleControl* pControl=m_SampleControl;
if (xClicks > pControl->m_xEventThreshold)
{
SampleEvent* pEvent = m_SampleEvent;
pEvent->m_xClicks = xClicks;
m_SampleEvent.FireEvent();
}
}
return I.PnpComplete(this, status);
}
/////////////////////////////////////////////////////////////////////
// KWmiDataBlock<SamplePdoInfo>::Query
//
// Specialization of KWmiDataBlock<>::Query for SamplePdoInfo
//
// Input
// pIrp current IRP
// BufferSize size of output buffer
// pPdoInfo output struct
// pBufferUsed addr of var to get count of bytes written
// Last true if last request for IRP
//
// Note
// This specialization of the template form is required because
// the block contains variably sized data ( a string ).
//
NTSTATUS KWmiDataBlock<SamplePdoInfo>::Query(
PIRP pIrp,
ULONG BufferSize,
SamplePdoInfo* pPdoInfo,
PULONG pBufferUsed,
BOOLEAN Last
)
{
USHORT usBufferSize = USHORT(BufferSize);
SamplePdoInfo* pInfo = *this;
USHORT* pOutput = reinterpret_cast<USHORT*>(pPdoInfo);
ULONG SizeRequired;
if ( (pInfo->m_pPdoName == NULL) || (pInfo->m_pPdoRegistryPath == NULL) )
{
*pOutput++ = 0;
*pOutput++ = 0;
*pBufferUsed = 2*sizeof(USHORT);
return STATUS_SUCCESS;
}
SizeRequired = pInfo->m_pPdoName->Size() + pInfo->m_pPdoRegistryPath->Size();
if (BufferSize < SizeRequired)
return STATUS_BUFFER_TOO_SMALL;
else
*pBufferUsed = SizeRequired;
pInfo->m_pPdoName->Get(pOutput, usBufferSize);
pInfo->m_pPdoRegistryPath->Get(pOutput, usBufferSize);
return STATUS_SUCCESS;
UNREFERENCED_PARAMETER(Last);
UNREFERENCED_PARAMETER(pIrp);
}
/////////////////////////////////////////////////////////////////////
// MouseFilterDevice::ResetActivityStatistics
//
// Zero out all stats
//
// Input
// pActivityStats pointer to activity stats struct
//
// Output
// all stats reset
//
// Notes:
// This routine implements a WMI method.
NTSTATUS WMISampleDevice::ResetActivityStatistics(
SampleActivity* pActivityStats,
ULONG inSize,
ULONG outSize,
PUCHAR Buffer,
PULONG pBufferUsed
)
{
xClicks=0;
pActivityStats->m_xClicks = 0;
*pBufferUsed = 0;
return STATUS_SUCCESS;
UNREFERENCED_PARAMETER(inSize);
UNREFERENCED_PARAMETER(outSize);
UNREFERENCED_PARAMETER(Buffer);
}
////////////////////////////////////////////////////////////////////////
// SamplePdoInfo::Initialize
//
// Initialization Method for SamplePdoInfo
//
NTSTATUS SamplePdoInfo::Initialize(PDEVICE_OBJECT Pdo)
{
WCHAR Scratch[100];
NTSTATUS status;
ULONG Length;
status = IoGetDeviceProperty(
Pdo,
DevicePropertyPhysicalDeviceObjectName,
sizeof Scratch - sizeof(WCHAR),
Scratch,
&Length
);
if ( !NT_SUCCESS(status) )
{
return status;
}
m_pPdoName = new (Scratch, static_cast<USHORT>(Length)) KWmiString();
status = IoGetDeviceProperty(
Pdo,
DevicePropertyDriverKeyName,
sizeof Scratch - sizeof(WCHAR),
Scratch,
&Length
);
if ( !NT_SUCCESS(status) )
{
return status;
}
m_pPdoRegistryPath = new(Scratch, static_cast<USHORT>(Length)) KWmiString();
return STATUS_SUCCESS;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?