Windows Driver Development-8

Source: Internet
Author: User

The last bit of development work: tracking drivers.


One, drive tracking 1. Include TMH header file

#include "STEP5.TMH"

2. Initialize the trace

Initialize in DriverEntry.

    Wpp_init_tracing (DriverObject, registrypath);    Wdf_object_attributes_init (&attributes);    Attributes. Evtcleanupcallback = Evtdrivercontextcleanup;


(1) WPP tracking initialization

Wpp_init_tracing

VOID  wpp_init_tracing (    in Pdriver_object  driverobject,    in OPT punicode_string  Registrypath    );

(2) WDF object property Initialization

Wdf_object_attributes_init

VOID wdf_object_attributes_init (  _out_ pwdf_object_attributes ATTRIBUTES);

(3) Setting callback events

Evtdrivercontextcleanup

Evt_wdf_object_context_cleanup Evtcleanupcallback; VOID evtcleanupcallback (  _in_ wdfobject Object) {...}



3. Cleanup tracking

Use the WPP_CLEANUP macro.

VOID Wpp_cleanup (in    pdriver_object  driverobject);

(1) Cleaning up WDM driver objects

Wdfdriverwdmgetdriverobject

Pdriver_object Wdfdriverwdmgetdriverobject (  [in] wdfdriver Driver);




Windows Driver Development PostScript session:

We have finished our development work. But. This is not a driver for the working state.

Subsequent IRQL, evtiostop, power supply refinement, iotarget (including Evtiodevicecontrol), INF files, and so on.


Report:

Step5.c

