參考資訊:
https://wasm.in/
http://four-f.narod.ru/
https://github.com/steward-fu/ddk
main.c
#include <wdm.h> #define DEV_NAME L"\\Device\\MyDriver" #define SYM_NAME L"\\DosDevices\\MyDriver" char szBuffer[255] = {0}; ULONG dwBufferLength = 0; PDEVICE_OBJECT pNextDevice = NULL; VOID StartIo(struct _DEVICE_OBJECT *pMyDriver, struct _IRP *pIrp) { ULONG dwLen = 0; PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp); switch (pStack->MajorFunction) { case IRP_MJ_READ: dwLen = strlen(szBuffer); strcpy(pIrp->AssociatedIrp.SystemBuffer, szBuffer); DbgPrint("StartIo, IRP_MJ_READ"); break; case IRP_MJ_WRITE: dwLen = pStack->Parameters.Write.Length; strcpy(szBuffer, pIrp->AssociatedIrp.SystemBuffer); DbgPrint("StartIo, IRP_MJ_WRITE"); DbgPrint("Buffer: %s, Length: %d", szBuffer, dwLen); break; } pIrp->IoStatus.Status = STATUS_SUCCESS; pIrp->IoStatus.Information = dwLen; IoCompleteRequest(pIrp, IO_NO_INCREMENT); IoStartNextPacket(pMyDriver, FALSE); } NTSTATUS AddDevice(PDRIVER_OBJECT pMyDriver, PDEVICE_OBJECT pPhyDevice) { PDEVICE_OBJECT pMyDevice = NULL; UNICODE_STRING usDeviceName = { 0 }; UNICODE_STRING usSymbolName = { 0 }; RtlInitUnicodeString(&usDeviceName, DEV_NAME); IoCreateDevice(pMyDriver, 0, &usDeviceName, FILE_DEVICE_UNKNOWN, 0, FALSE, &pMyDevice); RtlInitUnicodeString(&usSymbolName, SYM_NAME); IoCreateSymbolicLink(&usSymbolName, &usDeviceName); pNextDevice = IoAttachDeviceToDeviceStack(pMyDevice, pPhyDevice); pMyDevice->Flags &= ~DO_DEVICE_INITIALIZING; pMyDevice->Flags |= DO_BUFFERED_IO; return STATUS_SUCCESS; } void Unload(PDRIVER_OBJECT pMyDriver) { pMyDriver = pMyDriver; } NTSTATUS IrpPnp(PDEVICE_OBJECT pMyDriver, PIRP pIrp) { UNICODE_STRING usSymbolName = { 0 }; PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp); if (pStack->MinorFunction == IRP_MN_REMOVE_DEVICE) { RtlInitUnicodeString(&usSymbolName, SYM_NAME); IoDeleteSymbolicLink(&usSymbolName); IoDetachDevice(pNextDevice); IoDeleteDevice(pMyDriver); IoCompleteRequest(pIrp, IO_NO_INCREMENT); return STATUS_SUCCESS; } IoSkipCurrentIrpStackLocation(pIrp); return IoCallDriver(pNextDevice, pIrp); } NTSTATUS IrpFile(PDEVICE_OBJECT pMyDriver, PIRP pIrp) { PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp); switch (pStack->MajorFunction) { case IRP_MJ_CREATE: DbgPrint("IRP_MJ_CREATE"); break; case IRP_MJ_WRITE: DbgPrint("IrpFile, IRP_MJ_WRITE"); IoMarkIrpPending(pIrp); IoStartPacket(pMyDriver, pIrp, 0, NULL); return STATUS_PENDING; case IRP_MJ_READ: DbgPrint("IrpFile, IRP_MJ_READ"); IoMarkIrpPending(pIrp); IoStartPacket(pMyDriver, pIrp, 0, NULL); return STATUS_PENDING; case IRP_MJ_CLOSE: DbgPrint("IRP_MJ_CLOSE"); break; } IoCompleteRequest(pIrp, IO_NO_INCREMENT); return STATUS_SUCCESS; } NTSTATUS DriverEntry(PDRIVER_OBJECT pMyDriver, PUNICODE_STRING pMyRegistry) { pMyDriver->MajorFunction[IRP_MJ_PNP] = IrpPnp; pMyDriver->MajorFunction[IRP_MJ_CREATE] = IrpFile; pMyDriver->MajorFunction[IRP_MJ_WRITE] = IrpFile; pMyDriver->MajorFunction[IRP_MJ_READ] = IrpFile; pMyDriver->MajorFunction[IRP_MJ_CLOSE] = IrpFile; pMyDriver->DriverExtension->AddDevice = AddDevice; pMyDriver->DriverStartIo = StartIo; pMyDriver->DriverUnload = Unload; return STATUS_SUCCESS; }
app.c
#include <windows.h> #include <winioctl.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { DWORD dwLen = 0; 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); dwLen = strlen(szBuffer); printf("WR: %s\n", szBuffer); printf("Length: %d\n", dwLen); WriteFile(hFile, szBuffer, dwLen, &dwRet, NULL); memset(szBuffer, 0, sizeof(szBuffer)); ReadFile(hFile, szBuffer, sizeof(szBuffer), &dwRet, NULL); printf("RD: %s\n", szBuffer); printf("Length: %d\n", dwRet); CloseHandle(hFile); return 0; }
完成