- Buffer mode read and write operations
Read and write operations are generally caused by readfile and WriteFile functions, where the WriteFile function is the first example to be introduced. WriteFile requires the user to provide a buffer and describes the size of the buffer, and then writefile the memory data into the driver. In this way, the operating system copies the application-supplied buffer data directly into the kernel-mode address . This makes it easier to solve the problem of passing the user address into the driver, but the disadvantage is that it is necessary to replicate the data between user mode and kernel mode, which affects the efficiency. This method can be used in a small amount of memory operations. Copy the AssociatedIrp.SystemBuffer subdomain record of the IRP created by WriteFile to the address in kernel mode.
The following code shows how to read a device using a buffer, in which case the driver is responsible for filling in the data into the buffer:
The application layer calls ReadFile and wants to drive a read IRP request:
1 intMain () {2HANDLE Hdevice =3CreateFile ("\\\\.\\HELLODDK",4Generic_read |Generic_write,5 0, NULL,6 open_existing,7 File_attribute_normal,8 NULL);9 if(Hdevice = =Invalid_handle_value) {Tenprintf"Open Device failed!\n"); One } A Else{ -printf"Open Device succeed!\n"); - } theUCHAR buffer[Ten]; - ULONG Ulread; -BOOL BRet = ReadFile (hdevice, buffer,Ten, &Ulread, NULL); - if(bRet) { +printf"Read%d bytes!", ulread); - for(inti =0; I < (int) Ulread; i++){ +printf"%02x", Buffer[i]); A } atprintf"\ n"); - } - CloseHandle (hdevice); -System"Pause"); - return 0; -}
The results after the run are as follows:
Read and write operation of Windows Driver Development technology detailed