Steward
分享是一種喜悅、更是一種幸福
驅動程式 - Windows Driver Model (WDM) - 使用範例 - C/C++ (DDK) - Handle File IRP
參考資訊:
https://wasm.in/
http://four-f.narod.ru/
https://github.com/steward-fu/ddk
main.c
#include <wdm.h>
static PDEVICE_OBJECT pNextDevice = NULL;
static NTSTATUS AddDevice(PDRIVER_OBJECT pMyDriver, PDEVICE_OBJECT pPhyDevice)
{
PDEVICE_OBJECT pMyDevice = NULL;
UNICODE_STRING usDeviceName = { 0 };
UNICODE_STRING usSymbolName = { 0 };
RtlInitUnicodeString(&usDeviceName, L"\\Device\\MyDriver");
IoCreateDevice(pMyDriver, 0, &usDeviceName, FILE_DEVICE_UNKNOWN, 0, FALSE, &pMyDevice);
DbgPrint("%s, pMyDevice = %p\n", __FUNCTION__, pMyDevice);
RtlInitUnicodeString(&usSymbolName, L"\\DosDevices\\MyDriver");
IoCreateSymbolicLink(&usSymbolName, &usDeviceName);
pNextDevice = IoAttachDeviceToDeviceStack(pMyDevice, pPhyDevice);
DbgPrint("%s, pNextDevice = %p\n", __FUNCTION__, pNextDevice);
pMyDevice->Flags &= ~DO_DEVICE_INITIALIZING;
pMyDevice->Flags |= DO_BUFFERED_IO;
return STATUS_SUCCESS;
}
static VOID Unload(PDRIVER_OBJECT pMyDriver)
{
DbgPrint("%s, pMyDriver = %p\n", __FUNCTION__, pMyDriver);
}
static NTSTATUS IrpPnp(PDEVICE_OBJECT pMyDevice, PIRP pIrp)
{
UNICODE_STRING usSymbolName = { 0 };
PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
DbgPrint("%s, Major = 0x%x\n", __FUNCTION__, pStack->MajorFunction);
if (pStack->MinorFunction == IRP_MN_REMOVE_DEVICE) {
RtlInitUnicodeString(&usSymbolName, L"\\DosDevices\\MyDriver");
IoDeleteSymbolicLink(&usSymbolName);
DbgPrint("%s, Delete symbolic link\n", __FUNCTION__);
IoDetachDevice(pNextDevice);
DbgPrint("%s, Delete device\n", __FUNCTION__);
IoDeleteDevice(pMyDevice);
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
return STATUS_SUCCESS;
}
IoSkipCurrentIrpStackLocation(pIrp);
return IoCallDriver(pNextDevice, pIrp);
}
static NTSTATUS IrpFile(PDEVICE_OBJECT pMyDevice, PIRP pIrp)
{
PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
switch (pStack->MajorFunction) {
case IRP_MJ_CREATE:
DbgPrint("%s, Handle IRP_MJ_CREATE\n", __FUNCTION__);
break;
case IRP_MJ_READ:
DbgPrint("%s, Handle IRP_MJ_READ\n", __FUNCTION__);
break;
case IRP_MJ_WRITE:
DbgPrint("%s, Handle IRP_MJ_WRITE\n", __FUNCTION__);
break;
case IRP_MJ_CLOSE:
DbgPrint("%s, Handle IRP_MJ_CLOSE\n", __FUNCTION__);
break;
}
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
return STATUS_SUCCESS;
}
NTSTATUS DriverEntry(PDRIVER_OBJECT pMyDriver, PUNICODE_STRING pMyRegistry)
{
DbgPrint("%s, pMyDriver = %p\n", __FUNCTION__, pMyDriver);
pMyDriver->MajorFunction[IRP_MJ_PNP] = IrpPnp;
pMyDriver->MajorFunction[IRP_MJ_CREATE] = IrpFile;
pMyDriver->MajorFunction[IRP_MJ_READ] = IrpFile;
pMyDriver->MajorFunction[IRP_MJ_WRITE] = IrpFile;
pMyDriver->MajorFunction[IRP_MJ_CLOSE] = IrpFile;
pMyDriver->DriverExtension->AddDevice = AddDevice;
pMyDriver->DriverUnload = Unload;
return STATUS_SUCCESS;
}
app.c
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <strsafe.h>
#include <setupapi.h>
int main(int argc, char* argv[])
{
DWORD dwRet = 0;
char szBuffer[32] = { "I am error" };
HANDLE hFile = INVALID_HANDLE_VALUE;
hFile = CreateFile("\\\\.\\MyDriver", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
WriteFile(hFile, szBuffer, strlen(szBuffer), &dwRet, NULL);
printf("%s, Write %s bytes, return %d bytes\n", __FUNCTION__, szBuffer, dwRet);
memset(szBuffer, 0, sizeof(szBuffer));
ReadFile(hFile, szBuffer, sizeof(szBuffer), &dwRet, NULL);
printf("%s, Read %s bytes, return %d bytes\n", __FUNCTION__, szBuffer, dwRet);
CloseHandle(hFile);
return 0;
}
由於我們尚未實作驅動程式的檔案讀寫操作,因此,Write、Read的回傳長度都是0 bytes

Log