Second Lecture: My first drive

Source: Internet
Author: User

Original: http://blog.csdn.net/caperingrabbit/article/details/5285288

Once the development environment has been configured, it is necessary to understand the development of the driver through specific procedures. Below we use a WDM -driven framework to implement the HelloWorld of Windows drivers.

As a driver, the first thing to write is its entry function, which is the same as MFC 's Winmain or C + + main function, the driven entry function uses DriverEntry. The main functionality implemented in the Ingress function is the registration of some distribution routines and other transactions that need to be initialized.

the prototype of DriverEntry is Ntstatus driverentry (in Pdriver_object pdriverobject,in punicode_string pregistrypath), in ,out This is just a macro that represents the input and output type of the parameter, and it has no substantive meaning and does not occupy space. Pdriverobject is the driver object that we want to create, and Pregistrypath is the address of the driver in the registry. As for their type, Pdriver_object is an important data structure in the driver development, this is the driving object, the specific we will discuss later, and Punicode_string is a wide-character pointer, in the kernel development, the general use of strings are wide characters, that is  A Unicode string. Here's an example:

[CPP]View PlainCopy
  1. extern"C" //is generally used in C + + for Windows Driver development, in front of the entry function to add this keyword, mainly in order to be able to call C function in the driver
  2. NTSTATUS DriverEntry (in Pdriver_object pdriverobject,
  3. In Punicode_string Pregistrypath
  4. )
  5. {
  6. Kdprint (("hello!welcome to the Driver entry!/n")); Kdprint is a macro, similar to the Trace in MFC
  7. //For common routines, this is also used to register with a common handler function
  8. For (ULONG i = 0; I <= irp_mj_maximum_function; i++)
  9. {
  10. Pdriverobject->majorfunction[i] = Dispatchroutine;
  11. }
  12. //Driver function routine registration, which is a few more special routines that we register separately,
  13. //And the main task of driving development is to complete their implementation functions of these routines, for simple and convenient to explain, used here and above the same
  14. Pdriverobject->driverextension->adddevice = Mydeviceadd;
  15. Pdriverobject->driverunload = Driverunload;
  16. Pdriverobject->majorfunction[irp_mj_create] = Dispatchroutine;
  17. Pdriverobject->majorfunction[irp_mj_close] = Dispatchroutine;
  18. Pdriverobject->majorfunction[irp_mj_read] = Dispatchroutine;
  19. Pdriverobject->majorfunction[irp_mj_write] = Dispatchroutine;
  20. return status_success;
  21. }

After the entry function is completed, the next step is to implement the function functions of the distribution routines registered above. As a WDM driver, you first need to create the device object, this task is given to the AddDevice routine, through the above registration, the implementation part of the Mydeviceadd.

[CPP]View PlainCopy
  1. NTSTATUS Mydeviceadd (in Pdriver_object DriverObject,
  2. In Pdevice_object Physicaldeviceobject)
  3. {
  4. Paged_code (); a macro provided by//DDK
  5. Kdprint (("Enter mydrivers/n"));
  6. NTSTATUS status;
  7. Pdevice_object Fdo;
  8. Unicode_string Devname;
  9. Rtlinitunicodestring (&devname,l"//device//mydevice");
  10. //Create a Device object
  11. Status = IoCreateDevice (
  12. DriverObject,
  13. sizeof (device_extension),
  14. & (unicode_string) Devname,
  15. File_device_unknown,
  16. 0,
  17. FALSE,
  18. &FDO);
  19. if (! Nt_success (status))
  20. return status;
  21. //Get the data structure of the device extension. This data structure is generally defined by the user, saving some device-related information
  22. Pdevice_extension PDX = (pdevice_extension) fdo->deviceextension;
  23. //Configure the contents of the device extension
  24. Pdx->fdo = FDO;
  25. //Device Mount
  26. Pdx->nextstackdevice = IoAttachDeviceToDeviceStack (FDO, physicaldeviceobject);
  27. Unicode_string Symlinkname;
  28. Rtlinitunicodestring (&symlinkname,l"//dosdevices//mydriver");
  29. Pdx->ustrdevicename = Devname;
  30. Pdx->ustrsymlinkname = Symlinkname;
  31. Status = Iocreatesymboliclink (& (unicode_string) symlinkname,& (unicode_string) devname);
  32. if (! Nt_success (status))
  33. {
  34. Iodeletesymboliclink (&pdx->ustrsymlinkname);
  35. Status = Iocreatesymboliclink (&symlinkname,&devname);
  36. if (! Nt_success (status))
  37. {
  38. return status;
  39. }
  40. }
  41. //Set the IO status of the device
  42. Fdo->flags |= Do_buffered_io | do_power_pagable;
  43. Fdo->flags &= ~do_device_initializing;
  44. Kdprint (("leavemydrivers/n"));
  45. return status_success;
  46. }

