Windows Driver WDF---charsample

Source: Internet
Author: User

Driver section:

Ntstatusdriverentry (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. Parameters Description:driverobject-represents The instance of the function driver that's 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's 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; Initiialize Driver Config To control the attributes that//is global to the driver. Note that the framework by default//provides a driver unload routine. If you create any resources//in the DriverEntry and want to being cleaned in driver unload,//can override that By manually setting the Evtdriverunload in the//config structure.    In general Xxx_config_init macros is provided to//initialize most commonly used members.    Wdf_driver_config_init (&config, Charsample_evtdeviceadd);    Create a framework driver object to represent our driver. Status = Wdfdrivercreate (DriverObject, Registrypath, wdf_no_object_attributes,//Driver Attrib    Utes &config,//Driver config Info wdf_no_handle//hdriver); return status;}
Deviceadd can also be customized, hehe
Ntstatuscharsample_evtdeviceadd (in Wdfdriver Driver, in Pwdfdevice_init deviceinit) {ntstatusstatus;    Wdfdevicedevice; The first sentence of the wdf_io_queue_configioqueueconfig;//routine Paged_code, which indicates that the code of the routine occupies paged memory. This routine can only be called at the Passive_level interrupt level, otherwise it will be blue screen.    If not, then occupy the system's non-paged memory, to cherish the use. Paged_code ();//Create Device without object property and device object environment variable structure status = Wdfdevicecreate (&deviceinit, Wdf_no_object_attributes, &    Device); if (!    Nt_success (status)) {return status; }//initializes the default queue configuration and sets the I/O request distribution to be processed serially.    For this example, it is possible to select serial or parallel, but not the competitor. Wdf_io_queue_config_init_default_queue (&ioqueueconfig, wdfioqueuedispatchsequential);// Set the Evtiodevicecontrol routine to process the application's DeviceIoControl () function call Ioqueueconfig.evtiodevicecontrol = Charsample_    evtiodevicecontrol;//Create Queue status = Wdfioqueuecreate (device, &ioqueueconfig, Wdf_no_object_attributes, NULL); if (!    Nt_success (status)) {return status;    }//Create device GUID Interface status = Wdfdevicecreatedeviceinterface (device, (Lpguid) &charsample_devinterface_guid, NULL); if (! Nt_success (status) {} return status;}

Voidcharsample_evtiodevicecontrol (in Wdfqueue Queue, in Wdfrequest Request, in size_t outputbufferlength,    In size_t inputbufferlength, in ULONG iocontrolcode) {NTSTATUS status; PVOID buffer;    CHAR n,c[]= "0123456789";    Paged_code (); Switch (iocontrolcode) {case Charsample_ioctl_800:if (inputbufferlength = = 0 | | Outputbufferlength < 2) {//check input, output parameter validity wdfrequestcomplete (Request, status_invalid_parameter);} else{//input buffer address can be obtained by calling the Wdfrequestretrieveinputbuffer function//output buffer address can be obtained by calling the Wdfrequestretrieveoutputbuffer function/ Get input buffer address buffer//requires 1 bytes space status = Wdfrequestretrieveinputbuffer (Request, 1, &buffer, NULL);        Nt_success (status)) {Wdfrequestcomplete (Request, status_unsuccessful); break;} This buffer represents the input buffer address//input n= application to the driver's digital ASCII code n = * (CHAR *) buffer;if ((n>= ' 0 ') && (n<= ' 9 ')) {//If it is a number, the N- = ' 0 ';//n= number (0-9)//Get output buffer address Bufferstatus = Wdfrequestretrieveoutputbuffer (Request, 2, &buffer, NULL); Nt_success (status)) {WdfrequesTcomplete (Request, status_unsuccessful); Here buffer represents the output buffer address//output: From the Chinese array c[] out the corresponding number of Chinese code, copied to the output buffer strncpy ((PCHAR) buffer,&c[n*2],2);//completion of the I/O request, The data length of the driver passed to the application is 2 bytes (one Chinese) wdfrequestcompletewithinformation (Request, status_success, 2);}        else//Returns invalid parameter Wdfrequestcomplete (Request, status_invalid_parameter);}    Break Default:status = Status_invalid_device_request;        Wdfrequestcompletewithinformation (Request, status, 0);    Break } return;

Application section: For the custom DS compiled program differences or relatively large.

    DevicePath = Getdevicepath ((lpguid) &charsample_devinterface_guid); Hdevice = CreateFile (DevicePath, generic_read| Generic_write, File_share_read |                         File_share_write, NULL, open_existing, 0,    NULL);        if (Hdevice = = Invalid_handle_value) {printf ("ERROR Opening device: (%0x) returned from createfile\n", GetLastError ());    return 0; }printf ("ok.\n"); charbufinput[1];//Input to devicecharbufoutput[2];//Output from deviceulongnoutput;//Count written to bufoutputprintf ("Please enter number (0-9) \ n"); L0:bufinput[0] = _getch (); if (bufinput[0]< ' 0 ') | | (bufinput[0]> ' 9 '))   Goto L0;_putch (bufinput[0]); Call Device IO Control interface (charsample_ioctl_800) in Driverif (! DeviceIoControl (Hdevice, charsample_ioctl_800, Bufinput, 1, Bufoutput, 2, &noutput, NULL)) {printf ("Error:deviceio        Control returns%0X. ", GetLastError ()); Goto exit;} 

Pchargetdevicepath (in Lpguid interfaceguid) {hdevinfo hardwaredeviceinfo;    Sp_device_interface_data Deviceinterfacedata;    Psp_device_interface_detail_data deviceinterfacedetaildata = NULL;    ULONG Length, requiredlength = 0;    BOOL Bresult;                             Hardwaredeviceinfo = Setupdigetclassdevs (Interfaceguid, NULL, NULL, (digcf_present |    Digcf_deviceinterface));        if (Hardwaredeviceinfo = = Invalid_handle_value) {printf ("Setupdigetclassdevs failed!\n");    Exit (1);    } deviceinterfacedata.cbsize = sizeof (Sp_device_interface_data);                                              Bresult = setupdienumdeviceinterfaces (hardwaredeviceinfo, 0,                                              Interfaceguid, 0,    &deviceinterfacedata); if (bresult = = FALSE) {PrinTF ("Setupdienumdeviceinterfaces failed.\n");        Setupdidestroydeviceinfolist (Hardwaredeviceinfo);    Exit (1);        } setupdigetdeviceinterfacedetail (Hardwaredeviceinfo, &deviceinterfacedata, NULL, 0,    &requiredlength, NULL);    Deviceinterfacedetaildata = (psp_device_interface_detail_data) LocalAlloc (lmem_fixed, RequiredLength);        if (Deviceinterfacedetaildata = = NULL) {setupdidestroydeviceinfolist (hardwaredeviceinfo);        printf ("Failed to allocate memory.\n");    Exit (1);    } deviceinterfacedetaildata->cbsize = sizeof (Sp_device_interface_detail_data);    Length = Requiredlength;                   Bresult = Setupdigetdeviceinterfacedetail (Hardwaredeviceinfo, &deviceinterfacedata,                  Deviceinterfacedetaildata, Length, &requiredlength,    NULL); if (bresult = = FALSE) {printf ("Error in SetupdigetdeviCeinterfacedetail\n ");        Setupdidestroydeviceinfolist (Hardwaredeviceinfo);        LocalFree (Deviceinterfacedetaildata);    Exit (1); } return Deviceinterfacedetaildata->devicepath;}


Windows Driver WDF---charsample

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.