Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2585253
  • 博文数量: 877
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 5920
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-05 12:25
个人简介

技术的乐趣在于分享,欢迎多多交流,多多沟通。

文章分类

全部博文(877)

文章存档

2021年(2)

2016年(20)

2015年(471)

2014年(358)

2013年(26)

分类: 嵌入式

2015-04-23 19:07:29


http://blog.csdn.net/lh806732/article/details/24270165

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


  1. NTSTATUS DriverEntry(  
  2.   _In_  PDRIVER_OBJECT DriverObject,  
  3.   _In_  PUNICODE_STRING RegistryPath  
  4. );  


参数

DriverObject [in]

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

RegistryPath [in]

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



返回值


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



备注


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

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

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

2. 调用WdfDriverCreate。

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

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

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

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

    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. 提供返回值


例:


  1. #include "driver.h"  
  2. #include "driver.tmh"  
  3.   
  4. #ifdef ALLOC_PRAGMA  
  5. #pragma alloc_text (INIT, DriverEntry)  
  6. #pragma alloc_text (PAGE, KMDFTestEvtDeviceAdd)  
  7. #pragma alloc_text (PAGE, KMDFTestEvtDriverContextCleanup)  
  8. #endif  
  9.   
  10.   
  11. NTSTATUS  
  12. DriverEntry(  
  13.     _In_ PDRIVER_OBJECT  DriverObject,  
  14.     _In_ PUNICODE_STRING RegistryPath  
  15.     )  
  16. /*++ 
  17.  
  18. Routine Description: 
  19.     DriverEntry initializes the driver and is the first routine called by the 
  20.     system after the driver is loaded. DriverEntry specifies the other entry 
  21.     points in the function driver, such as EvtDevice and DriverUnload. 
  22.  
  23. Parameters Description: 
  24.  
  25.     DriverObject - represents the instance of the function driver that is loaded 
  26.     into memory. DriverEntry must initialize members of DriverObject before it 
  27.     returns to the caller. DriverObject is allocated by the system before the 
  28.     driver is loaded, and it is released by the system after the system unloads 
  29.     the function driver from memory. 
  30.  
  31.     RegistryPath - represents the driver specific path in the Registry. 
  32.     The function driver can use the path to store driver related data between 
  33.     reboots. The path does not store hardware instance specific data. 
  34.  
  35. Return Value: 
  36.  
  37.     STATUS_SUCCESS if successful, 
  38.     STATUS_UNSUCCESSFUL otherwise. 
  39.  
  40. --*/  
  41. {  
  42.     WDF_DRIVER_CONFIG config;  
  43.     NTSTATUS status;  
  44.     WDF_OBJECT_ATTRIBUTES attributes;  
  45.   
  46.     //  
  47.     // Initialize WPP Tracing  
  48.     //  
  49.     WPP_INIT_TRACING( DriverObject, RegistryPath );  
  50.   
  51.     TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry");  
  52.   
  53.     //  
  54.     // Register a cleanup callback so that we can call WPP_CLEANUP when  
  55.     // the framework driver object is deleted during driver unload.  
  56.     //  
  57.     WDF_OBJECT_ATTRIBUTES_INIT(&attributes);  
  58.     attributes.EvtCleanupCallback = KMDFTestEvtDriverContextCleanup;  
  59.   
  60.     WDF_DRIVER_CONFIG_INIT(&config,  
  61.                            KMDFTestEvtDeviceAdd  
  62.                            );  
  63.   
  64.       
  65.   
  66.     status = WdfDriverCreate(DriverObject,  
  67.                              RegistryPath,  
  68.                              &attributes,  
  69.                              &config,  
  70.                              WDF_NO_HANDLE  
  71.                              );  
  72.   
  73.     if (!NT_SUCCESS(status)) {  
  74.         TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, "WdfDriverCreate failed %!STATUS!", status);  
  75.         WPP_CLEANUP(DriverObject);  
  76.         return status;  
  77.     }  
  78.   
  79.     TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Exit");  
  80.   
  81.     return status;  
  82. }  
  83.   
  84. NTSTATUS  
  85. KMDFTestEvtDeviceAdd(  
  86.     _In_    WDFDRIVER       Driver,  
  87.     _Inout_ PWDFDEVICE_INIT DeviceInit  
  88.     )  
  89. /*++ 
  90. Routine Description: 
  91.  
  92.     EvtDeviceAdd is called by the framework in response to AddDevice 
  93.     call from the PnP manager. We create and initialize a device object to 
  94.     represent a new instance of the device. 
  95.  
  96. Arguments: 
  97.  
  98.     Driver - Handle to a framework driver object created in DriverEntry 
  99.  
  100.     DeviceInit - Pointer to a framework-allocated WDFDEVICE_INIT structure. 
  101.  
  102. Return Value: 
  103.  
  104.     NTSTATUS 
  105.  
  106. --*/  
  107. {  
  108.     NTSTATUS status;  
  109.   
  110.     UNREFERENCED_PARAMETER(Driver);  
  111.   
  112.     PAGED_CODE();  
  113.   
  114.     TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry");  
  115.   
  116.     status = KMDFTestCreateDevice(DeviceInit);  
  117.   
  118.     TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Exit");  
  119.   
  120.     return status;  
  121. }  
  122.   
  123. VOID  
  124. KMDFTestEvtDriverContextCleanup(  
  125.     _In_ WDFOBJECT DriverObject  
  126.     )  
  127. /*++ 
  128. Routine Description: 
  129.  
  130.     Free all the resources allocated in DriverEntry. 
  131.  
  132. Arguments: 
  133.  
  134.     DriverObject - handle to a WDF Driver object. 
  135.  
  136. Return Value: 
  137.  
  138.     VOID. 
  139.  
  140. --*/  
  141. {  
  142.     UNREFERENCED_PARAMETER(DriverObject);  
  143.   
  144.     PAGED_CODE ();  
  145.   
  146.     TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry");  
  147.   
  148.     //  
  149.     // Stop WPP Tracing  
  150.     //  
  151.     WPP_CLEANUP( WdfDriverWdmGetDriverObject(DriverObject) );  
  152.   
  153. }  

阅读(1221) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~