WDM driver entry (1) - Hellowdm

zhaozj2021-02-11  188

The WDM driver is a very new thing. I believe that many people are interested in it, but they can't find the entry point of learning. The reason, or because WDM is a very "dead board" program, it is running at the bottom RING 0 of the system, providing various interfaces to application calls. Because of this, it is not like a normal application, you can quickly get your hand - more, you are reading its technical information and various interface information, you must be familiar with the underlying work The principle, otherwise an accidentally, "blue screen", huh, huh, say it back, when writing drivers, the crash is home to the house. Therefore, many people are looking for WDM. Recalling, I just started learning WDM, I'm looking at - reading a whole 3 days, but after reading it, I didn't see how much I didn't look, I still don't know how to get started, even how to write a "Hello World "I don't know - later, I know that in fact, WDM is not the so-called" Hello World "program, oh, it is really painful, this is mainly due to too little WDM information on the network. In order not to let everyone repeat my mistakes and have a sense of sensibility to WDM, I give a simple and complete WDM framework and come with annotation, just a "Hello World" that is entry. Less nonsense, let us start studying immediately, requiring readers to install DDK 2000. (I have not tested it in Win98, I don't know if it can run normally) / ******************************************* ******************************* program name: Hello World for WDM file name: HelloWDM.cpp author: Luo Cong date: 2002-8-16 ********************************************************************* ***************** / / / must be the header file, declare the function module and variables: #include "hellowdm.h" / ******** *********************************************************** ***** Function Name: DriveREntry () function description: WDM program entrance ******************************************* ***************************** /// extern "C" is required, indicating "CL link". This sentence can be omitted if your file name is Hellowdm.c. extern "C" NTSTATUS DriverEntry (IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath) {// Specify "Add Device" message by the function "HelloWDMAddDevice ()" to process: DriverObject-> DriverExtension-> AddDevice = HelloWDMAddDevice; // Specify "Plug That is, use the "message by the function" hellowdmpnp () ": driverObject-> majorfunction [IRP_MJ_PNP] = hellowDMPNP; // Returns a ntstatus value status_success. Almost all driver routines must return a NTSTATUS value that has a detailed definition in the NTSTATUS.H DDK header file.

Return status_success;} / ************************************************************************************************************************************************************************************************************ ***************** Function Name: HellowDmadDDevice () function description: Handling "Add Device" message **************** ******************************************************* / NTSTATUS HELLOWDMADDDEVICE iN PDRIVER_OBJECT DriverObject, iN PDEVICE_OBJECT PhysicalDeviceObject) {// definition of a return value of type NTSTATUS: NTSTATUS status; // definition of a functional device object (functional device Object): PDEVICE_OBJECT fdo; // create our functional device object, and save it to In FDO: status = ocreateDevice (DRIVEROBJECT, / / ​​Driver SIZEOF (Device_extension), // Requirements The size null, // device name, here, here, the type of null file_device_unknown, // device, in the standard header file WDM One of the File_Device_xxx values ​​listed in NTDDK.H, // Various constants with or in portions, indicating delete media, read-only, etc. false, // If only one thread can access the device at a time, For True, otherwise, for false & fdo); // The returned device object // nt_success macro is used to test whether the IOCREATEVICE kernel is successful. Don't forget to check if all calls to the kernel are successful. NT_ERROR macro does not equip with! NT_Success, it is best to use! NT_Success, because in addition to errors, it also intercepts warning information. If (! nt_success (status)) return status; // Create a device extension object DX for storing pointers pointing to FDO: pdevice_extension dx = (pdevice_extension) fdo-> deviceExtension; dx-> fdo = fdo; // with oattachdevicetodeventack The function is hung by the HellowDM device to the device stack: DX-> NextstackDevice = oattachdeventodeodeventack (fdo, physicalDeviceObject); // Set FDO's Flags. There are two "bit" that must be changed, one is to clear the Do_Device_initializing flag, if IOCREATEDEVICE () is called in the DriveREntry routine, it is not necessary to clear this flag.

Another one must set the do_buffer_io flag: fdo-> flags | = do_buffered_io | do_power_pagable; fdo-> flags & = ~ do_device_initializing; // Return Value: Return Status_suCcess;} / ********** *********************************************************** ** Function Name: HellowdmpnP () Function Description: Processing "Plug and Play" News ****************************************** ********************************* / NTSTATUS HELLOWDMPNP (in PDevice_Object FDO, In PIRP IRP) {// Create a device extension Object dx, to store a pointer to the fdo: PDEVICE_EXTENSION dx = (PDEVICE_EXTENSION) fdo-> DeviceExtension; // first () function to obtain the current through the IRP IoGetCurrentIrpStackLocation, and thereby obtain Minor function: PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation (Irp ); Ulong minorfunction = irpstack-> minorfunction; // then pass this minor function to the next device stack: ioskipCurrentirPstackLocation (IRP); ntstatus status = IocallDriver (DX-> NextstackDevice, IRP); // Processing Plug and Play "Sub-function code: // When the minor function is equal to IRP_MN_REMOVE_DEVICE, it means that the device is unplugged or removed, then the resource allocation is to be distributed and delete the device: if (minorfunction == IRP_MN_REMOVE_DEVICE) {// Cancel device interface: iOSetDeviceInt erfaceState (& dx-> ifSymLinkName, FALSE); RtlFreeUnicodeString (& dx-> ifSymLinkName); // call IoDetachDevice () is disengaged from the device stack fdo: if (dx-> NextStackDevice) IoDetachDevice (dx-> NextStackDevice); // Delete fdo: odeletedevice (fdo);} // return value: return status;} / ****************************************** ******************************** program name: Hello World for WDM file name: HelloWDM.h author: Luo Cong date : 2002-8-16 ************************************************************ ****************** / ///-head file, just declare some functions and variables, more simple, don't say more,

Reader own research: #ifdef __cplusplusextern "C" {# endif # include "ntddk.h" #ifdef __cplusplus} #endiftypedef struct _DEVICE_EXTENSION {PDEVICE_OBJECT fdo; PDEVICE_OBJECT NextStackDevice; UNICODE_STRING ifSymLinkName;} DEVICE_EXTENSION, * PDEVICE_EXTENSION; NTSTATUS HelloWDMAddDevice (IN PDRIVER_OBJECT DriverObject, in pdevice_object physicalDeviceObject; ntstatus hellowdmpnp (in pDevice_object fdo, in pirp IRP); Ok, the first WDM version of "Hello World" is here, although it is actually nothing, but because it contains A complete framework, so there is still a reference value for beginners. As for how to compile and install, leave a next time, please pay attention. (I don't want to sell Guan Chi, these steps are really troublesome, I have to write an article to be clear!) (Source: http://laoluoc.yeah.net)

转载请注明原文地址:https://www.9cbs.com/read-4887.html

New Post(0)