After the device object is generated, the next step is to process the various requests to the device. Here is a generic processing example to demonstrate. In fact, this handler function simply transfers these requests to the real device, with no actual functionality.

[CPP]View PlainCopy
  1. NTSTATUS hellowdmdispatchroutine (in Pdevice_object FDO,
  2. In Pirp IRP)
  3. {
  4. Paged_code ();
  5. Kdprint (("Enter hellowdmdispatchroutine/n"));
  6. Irp->iostatus.status = status_success;
  7. irp->iostatus.information = 0; //   
  8. IoCompleteRequest (IRP, io_no_increment);
  9. Kdprint (("Leave hellowdmdispatchroutine/n"));
  10. return status_success;
  11. }
  12. After all the requests are executed, the driver is uninstalled at the end. For WDM drivers, the standard programs actually unload routines with no real content, usually with PnP inside the Removedevice to deal with, here is just a demo, demonstrate the function and content of this routine.
  13. VOID driverunload (in Pdriver_object pdriverobject)
  14. {
  15. Pdevice_object Pnextobj;
  16. Kdprint (("Enter driverunload/n"));
  17. Pnextobj = pdriverobject->deviceobject;
  18. While (pnextobj! = NULL)
  19. {
  20. Pdevice_extension Pdevext = (pdevice_extension)
  21. pnextobj->deviceextension;
  22. //Remove Symbolic Links
  23. Unicode_string Plinkname = pdevext->ustrsymlinkname;
  24. Iodeletesymboliclink (&plinkname);
  25. Pnextobj = pnextobj->nextdevice;
  26. Iodeletedevice (Pdevext->pdevice);
  27. }
  28. }

In this way, the main part of a drive is completed. Then complete some fine work, compile, a driver appears.

Header file

[CPP]View PlainCopy
  1. #ifdef __cplusplus
  2. extern "C"
  3. {
  4. #endif
  5. #include <wdm.h>
  6. #ifdef __cplusplus
  7. }
  8. #endif
  9. typedef struct _device_extension
  10. {
  11. Pdevice_object Fdo;
  12. Pdevice_object Nextstackdevice;
  13. Unicode_string Ustrdevicename; //Device name
  14. Unicode_string Ustrsymlinkname; //Symbolic Link name
  15. } device_extension, *pdevice_extension;
  16. #define Pagedcode code_seg ("page")
  17. #define Lockedcode code_seg ()
  18. #define Initcode code_seg ("INIT")
  19. #define PAGEDDATA data_seg ("page")
  20. #define LOCKEDDATA data_seg ()
  21. #define InitData data_seg ("INIT")
  22. #define ARRAYSIZE (P) (sizeof (P)/sizeof ((p) [0])
  23. NTSTATUS Mydeviceadd (in Pdriver_object DriverObject,
  24. in Pdevice_object Physicaldeviceobject);
  25. NTSTATUS dispatchroutine (in Pdevice_object FDO,
  26. in Pirp IRP);
  27. void Driverunload (in Pdriver_object driverobject);
  28. extern "C"
  29. NTSTATUS DriverEntry (in Pdriver_object DriverObject,
  30. In punicode_string Registrypath);

Create a new text file with the following contents:

[CPP]View PlainCopy
    1. #
    2. # do not EDIT the This FILE!!! Edit./sources. If you want to add a new source
    3. # file to this component. This file merely indirects to the real make file
    4. # That's shared by all the driver components of the Windows NT DDK
    5. #
    6. ! INCLUDE $ (ntmakeenv)/makefile.def

Rename file, change to Makefile, suffix not. Then re-create a text file, write the following content:

[CPP]View PlainCopy
    1. Targetname=helloworld
    2. Targettype=driver
    3. Drivertype=wdm
    4. Targetpath=obj
    5. includes=$ (BASEDIR)/inc;/
    6. $ (BASEDIR)/inc/ddk;/
    7. sources=mydrivers.cpp/

Save, change the file name to Sources, the suffix does not, so enter the DDK compilation environment, compiled, a driver file with a suffix of. sys appears.

Well, a simple drive was born.

http://blog.csdn.net/chence19871/article/details/50697439

Second Lecture: My first drive

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.