📄 hellodev.cpp
字号:
// Code for device class of most basic driver
//=============================================================================
//
// Compuware Corporation
// NuMega Lab
// 9 Townsend West
// Nashua, NH 03060 USA
//
// Copyright (c) 1998 Compuware Corporation. All Rights Reserved.
// Unpublished - rights reserved under the Copyright laws of the
// United States.
//
//=============================================================================
#include <vdw.h>
#include "HelloWdm.h"
#include "HelloDev.h"
#include "HelloDI.h"
GUID HelloDevGuid = HELLO_CLASS_GUID;
///////////////////////////////////////////////////////////////////////////////
// Constructor for subclass of KPnpDevice
//
SimpleWdmDevice::SimpleWdmDevice(PDEVICE_OBJECT Pdo, ULONG Unit) :
KPnpDevice(
Pdo,
KUnitizedName(L"HelloWdm", Unit),
FILE_DEVICE_UNKNOWN,
&HelloDevGuid,
0,
0
)
{
// check for succes of base class construction
if ( ! NT_SUCCESS(m_ConstructorStatus) )
{
T << TraceError
<< "Failed to create device, status="
<< ULONG(m_ConstructorStatus)
<< "\n";
return;
}
// Attach the PDO as a lower device
m_Pdo.Initialize(this, Pdo);
// Establish m_Pdo as the lower device object
SetLowerDevice(&m_Pdo);
// Take the standard PnP Policy
SetPnpPolicy();
}
///////////////////////////////////////////////////////////////////////////////
//
NTSTATUS SimpleWdmDevice::Create(KIrp I)
{
NTSTATUS status = STATUS_SUCCESS;
T << "In the Create handler\n";
// An application (or other driver) is opening the device. The driver can
// store handle specific data at I.FileObject()->FsContext if required.
// TODO: Add driver specific create handling code here
// Complete the request. By using PnpComplete rather than simply Complete,
// we keep track of outstanding requests. If we get a remove notification,
// we can defer deletion of the device object until all requests complete.
return I.PnpComplete(this, status, IO_NO_INCREMENT);
}
///////////////////////////////////////////////////////////////////
// Close handler
//
NTSTATUS SimpleWdmDevice::Close(KIrp I)
{
NTSTATUS status = STATUS_SUCCESS;
// Complete the request
return I.PnpComplete(this, status, IO_NO_INCREMENT);
}
///////////////////////////////////////////////////////////////////
// DeviceControl handler
//
NTSTATUS SimpleWdmDevice::DeviceControl(KIrp I)
{
NTSTATUS status = STATUS_SUCCESS;
T << "DeviceControl, code=" << I.IoctlCode() << "\n";
switch (I.IoctlCode())
{
#pragma warning(disable:4060)
// add case stmts here
}
I.Information() = 0;
// It is important to complete the IRP with PnpComplete rather than
// Complete, because doing so enables the base class to track outstanding
// IRPs. This is required for safe removal and clean up.
return I.PnpComplete(this, status);
}
///////////////////////////////////////////////////////////////////
// OnStartDevice
//
// This call instructs the device to initialize itself
//
NTSTATUS SimpleWdmDevice::OnStartDevice(KIrp I)
{
NTSTATUS status = STATUS_SUCCESS;
I.Information() = 0;
// The base class handles passing the IRP to the PDO, and will
// not call this routine if the PDO indicates a failure occurred.
// In addition, our PnP policy is set up to automatically enable
// the device interface when the device is started.
// If the device has hardware resources, make the following call
// PCM_RESOURCE_LIST pResList = I.TranslatedResources();
//
// Then use pResList to initialize the resources. Refer to the PCI/WDM
// example to see how this is done.
// The base class will handle completion
return status;
}
///////////////////////////////////////////////////////////////////
// OnStopDevice
//
// This call instructs the device to uninitialize itself. The system
// stops a device for the purpose of reconfiguration. It is likely
// that the device will subsequently receive an IRP_MN_START.
//
NTSTATUS SimpleWdmDevice::OnStopDevice(KIrp I)
{
NTSTATUS status = STATUS_SUCCESS;
// TODO: Add code to disconnect interrupts, destroy memory or I/O
// ranges, or dismantle anything else initialized in OnStartDevice.
// The base class handles disabling of the device interface and forwarding
// to the PDO
return status;
}
///////////////////////////////////////////////////////////////////
// OnRemoveDevice
//
// This call notifies the driver that the device has been removed.
// It is not necessarily preceeded by QueryRemove or Stop
//
NTSTATUS SimpleWdmDevice::OnRemoveDevice(KIrp I)
{
NTSTATUS status = STATUS_SUCCESS;
T << "entered OnRemoveDevice() \n";
// TODO: add device specific removal code
// Our PnP policy will take care of
// (1) giving the IRP to the PDO
// (2) detaching the PDO
// (3) deleting the device object when it is safe to do so
return status;
}
///////////////////////////////////////////////////////////////////
// OnQueryRemoveDevice
//
// This call notifies the driver that the device has been removed.
//
NTSTATUS SimpleWdmDevice::OnQueryRemoveDevice(KIrp I)
{
NTSTATUS status = STATUS_SUCCESS;
T << "entered OnQueryRemoveDevice() \n";
// The default Pnp policy fails the request before even calling this
// handler if the device is open.
// TODO: prepare to allow removal if ok to do so
// If this handler returns STATUS_SUCCESS, the default Pnp policy causes
// any subsequent I/O requests to be put into the hold queue (deferred
// request queue).
// The PnP policy can be configured to cancel the current request
// or outstanding requests in the device queue (if any) upon successful return
// from this handler.
// The default policy causes the IRP to be forwarded to (and completed by)
// the lower device (PDO).
return status;
}
///////////////////////////////////////////////////////////////////
// DefaultPnp
//
NTSTATUS SimpleWdmDevice::DefaultPnp(KIrp I)
{
I.ForceReuseOfCurrentStackLocationInCalldown();
return m_Pdo.PnpCall(this, I);
}
///////////////////////////////////////////////////////////////////
// DefaultPower
//
NTSTATUS SimpleWdmDevice::DefaultPower(KIrp I)
{
I.IndicatePowerIrpProcessed();
I.CopyParametersDown();
return m_Pdo.PnpPowerCall(this, I);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -