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

📄 power.c

📁 一个简单的wdm驱动开发实例,可以大概看开发流程作为入门
💻 C
字号:
// power.c
//
// Generated by C DriverWizard 3.1.0 (Build 1722)
// Requires DDK Only
// File created on 1/23/2009
//

#include "pch.h"
#ifdef CHARSAMPLE_WMI_TRACE
#include "power.tmh"
#endif

///////////////////////////////////////////////////////////////////////////////////////////////////
//  CharSamplePowerDispatch 
//      Dispatch routine for IRPs of IRP_MJ_POWER type
//
//  Arguments:
//      IN  DeviceObject
//              Device object for our driver
//
//      IN  Irp
//              The power IRP to handle
//
//  Return Value:
//      Status
//
NTSTATUS CharSamplePowerDispatch(
    IN  PDEVICE_OBJECT  DeviceObject,
    IN  PIRP            Irp
    )
{
    PCHARSAMPLE_DEVICE_EXTENSION    deviceExtension;
    NTSTATUS                        status;
    PIO_STACK_LOCATION              irpStack;

    CharSampleDebugPrint(DBG_POWER, DBG_TRACE, __FUNCTION__"++. IRP %p", Irp);

    CharSampleDumpIrp(Irp);

    // Get our current IRP stack location
    irpStack = IoGetCurrentIrpStackLocation(Irp);

    // Get our device extension
    deviceExtension = (PCHARSAMPLE_DEVICE_EXTENSION)DeviceObject->DeviceExtension;

    // check if device has been removed
    if (!CharSampleAcquireRemoveLock(deviceExtension))
    {
        status = STATUS_NO_SUCH_DEVICE;

        PoStartNextPowerIrp(Irp);
        Irp->IoStatus.Status = status;
        IoCompleteRequest(Irp, IO_NO_INCREMENT);

        CharSampleDebugPrint(DBG_POWER, DBG_WARN, __FUNCTION__"--. IRP %p STATUS %x", Irp, status);

        return status;
    }

    // If our device has not been started, we just pass the 
    // power requests down the stack
    if (deviceExtension->PnpState == PnpStateNotStarted) 
    {
        PoStartNextPowerIrp(Irp);
        IoSkipCurrentIrpStackLocation(Irp);

        status = PoCallDriver(deviceExtension->LowerDeviceObject, Irp);

        // Release Remove Lock
        CharSampleReleaseRemoveLock(deviceExtension);

        CharSampleDebugPrint(DBG_POWER, DBG_WARN, __FUNCTION__"--. IRP %p STATUS %x", Irp, status);

        return status;
    }

    // Determine the power IRP type
    switch(irpStack->MinorFunction)
    {
    case IRP_MN_QUERY_POWER:
    case IRP_MN_SET_POWER:
    case IRP_MN_WAIT_WAKE:
    case IRP_MN_POWER_SEQUENCE:
    default:
        // default case, just send the IRP down and let other drivers
        // in the stack handle it

        // Let the power manager know we can handle another
        // power IRP of this type
        PoStartNextPowerIrp(Irp);

        // send the IRP down the stack
        IoSkipCurrentIrpStackLocation(Irp);

        // Drivers must use PoCallDriver, rather than IoCallDriver,
        // to pass power IRPs. PoCallDriver allows the Power Manager
        // to ensure that power IRPs are properly synchronized throughout
        // the system.
        status = PoCallDriver(deviceExtension->LowerDeviceObject, Irp);

        // Adjust our IRP count
        CharSampleReleaseRemoveLock(deviceExtension);

        break;
    }

    CharSampleDebugPrint(DBG_POWER, DBG_TRACE, __FUNCTION__"--. IRP %p STATUS %x", Irp, status);
    return status;
}

⌨️ 快捷键说明

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