核心模式驅動程式架構

核心模式驅動程式架構。Kernel-Mode Driver Framework目前支援下列型別的核心模式驅動程式之建立了：KMDF是可重新進入程式庫。與WDM的關係.自Windows 2000開始，開發驅動程式必以WDM為基礎的，但開發難度太大，無法像使用者模式應用程式開發那樣容易。KMDF支援驅動程式在Windows Driver Model環境中撰寫驅動程式，簡化其中的過程，但是KMDF的設計並不能用來取代WDM，它提供「Skeletal WDM」建置來替代WDM。早期的WDM可支援Windows 98、Windows Me、Windows 2000和Windows XP；至於WDF計劃支援Windows XP，以及更新的版本。KMDF係以物件為基底建立於WDM架構之上。不同的功能有不同的物件，KMDF在實作上包含了：驅動程式進入點.在Windows作業系統中驅動程式的起始點都是在DriverEntry函式，DriveryEntry是驅動程式的進入點你的WDFDRIVER物件，並且告知WDF framework要去哪裡呼叫你的系統。NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegistryPath{WDF_DRIVER_CONFIG config;NTSTATUS status = "S_OK";KdPrint((__DRIVER_NAME "DriverEntry Begin\n"));WDF_DRIVER_CONFIG_INIT(&amp;config, EvtDeviceAdd);status = WdfDriverCreate(DriverObject,RegistryPath,"WDF_NO_OBJECT_ATTRIBUTES",&amp;config, // Pointer to config structure"WDF_NO_HANDLE"); // or NULL, Pointer to get WDFDRIVER handleif(T_SUCCESS(status))KdPrint((__DRIVER_NAME "WdfDriverCreate failed with status 0x%08x\n", status));KdPrint((__DRIVER_NAME "DriverEntry End\n"));return status;Add Device.EvtDeviceAdd函式，在系統發現新硬體插入時被呼叫。這個函式將挑起WDF驅動程式架構的大部分工作，EvtDeviceAdd事件被喚起之餘一定會帶出一個WDFDRIVER物件，並且指向一個WDFDEVICE_INIT結構。在裝置產生之前，必先進行初始化的動作。如果EvtDeviceAdd執行成功，那麼EvtDevicePrepareHardware是架構下一個被執行的函式，用以保證驅動程式能夠訪問硬體。WDFSTATUS DioEvtDeviceAdd(WDFDRIVER Driver, PWDFDEVICE_INIT DeviceInit)WDFSTATUS status = STATUS_SUCCESS;WDF_PNPPOWER_EVENT_CALLBACKS pnpPowerCallbacks;WDF_OBJECT_ATTRIBUTES objAttributes;WDFDEVICE device;PDIO_DEVICE_CONTEXT devContext;WDF_IO_QUEUE_CONFIG ioCallbacks;WDF_INTERRUPT_CONFIG interruptConfig;WDF_DEVICE_POWER_POLICY_IDLE_SETTINGS idleSettings;WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&amp;pnpPowerCallbacks);pnpPowerCallbacks.EvtDevicePrepareHardware = DioEvtPrepareHardware;pnpPowerCallbacks.EvtDeviceReleaseHardware = DioEvtReleaseHardware;pnpPowerCallbacks.EvtDeviceD0Entry= DioEvtDeviceD0Entry;pnpPowerCallbacks.EvtDeviceD0Exit = DioEvtDeviceD0Exit;WdfDeviceInitSetPnpPowerEventCallbacks(DeviceInit, pnpPowerCallbacks);WDF_OBJECT_ATTRIBUTES_INIT(&amp;objAttributes);WDF_OBJECT_ATTRIBUTES_SET_CONTEXT_TYPE(&amp;objAttributes, DIO_DEVICE_CONTEXT);status = WdfDeviceInitUpdateName(DeviceInit, L"\\device\\WDFDIO");status = WdfDeviceCreate(&amp;DeviceInit, // Device Init structure&amp;objAttributes, // Attributes for WDF Device&amp;device); // return new WDF Device pointer,devContext = DioGetContextFromDevice(device); // Get device extensiondevContext-&gt;WdfDevice = device;// Create a symbolic link for the control objectstatus = WdfDeviceCreateSymbolicLink(device, L"\\DosDevices\\WDFDIO");WDF_IO_QUEUE_CONFIG_INIT(&amp;ioCallbacks,WdfIoQueueDispatchSerial,WDF_NO_EVENT_CALLBACK, // StartIoWDF_NO_EVENT_CALLBACK); // CancelRoutineioCallbacks.EvtIoDeviceControl = DioEvtDeviceControlIoctl;status = WdfDeviceCreateDefaultQueue(device,&amp;ioCallbacks,"WDF_NO_OBJECT_ATTRIBUTES",NULL); // pointer to default queueWDF_INTERRUPT_CONFIG_INIT(&amp;interruptConfig, // Configure the Interrupt objectFALSE, // auto-queue DPC?DioIsr, // ISRDioDpc); // Defered Procedule CallinterruptConfig.EvtInterruptEnable = DioEvtInterruptEnable;interruptConfig.EvtInterruptDisable = DioEvtInterruptDisable;status = WdfInterruptCreate(device,&amp;interruptConfig,&amp;objAttributes,&amp;devContext-&gt;WdfInterrupt);WDF_DEVICE_POWER_POLICY_IDLE_SETTINGS_INIT(&amp;idleSettings, // Initialize idle policyIdleCannotWakeFromS0);status = WdfDeviceUpdateS0IdleSettings(device, &amp;idleSettings);return status;Prepare Hardware.如果EvtDeviceAdd順利執行成功，那麼EvtDevicePrepareHardware是架構下一個被執行的函式，用以保證驅動程式能夠訪問硬體。NTSTATUS EvtDevicePrepareHardware(IN WDFDEVICE Device,IN WDFCMRESLIST ResourceList,IN WDFCMRESLIST ResourceListTranslated{NTSTATUS status = STATUS_SUCCESS;UNREFERENCED_PARAMETER(Device);UNREFERENCED_PARAMETER(ResourceList);UNREFERENCED_PARAMETER(ResourceListTranslated);return status;NTSTATUS EvtDeviceD0Entry(IN WDFDEVICE Device,IN WDF_POWER_DEVICE_STATE PreviousState{NTSTATUS status = STATUS_SUCCESS;return status;NTSTATUS EvtDeviceD0Exit(IN WDFDEVICE Device,IN WDF_POWER_DEVICE_STATE TargetState{NTSTATUS status = STATUS_SUCCESS;return status;IO requests.VOID EvtDeviceIoDefault(IN WDFQUEUE Queue,IN WDFREQUEST Request{WdfRequestComplete(Request, STATUS_NOT_IMPLEMENTED);