驱动问题代码01
Posted a-s-m
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了驱动问题代码01相关的知识,希望对你有一定的参考价值。
蓝屏,待调试
#include <ntddk.h> typedef struct _DEVICE_EXTENSION { PDEVICE_OBJECT pDevObj; UNICODE_STRING ustrDeviceName; UNICODE_STRING ustrSymbolLinkName; }DEVICE_EXTENSION,*PDEVICE_EXTENSION; NTSTATUS DeviceCreate(PDEVICE_OBJECT pDeviceObject, PIRP pIrp); NTSTATUS DeviceClose(PDEVICE_OBJECT pDeviceObject, PIRP pIrp); NTSTATUS DeviceRead(PDEVICE_OBJECT pDeviceObject, PIRP pIrp); NTSTATUS DemoCreateDevice(PDRIVER_OBJECT pDriver,PCWSTR devName,PCWSTR devSymName); NTSTATUS unload(PDRIVER_OBJECT driver) { PDEVICE_OBJECT pDev; DbgPrint("driver :%ws unload", driver->DriverName.Buffer); pDev = driver->DeviceObject; while (pDev != NULL) { PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)pDev->DeviceExtension; //删除符号链接 UNICODE_STRING pLinkName = pDevExt->ustrSymbolLinkName; IoDeleteSymbolicLink(&pLinkName); pDev = pDev->NextDevice; IoDeleteDevice(pDevExt->pDevObj); } DbgPrint("driver unload success..."); return STATUS_SUCCESS; } NTSTATUS DriverEntry(PDRIVER_OBJECT driver, PUNICODE_STRING reg_path) { NTSTATUS ntstatus = STATUS_SUCCESS; driver->DriverUnload = unload; driver->MajorFunction[IRP_MJ_CREATE] = DeviceCreate;//创建 driver->MajorFunction[IRP_MJ_READ] = DeviceRead;//通信 driver->MajorFunction[IRP_MJ_CLOSE] = DeviceClose;//关闭 ntstatus = DemoCreateDevice(driver, L"\Device\MyDevice01", L"\??\MyDeviceSymbolLinkName01"); if (!NT_SUCCESS(ntstatus)) { DbgPrint("IoCreateDevice Failed"); return ntstatus; } ntstatus = DemoCreateDevice(driver, L"\Device\MyDevice02", L"\??\MyDeviceSymbolLinkName02"); if (!NT_SUCCESS(ntstatus)) { DbgPrint("IoCreateDevice Failed"); return ntstatus; } DbgPrint("%ws", reg_path->Buffer); DbgPrint("driver load success..."); return STATUS_SUCCESS; } NTSTATUS DeviceCreate(PDEVICE_OBJECT pDeviceObject, PIRP pIrp) { //业务代码区 //设置返回状态 pIrp->iostatus.Status = STATUS_SUCCESS; pIrp->IoStatus.Information = 0; IoCompleteRequest(pIrp, IO_NO_INCREMENT); DbgPrint("create device success...%d", pDeviceObject->ActiveThreadCount); return STATUS_SUCCESS; } NTSTATUS DeviceClose(PDEVICE_OBJECT pDeviceObject, PIRP pIrp) { //业务代码区 //设置返回状态 pIrp->IoStatus.Status = STATUS_SUCCESS;//getLastError()得到的值 pIrp->IoStatus.Information = 0; //返回给3环多少数据,没有填0 IoCompleteRequest(pIrp, IO_NO_INCREMENT); DbgPrint("create device success...%d", pDeviceObject->ActiveThreadCount); return STATUS_SUCCESS; } NTSTATUS DeviceRead(PDEVICE_OBJECT pDeviceObject, PIRP pIrp) { PIO_STACK_LOCATION iostack; iostack = IoGetCurrentIrpStackLocation(pIrp); ULONG length = iostack->Parameters.Read.Length; PVOID pBuffer = pIrp->AssociatedIrp.SystemBuffer; pIrp->IoStatus.Status = STATUS_SUCCESS;//getLastError()得到的值 pIrp->IoStatus.Information = length; //返回给3环多少数据,没有填0 RtlFillMemory(pBuffer, length, 0xAA); IoCompleteRequest(pIrp, IO_NO_INCREMENT); DbgPrint("read device success...%d", pDeviceObject->ActiveThreadCount); return STATUS_SUCCESS; } NTSTATUS DemoCreateDevice(PDRIVER_OBJECT pDriver,PCWSTR devName,PCWSTR devSymName) { PDEVICE_OBJECT pDevice; PDEVICE_EXTENSION pDevExt; UNICODE_STRING DeviceName; UNICODE_STRING SymbolLinkName; RtlInitUnicodeString(&DeviceName, devName); RtlInitUnicodeString(&SymbolLinkName, devSymName); NTSTATUS ntstatus = STATUS_SUCCESS; ntstatus = IoCreateDevice(pDriver, 0, &DeviceName, FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN, FALSE, &pDevice); if (!NT_SUCCESS(ntstatus)) { DbgPrint("IoCreateDevice Failed"); return ntstatus; } ntstatus = IoCreateSymbolicLink(&SymbolLinkName, &DeviceName); if (!NT_SUCCESS(ntstatus)) { DbgPrint("IoCreateSymbolicLink Failed"); IoDeleteDevice(pDevice); return ntstatus; } pDevice->Flags |= DO_BUFFERED_IO; pDevExt = (PDEVICE_EXTENSION)pDevice->DeviceExtension; pDevExt->pDevObj = pDevice; pDevExt->ustrDeviceName = DeviceName; pDevExt->ustrSymbolLinkName = SymbolLinkName; return ntstatus; }
以上是关于驱动问题代码01的主要内容,如果未能解决你的问题,请参考以下文章