Windows NT Driver >> C/C++ >> IOCTL
METHOD_BUFFERED
參考資訊:
1. Source Code
METHOD_BUFFERED的處理手法就是I/O Manager會在Kernel配置一塊跟User Buffer一樣大小的記憶體,Driver只能對該配置的記憶體做讀寫的操作,而讀寫的操作都會同步回User Buffer,也因為結果都需要整塊複製回User Buffer,因此,METHOD_BUFFERED的方式會消耗比較大的資源,速度也比較慢。
參考如下微軟的圖表說明(IRP_MJ_READ):
記憶體指標:
Buffer | Length | |
---|---|---|
Input | (Irp) AssociatedIrp.SystemBuffer |
(IrpStack) Parameters.DeviceIoControl.InputBufferLength |
Output | (Irp) AssociatedIrp.SystemBuffer |
(IrpStack) Parameters.DeviceIoControl.OutputBufferLength |
main.c
#include <wdm.h> #define IOCTL_SET CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS) #define IOCTL_GET CTL_CODE(FILE_DEVICE_UNKNOWN, 0x801, METHOD_BUFFERED, FILE_ANY_ACCESS) #define DEV_NAME L"\\Device\\MyDriver" #define SYM_NAME L"\\DosDevices\\MyDriver" char szBuffer[255]={0}; void Unload(PDRIVER_OBJECT pOurDriver) { UNICODE_STRING usSymboName; RtlInitUnicodeString(&usSymboName, L"\\DosDevices\\MyDriver"); IoDeleteSymbolicLink(&usSymboName); IoDeleteDevice(pOurDriver->DeviceObject); } NTSTATUS IrpIOCTL(PDEVICE_OBJECT pOurDevice, PIRP pIrp) { ULONG Len=0; PIO_STACK_LOCATION psk = IoGetCurrentIrpStackLocation(pIrp); switch(psk->Parameters.DeviceIoControl.IoControlCode){ case IOCTL_SET: DbgPrint("IOCTL_SET"); Len = psk->Parameters.DeviceIoControl.InputBufferLength; memcpy(szBuffer, pIrp->AssociatedIrp.SystemBuffer, Len); DbgPrint("Buffer: %s, Length: %d", szBuffer, Len); break; case IOCTL_GET: DbgPrint("IOCTL_GET"); Len = strlen(szBuffer)+1; memcpy(pIrp->AssociatedIrp.SystemBuffer, szBuffer, Len); break; } pIrp->IoStatus.Status = STATUS_SUCCESS; pIrp->IoStatus.Information = Len; IoCompleteRequest(pIrp, IO_NO_INCREMENT); return STATUS_SUCCESS; } NTSTATUS IrpFile(PDEVICE_OBJECT pOurDevice, PIRP pIrp) { PIO_STACK_LOCATION psk = IoGetCurrentIrpStackLocation(pIrp); switch(psk->MajorFunction){ case IRP_MJ_CREATE: DbgPrint("IRP_MJ_CREATE"); break; case IRP_MJ_CLOSE: DbgPrint("IRP_MJ_CLOSE"); break; } IoCompleteRequest(pIrp, IO_NO_INCREMENT); return STATUS_SUCCESS; } NTSTATUS DriverEntry(PDRIVER_OBJECT pOurDriver, PUNICODE_STRING pOurRegistry) { PDEVICE_OBJECT pOurDevice=NULL; UNICODE_STRING usDeviceName; UNICODE_STRING usSymboName; pOurDriver->MajorFunction[IRP_MJ_CREATE] = pOurDriver->MajorFunction[IRP_MJ_CLOSE] = IrpFile; pOurDriver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = IrpIOCTL; pOurDriver->DriverUnload = Unload; RtlInitUnicodeString(&usDeviceName, L"\\Device\\MyDriver"); IoCreateDevice(pOurDriver, 0, &usDeviceName, FILE_DEVICE_UNKNOWN, 0, FALSE, &pOurDevice); RtlInitUnicodeString(&usSymboName, L"\\DosDevices\\MyDriver"); IoCreateSymbolicLink(&usSymboName, &usDeviceName); pOurDevice->Flags&= ~DO_DEVICE_INITIALIZING; pOurDevice->Flags|= DO_BUFFERED_IO; return STATUS_SUCCESS; }
IrpIOCTL()收到IOCTL_SET時,Driver複製User Buffer的內容到szBuffer,而收到IOCTL_GET時,將szBuffer內容又複製回User Buffer,完成暫存的功能,IoStatus.Information的數值就是OutBufferSize回傳的長度。
app.c
#define INITGUID #include <windows.h> #include <winioctl.h> #include <strsafe.h> #include <setupapi.h> #include <stdio.h> #include <stdlib.h> #define IOCTL_SET CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS) #define IOCTL_GET CTL_CODE(FILE_DEVICE_UNKNOWN, 0x801, METHOD_BUFFERED, FILE_ANY_ACCESS) int __cdecl main(int argc, char* argv[]) { DWORD dwRet = 0; HANDLE hFile = NULL; char szBuffer[255]={"I am error"}; hFile = CreateFile("\\\\.\\MyDriver", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (hFile == INVALID_HANDLE_VALUE) { printf("failed to open mydriver"); return 1; } printf("SET: %s, %d\n", szBuffer, strlen(szBuffer)+1); DeviceIoControl(hFile, IOCTL_SET, szBuffer, strlen(szBuffer)+1, NULL, 0, &dwRet, NULL); memset(szBuffer, 0, sizeof(szBuffer)); DeviceIoControl(hFile, IOCTL_GET, NULL, 0, szBuffer, sizeof(szBuffer), &dwRet, NULL); printf("GET: %s, %d\n", szBuffer, dwRet); CloseHandle(hFile); return 0; }
結果