DriverEntry

在驱动被加载后DriverEntry是第一个被驱动程序调用的函数,它负责初始化驱动程序。

NTSTATUS DriverEntry(
  _In_  PDRIVER_OBJECT DriverObject,
  _In_  PUNICODE_STRING RegistryPath
);

参数

DriverObject [in]

指向一个 DRIVER_OBJECT 结构体,它是WDM驱动的对象

RegistryPath [in]

指向一个 UNICODE_STRING 结构体,他指定了驱动关键参数在注册表中的路径

返回值

如果程序返回成功,返回值为STATUS_SUCCESS。否则返回ntstatus.h文件中的一个错误。


备注

与WMD驱动一样,基于框架的驱动必须有一个DriverEntry例程(以下简称函数吧,有点拗口),DriverEntry函数式驱动加载后被调用。

一个基于框架驱动DriverEntry函数必须满足以下条件:

1. 激活WPP软件跟踪。
    DriverEntry应该包含一个WPP_INIT_TRACING宏来激活软件跟踪(如:WPP_INIT_TRACING( DriverObject, RegistryPath );)。

2. 调用WdfDriverCreate。

    (1) 驱动程序进入DriverEntry后,调用WdfDriverCreate函数之前必须设置WDF_DRIVER_CONFIG的结构体中的EvtDriverDeviceAdd回调函数(即插即用 (PnP) 管理器向设备分配系统资源(如中断矢量)之前,框架会调用驱动程序的 EvtDriverDeviceAdd 回调函数)地址。

    (2) 调用WdfDriverCreate函数后才能够使用windows驱动框架其他接口(在调用WdfDriverCreate之前驱动不能调用框架其他函数接口)。

3. 分配任意非特定设备的系统资源和全局变量,可能需要它。

    典型的,驱动以及各个设备相关的系统资源。因此,绝大多数基于框架的驱动在EvtDriverDeviceAdd回调函数中分配资源,当设备被检测到时EvtDriverDeviceAdd被调用。关于如何设置/初始化EvtDriverDeviceAdd,见WDF_DRIVER_CONFIG

    Because multiple instances of a UMDF driver might be hosted by separate instances of Wudfhost, a global variable might not be available across all instances of a UMDF driver.

4. 从注册表中获取驱动程序特定的参数。

    一些驱动从注册表中查询参数,这些驱动可以调用WdfDriverOpenParametersRegistryKey打开注册表包含这些参数键。

5. 提供返回值


例:

#include "driver.h"
#include "driver.tmh"

#ifdef ALLOC_PRAGMA
#pragma alloc_text (INIT, DriverEntry)
#pragma alloc_text (PAGE, KMDFTestEvtDeviceAdd)
#pragma alloc_text (PAGE, KMDFTestEvtDriverContextCleanup)
#endif


NTSTATUS
DriverEntry(
    _In_ PDRIVER_OBJECT  DriverObject,
    _In_ PUNICODE_STRING RegistryPath
    )
/*++

Routine Description:
    DriverEntry initializes the driver and is the first routine called by the
    system after the driver is loaded. DriverEntry specifies the other entry
    points in the function driver, such as EvtDevice and DriverUnload.

Parameters Description:

    DriverObject - represents the instance of the function driver that is loaded
    into memory. DriverEntry must initialize members of DriverObject before it
    returns to the caller. DriverObject is allocated by the system before the
    driver is loaded, and it is released by the system after the system unloads
    the function driver from memory.

    RegistryPath - represents the driver specific path in the Registry.
    The function driver can use the path to store driver related data between
    reboots. The path does not store hardware instance specific data.

Return Value:

    STATUS_SUCCESS if successful,
    STATUS_UNSUCCESSFUL otherwise.

--*/
{
    WDF_DRIVER_CONFIG config;
    NTSTATUS status;
    WDF_OBJECT_ATTRIBUTES attributes;

    //
    // Initialize WPP Tracing
    //
    WPP_INIT_TRACING( DriverObject, RegistryPath );

    TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry");

    //
    // Register a cleanup callback so that we can call WPP_CLEANUP when
    // the framework driver object is deleted during driver unload.
    //
    WDF_OBJECT_ATTRIBUTES_INIT(&attributes);
    attributes.EvtCleanupCallback = KMDFTestEvtDriverContextCleanup;

    WDF_DRIVER_CONFIG_INIT(&config,
                           KMDFTestEvtDeviceAdd
                           );

	

    status = WdfDriverCreate(DriverObject,
                             RegistryPath,
                             &attributes,
                             &config,
                             WDF_NO_HANDLE
                             );

    if (!NT_SUCCESS(status)) {
        TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, "WdfDriverCreate failed %!STATUS!", status);
        WPP_CLEANUP(DriverObject);
        return status;
    }

    TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Exit");

    return status;
}

NTSTATUS
KMDFTestEvtDeviceAdd(
    _In_    WDFDRIVER       Driver,
    _Inout_ PWDFDEVICE_INIT DeviceInit
    )
/*++
Routine Description:

    EvtDeviceAdd is called by the framework in response to AddDevice
    call from the PnP manager. We create and initialize a device object to
    represent a new instance of the device.

Arguments:

    Driver - Handle to a framework driver object created in DriverEntry

    DeviceInit - Pointer to a framework-allocated WDFDEVICE_INIT structure.

Return Value:

    NTSTATUS

--*/
{
    NTSTATUS status;

    UNREFERENCED_PARAMETER(Driver);

    PAGED_CODE();

    TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry");

    status = KMDFTestCreateDevice(DeviceInit);

    TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Exit");

    return status;
}

VOID
KMDFTestEvtDriverContextCleanup(
    _In_ WDFOBJECT DriverObject
    )
/*++
Routine Description:

    Free all the resources allocated in DriverEntry.

Arguments:

    DriverObject - handle to a WDF Driver object.

Return Value:

    VOID.

--*/
{
    UNREFERENCED_PARAMETER(DriverObject);

    PAGED_CODE ();

    TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry");

    //
    // Stop WPP Tracing
    //
    WPP_CLEANUP( WdfDriverWdmGetDriverObject(DriverObject) );

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值