/*++step5:this steps shows:1) How to map Kdprint function to does WPP tracing--*/#include <stdarg.h>/to AVO   ID build errors on Win2K due to Wpp#include "ntddk.h" #include "wdf.h" #include "prototypes.h" #pragma warning (disable:4200)  Suppress nameless struct/union warning#pragma warning (disable:4201)//Suppress nameless struct/union warning#pragma Warning (disable:4214)//Suppress bit field types other than int warning#include "usbdi.h" #pragma warning (default:4200) # pragma warning (default:4201) #pragma warning (default:4214) #include "wdfusb.h" #include "initguid.h" #include " STEP5.TMH "Define_guid (GUID_DEVINTERFACE_OSRUSBFX2,//Generated using Guidgen.exe 0x573e8c73, 0XCB4, 0x4471, 0xa1, 0XBF , 0xfa, 0xb2, 0x6c, 0x31, 0xd3, 0x84);//{573e8c73-0cb4-4471-a1bf-fab26c31d384} #define IOCTL_INDEX 0x8        00#define file_device_osrusbfx2 0x65500#define usbfx2lk_set_bargraph_display 0xd8#define BULK_OUT_ENDPOINT_INDEX   1#define Bulk_in_endpoint_index      2#define Ioctl_osrusbfx2_set_bar_graph_display Ctl_code (FILE_DEVICE_OSRUSBFX2,                                                     Ioctl_index + 5, method_buffered,  file_write_access) typedef struct _DEVICE_CONTEXT {Wdfusbdevice usbdevice;  Wdfusbinterface Usbinterface;  Wdfusbpipe Bulkreadpipe; Wdfusbpipe Bulkwritepipe;} Device_context, *pdevice_context;  Wdf_declare_context_type_with_name (Device_context, Getdevicecontext) ntstatusdriverentry (in PDRIVER_OBJECT    DriverObject, in punicode_string registrypath) {wdf_driver_config CONFIG;    NTSTATUS status;    Wdf_object_attributes ATTRIBUTES;    Wpp_init_tracing (DriverObject, Registrypath);    Kdprint (("DriverEntry of Step5\n"));    Wdf_driver_config_init (&config, Evtdeviceadd); Register a cleanup callback so then we can call Wpp_cleanup when//the FramEwork driver object is deleted during driver unload.    Wdf_object_attributes_init (&attributes); Attributes.    Evtcleanupcallback = Evtdrivercontextcleanup;                        Status = Wdfdrivercreate (DriverObject, Registrypath, &attributes,    &config, Wdf_no_handle); if (! Nt_success (status)) {Kdprint ("Wdfdrivercreate failed%!        status!\n ", status)); Cleanup tracing here because Drivercontextcleanup won't is called//as we have the failed to create WD        Fdriver object itself. Please note this if your return failure from DriverEntry after the//Wdfdriver object is created successfully, You don ' t has to//call WPP cleanup because in those cases drivercontextcleanup//would be executed when t        The He framework deletes the driverobject.    Wpp_cleanup (DriverObject); } return status; VoidevtdriveRcontextcleanup (in Wdfdriver Driver) {wpp_cleanup (Wdfdriverwdmgetdriverobject (Driver));}               Ntstatusevtdeviceadd (in Wdfdriver Driver, in Pwdfdevice_init deviceinit) {wdf_object_attributes    Attributes    NTSTATUS status;    Wdfdevice device;    Pdevice_context Pdevcontext;    Wdf_pnppower_event_callbacks pnppowercallbacks;    Wdf_io_queue_config Ioqueueconfig;    Unreferenced_parameter (Driver);    Wdf_pnppower_event_callbacks_init (&pnppowercallbacks);    Pnppowercallbacks.evtdevicepreparehardware = Evtdevicepreparehardware;    Wdfdeviceinitsetpnppowereventcallbacks (DeviceInit, &pnppowercallbacks);    Wdf_object_attributes_init_context_type (&attributes, Device_context);    Status = Wdfdevicecreate (&deviceinit, &attributes, &device); if (! Nt_success (status)) {Kdprint ("Wdfdevicecreate failed%! status!\n ", status));        return status;    } Pdevcontext = Getdevicecontext (device);                                Status = Wdfdevicecreatedeviceinterface (device, (Lpguid) &AMP;GUID_DEVINTERFACE_OSRUSBFX2, NULL);//Reference String if (! Nt_success (status)) {Kdprint ("Wdfdevicecreatedeviceinterface failed%!        status!\n ", status));    return status; } wdf_io_queue_config_init_default_queue (&ioqueueconfig, wdfioqueuedispatchparal    LEL);    Ioqueueconfig.evtiodevicecontrol = Evtiodevicecontrol;    Ioqueueconfig.evtioread = Evtioread;    Ioqueueconfig.evtiowrite = Evtiowrite; Status = Wdfioqueuecreate (device, &ioqueueconfig, Wdf_no_object_attrib    UTES, Wdf_no_handle); if (! Nt_success (status)) {Kdprint ("Wdfioqueuecreate failed%!        status!\n ", status));    return status; } return status; Ntstatusevtdevicepreparehardware (    In Wdfdevice Device, in Wdfcmreslist resourcelist, in Wdfcmreslist resourcelisttranslated) {NTSTATUS    Status    Pdevice_context Pdevicecontext;    Wdf_usb_device_select_config_params Configparams;    Wdfusbpipe pipe;    Unreferenced_parameter (resourcelist);    Unreferenced_parameter (resourcelisttranslated);        Pdevicecontext = Getdevicecontext (Device);    Create the USB device if it is not already created.                                    if (Pdevicecontext->usbdevice = = NULL) {status = Wdfusbtargetdevicecreate (Device,        Wdf_no_object_attributes, &pdevicecontext->usbdevice); if (! Nt_success (status)) {Kdprint ("Wdfusbtargetdevicecreate failed%!            status!\n ", status));        return status;    }} wdf_usb_device_select_config_params_init_single_interface (&configparams); Status = WDFUSBTArgetdeviceselectconfig (Pdevicecontext->usbdevice, Wdf_no_object_attributes,    &configparams); if (! Nt_success (status)) {Kdprint ("Wdfusbtargetdeviceselectconfig failed%!        status!\n ", status));    return status;    } pdevicecontext->usbinterface = ConfigParams.Types.SingleInterface.ConfiguredUsbInterface; Pipe = Wdfusbinterfacegetconfiguredpipe (Pdevicecontext->usbinterface, 2,//pipein Dex, NULL);//Pipeinfo pdevicecontext->bulkreadpipe = Wdfusbinterfacegetconfig                                                  Uredpipe (Pdevicecontext->usbinterface, Bulk_in_endpoint_index, NULL);//Pipeinfo Wdfusbtar    Getpipesetnomaximumpacketsizecheck (Pdevicecontext->bulkreadpipe); PdevicecontexT->bulkwritepipe = Wdfusbinterfacegetconfiguredpipe (PDEVICECONTEXT-&G T                                                  Usbinterface, Bulk_out_endpoint_index,    NULL);//Pipeinfo Wdfusbtargetpipesetnomaximumpacketsizecheck (pdevicecontext->bulkwritepipe); return status;} Voidevtiodevicecontrol (in Wdfqueue Queue, in Wdfrequest Request, in size_t outputbufferlength, in Size_    T inputbufferlength, in ULONG iocontrolcode) {Wdfdevice device;    Pdevice_context Pdevcontext;    size_t bytestransferred = 0;    NTSTATUS status;    Wdf_usb_control_setup_packet Controlsetuppacket;    Wdf_memory_descriptor Memdesc;    Wdfmemory memory;    Wdf_request_send_options sendoptions; Unreferenced_parameteR (inputbufferlength);    Unreferenced_parameter (outputbufferlength);    device = Wdfioqueuegetdevice (Queue);    Pdevcontext = Getdevicecontext (device);            Switch (iocontrolcode) {case ioctl_osrusbfx2_set_bar_graph_display:if (Inputbufferlength < sizeof (UCHAR)) {            status = Status_buffer_overflow;            bytestransferred = sizeof (UCHAR);        Break        } status = Wdfrequestretrieveinputmemory (Request, &memory); if (! Nt_success (status)) {Kdprint ("Wdfrequestretrievememory failed%!            Status! ", status));        Break } wdf_usb_control_setup_packet_init_vendor (&controlsetuppacket, bmreques Thosttodevice, Bmrequesttodevice, usbfx2lk_s                                        Et_bargraph_display,//Request 0,//Value 0); Index Wdf_memory_descriptOr_init_handle (&memdesc, Memory, NULL);       Send the I/O with a timeout to avoid hanging the calling//thread indefinitely. Wdf_request_send_options_init (&sendoptions, Wdf_request_send_option_timeout        ); Wdf_request_send_options_set_timeout (&sendoptions, Wdf_rel_timeout_in_ms (100))        ; Status = wdfusbtargetdevicesendcontroltransfersynchronously (Pdevcontext->usbdev Ice, NULL,//Optional wdfrequest &sendo                                        ptions,//Pwdf_request_send_options &controlsetuppacket,        &memdesc, (Pulong) &bytestransferred); if (! Nt_success (status)) {Kdprint ("Sendcontroltransfer failed%!         Status! ", status));   Break    } break;        Default:status = Status_invalid_device_request;    Break    } wdfrequestcompletewithinformation (Request, status, Bytestransferred); return;} Voidevtioread (in Wdfqueue Queue, in Wdfrequest Request, in size_t Length) {Wdfusb    Pipe pipe;    NTSTATUS status;    Wdfmemory reqmemory;    Pdevice_context Pdevicecontext;    BOOLEAN ret;    Unreferenced_parameter (Length);    Pdevicecontext = Getdevicecontext (Wdfioqueuegetdevice (Queue));    Pipe = pdevicecontext->bulkreadpipe;    Status = Wdfrequestretrieveoutputmemory (Request, &reqmemory); if (!    Nt_success (status)) {goto Exit;                                        } status = Wdfusbtargetpipeformatrequestforread (pipe, Request,                    Reqmemory, NULL//offsets                    ); if (!    Nt_success (status)) {goto Exit; } wdfrequestsetcompletionroutine (Request, Evtrequestreadcompleti    Onroutine, pipe); ret = Wdfrequestsend (Request, Wdfusbtargetpipegetiotarget (pipe), wdf_no_send_options    );        if (ret = = FALSE) {status = Wdfrequestgetstatus (Request);    Goto Exit;    } else {return;    }exit:wdfrequestcompletewithinformation (Request, status, 0); return;}    Voidevtrequestreadcompletionroutine (in Wdfrequest Request, in Wdfiotarget Target,     Pwdf_pequest_completion_params Completionparams, in Wdfcontext Context) {NTSTATUS status;    size_t bytesread = 0;    Pwdf_usb_request_completion_params Usbcompletionparams;    Unreferenced_parameter (Target);    Unreferenced_parameter (Context); Status = Completionparams->IoStatus.Status;    Usbcompletionparams = completionparams->parameters.usb.completion;    Bytesread = usbcompletionparams->parameters.piperead.length;    if (nt_success (status)) {Kdprint ("number of bytes read:%i64d\n", (INT64) bytesread)); } else {kdprint ("Read failed-request status%! Status! Usbdstatus%!    Status!\n ", status, Usbcompletionparams->usbdstatus));    } wdfrequestcompletewithinformation (Request, status, Bytesread); return;} Voidevtiowrite (in Wdfqueue Queue, in Wdfrequest Request, in size_t Length) {Ntsta    TUS status;    Wdfusbpipe pipe;    Wdfmemory reqmemory;    Pdevice_context Pdevicecontext;    BOOLEAN ret;    Unreferenced_parameter (Length);    Pdevicecontext = Getdevicecontext (Wdfioqueuegetdevice (Queue));    Pipe = pdevicecontext->bulkwritepipe; Status = Wdfrequestretrieveinputmemory (Request, &reqmemory); if (!    Nt_success (status)) {goto Exit;                                              } status = Wdfusbtargetpipeformatrequestforwrite (pipe, Request, Reqmemory, NULL); Offset if (!    Nt_success (status)) {goto Exit; } wdfrequestsetcompletionroutine (Request, Evtrequestwritecomplet    Ionroutine, pipe); ret = Wdfrequestsend (Request, Wdfusbtargetpipegetiotarget (pipe), wdf_no_send_options    );        if (ret = = FALSE) {status = Wdfrequestgetstatus (Request);    Goto Exit;    } else {return;    }exit:wdfrequestcompletewithinformation (Request, status, 0); return;}     Voidevtrequestwritecompletionroutine (in Wdfrequest Request, in Wdfiotarget Target, Pwdf_request_completion_params complEtionparams, in Wdfcontext Context) {NTSTATUS status;    size_t byteswritten = 0;    Pwdf_usb_request_completion_params Usbcompletionparams;    Unreferenced_parameter (Target);    Unreferenced_parameter (Context);    Status = completionparams->iostatus.status;    Usbcompletionparams = completionparams->parameters.usb.completion;    Byteswritten = usbcompletionparams->parameters.pipewrite.length;    if (nt_success (status)) {Kdprint ("number of bytes written:%i64d\n", (INT64) byteswritten)); } else {kdprint ("Write failed:request Status%! Status! Usbdstatus%!    Status!\n ", status, Usbcompletionparams->usbdstatus));    } wdfrequestcompletewithinformation (Request, status, Byteswritten); return;}


Windows Driver Development-8

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.