
_START_ARTICLE_
核心模式驅動程式架構
_START_SECTION_
驱动程序进入点
_START_PARAGRAPH_
在Windows操作系统中驱动程序的起始点都是在DriverEntry函数，DriveryEntry是驱动程序的进入点（entry point）。在DriverEntry函数的实现里，你需要具现化（instantiate）你的WDFDRIVER对象，并且告知WDF framework要去哪里调用你的系统。_NEWLINE_NTSTATUS DriverEntry(_NEWLINE_   IN PDRIVER_OBJECT  DriverObject,_NEWLINE_   IN PUNICODE_STRING  RegistryPath_NEWLINE_   )_NEWLINE_{_NEWLINE_ WDF_DRIVER_CONFIG config;_NEWLINE_ NTSTATUS status = S_OK;_NEWLINE_ KdPrint((__DRIVER_NAME "DriverEntry Begin\n"));_NEWLINE_ WDF_DRIVER_CONFIG_INIT(&config, EvtDeviceAdd);_NEWLINE_ status = WdfDriverCreate(_NEWLINE_                     DriverObject,_NEWLINE_                     RegistryPath,_NEWLINE_                     WDF_NO_OBJECT_ATTRIBUTES,_NEWLINE_                     &config, // Pointer to config structure_NEWLINE_                     WDF_NO_HANDLE); // or NULL, Pointer to get WDFDRIVER handle_NEWLINE_ if(T_SUCCESS(status))_NEWLINE_ {_NEWLINE_   KdPrint((__DRIVER_NAME "WdfDriverCreate failed with status 0x%08x\n", status));_NEWLINE_ }_NEWLINE_ KdPrint((__DRIVER_NAME "DriverEntry End\n"));_NEWLINE_ return status;_NEWLINE_}
_START_SECTION_
Add Device
_START_PARAGRAPH_
EvtDeviceAdd函数，在系统发现新硬件插入时被调用。这个函数将挑起WDF驱动程序框架的大部分工作，EvtDeviceAdd事件被唤起之余一定会带出一个WDFDRIVER对象，并且指向一个WDFDEVICE_INIT结构。在设备产生（device crated）之前，必先进行初始化的动作。如果EvtDeviceAdd运行成功，那么EvtDevicePrepareHardware是框架下一个被运行的函数，用以保证驱动程序能够访问硬件。_NEWLINE_WDFSTATUS DioEvtDeviceAdd(WDFDRIVER Driver, PWDFDEVICE_INIT DeviceInit)_NEWLINE_{_NEWLINE_ WDFSTATUS status = STATUS_SUCCESS;_NEWLINE_ WDF_PNPPOWER_EVENT_CALLBACKS pnpPowerCallbacks;_NEWLINE_ WDF_OBJECT_ATTRIBUTES objAttributes;_NEWLINE_ WDFDEVICE device;_NEWLINE_ PDIO_DEVICE_CONTEXT devContext;_NEWLINE_ WDF_IO_QUEUE_CONFIG ioCallbacks;_NEWLINE_ WDF_INTERRUPT_CONFIG interruptConfig;_NEWLINE_ WDF_DEVICE_POWER_POLICY_IDLE_SETTINGS idleSettings;_NEWLINE_ WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&pnpPowerCallbacks);_NEWLINE_ pnpPowerCallbacks.EvtDevicePrepareHardware = DioEvtPrepareHardware;_NEWLINE_ pnpPowerCallbacks.EvtDeviceReleaseHardware = DioEvtReleaseHardware;_NEWLINE_ pnpPowerCallbacks.EvtDeviceD0Entry= DioEvtDeviceD0Entry;_NEWLINE_ pnpPowerCallbacks.EvtDeviceD0Exit = DioEvtDeviceD0Exit;_NEWLINE_ WdfDeviceInitSetPnpPowerEventCallbacks(DeviceInit, pnpPowerCallbacks);_NEWLINE_ WDF_OBJECT_ATTRIBUTES_INIT(&objAttributes);_NEWLINE_ WDF_OBJECT_ATTRIBUTES_SET_CONTEXT_TYPE(&objAttributes, DIO_DEVICE_CONTEXT);_NEWLINE_ status = WdfDeviceInitUpdateName(DeviceInit, L"\\device\\WDFDIO");_NEWLINE_ status = WdfDeviceCreate(&DeviceInit,    // Device Init structure_NEWLINE_                          &objAttributes, // Attributes for WDF Device_NEWLINE_                          &device);       // return new WDF Device pointer,_NEWLINE_ devContext = DioGetContextFromDevice(device); // Get device extension_NEWLINE_ devContext->WdfDevice = device;_NEWLINE_ // Create a symbolic link for the control object_NEWLINE_ status = WdfDeviceCreateSymbolicLink(device, L"\\DosDevices\\WDFDIO");_NEWLINE_ WDF_IO_QUEUE_CONFIG_INIT(&ioCallbacks,_NEWLINE_                            WdfIoQueueDispatchSerial,_NEWLINE_                            WDF_NO_EVENT_CALLBACK,     // StartIo_NEWLINE_                            WDF_NO_EVENT_CALLBACK);    // CancelRoutine_NEWLINE_ ioCallbacks.EvtIoDeviceControl = DioEvtDeviceControlIoctl;_NEWLINE_ status = WdfDeviceCreateDefaultQueue(device,_NEWLINE_                                       &ioCallbacks,_NEWLINE_                                       WDF_NO_OBJECT_ATTRIBUTES,_NEWLINE_                                       NULL); // pointer to default queue_NEWLINE_ WDF_INTERRUPT_CONFIG_INIT(&interruptConfig,       // Configure the Interrupt object_NEWLINE_                             FALSE,                // auto-queue DPC?_NEWLINE_                             DioIsr,               // ISR_NEWLINE_                             DioDpc);              // Defered Procedule Call_NEWLINE_ interruptConfig.EvtInterruptEnable = DioEvtInterruptEnable;_NEWLINE_ interruptConfig.EvtInterruptDisable = DioEvtInterruptDisable;_NEWLINE_ status = WdfInterruptCreate(device,_NEWLINE_                             &interruptConfig,_NEWLINE_                             &objAttributes,_NEWLINE_                             &devContext->WdfInterrupt);_NEWLINE_ WDF_DEVICE_POWER_POLICY_IDLE_SETTINGS_INIT(&idleSettings,  // Initialize idle policy_NEWLINE_                                             IdleCannotWakeFromS0);_NEWLINE_ status = WdfDeviceUpdateS0IdleSettings(device, &idleSettings);_NEWLINE_ return status;_NEWLINE_}
_START_SECTION_
Prepare Hardware
_START_PARAGRAPH_
如果EvtDeviceAdd顺利运行成功，那么EvtDevicePrepareHardware是框架下一个被运行的函数，用以保证驱动程序能够访问硬件。_NEWLINE_NTSTATUS EvtDevicePrepareHardware(_NEWLINE_   IN WDFDEVICE    Device,_NEWLINE_   IN WDFCMRESLIST ResourceList,_NEWLINE_   IN WDFCMRESLIST ResourceListTranslated_NEWLINE_   )_NEWLINE_{_NEWLINE_ NTSTATUS status = STATUS_SUCCESS;_NEWLINE_ UNREFERENCED_PARAMETER(Device);_NEWLINE_ UNREFERENCED_PARAMETER(ResourceList);_NEWLINE_ UNREFERENCED_PARAMETER(ResourceListTranslated);_NEWLINE_ return status;_NEWLINE_}_NEWLINE_NTSTATUS EvtDeviceD0Entry(_NEWLINE_   IN WDFDEVICE  Device,_NEWLINE_   IN WDF_POWER_DEVICE_STATE  PreviousState_NEWLINE_   )_NEWLINE_{_NEWLINE_ NTSTATUS status = STATUS_SUCCESS;_NEWLINE_ return status;_NEWLINE_}_NEWLINE_NTSTATUS EvtDeviceD0Exit(_NEWLINE_   IN WDFDEVICE  Device,_NEWLINE_   IN WDF_POWER_DEVICE_STATE  TargetState_NEWLINE_   )_NEWLINE_{_NEWLINE_ NTSTATUS status = STATUS_SUCCESS;_NEWLINE_ return status;_NEWLINE_}
_START_SECTION_
IO requests
_START_PARAGRAPH_
VOID EvtDeviceIoDefault(_NEWLINE_   IN WDFQUEUE  Queue,_NEWLINE_   IN WDFREQUEST  Request_NEWLINE_   )_NEWLINE_{_NEWLINE_ WdfRequestComplete(Request, STATUS_NOT_IMPLEMENTED);_NEWLINE_}