基本NT式驱动代码结构

Posted debugme

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基本NT式驱动代码结构相关的知识,希望对你有一定的参考价值。

#include <ntddk.h>

void DriverUnload(IN PDRIVER_OBJECT DriverObject);
NTSTATUS MyCreateClose(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
NTSTATUS MyDefaultHandler(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);

NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegPath)
{
UNICODE_STRING ustrDevName, ustrLinkName;
PDEVICE_OBJECT pDeviceObj = NULL;
NTSTATUS status;
unsigned int i;

KdPrint(("DriverEntry ...\n"));

RtlInitUnicodeString(&ustrDevName, L"\\Device\\demo0");
RtlInitUnicodeString(&ustrLinkName, L"\\DosDevices\\demo0");

for (i=0; i<IRP_MJ_MAXIMUM_FUNCTION; i++)
{
DriverObject->MajorFunction[i] = MyDefaultHandler;
}

DriverObject->MajorFunction[IRP_MJ_CREATE] = MyCreateClose;
DriverObject->MajorFunction[IRP_MJ_CLOSE] = MyCreateClose;

DriverObject->DriverUnload = DriverUnload;

status = IoCreateDevice(DriverObject,
0,
&ustrDevName,
FILE_DEVICE_UNKNOWN,
0,
FALSE,
&pDeviceObj);

if (!NT_SUCCESS(status))
{
KdPrint(("IoCreateDevice failed ...\n"));
return status;
}

if (!pDeviceObj)
{
KdPrint(("pDeviceObj == NULL\n"));
return STATUS_UNEXPECTED_IO_ERROR;
}

pDeviceObj->Flags |= DO_DIRECT_IO;

status = IoCreateSymbolicLink(&ustrLinkName, &ustrDevName);

pDeviceObj->Flags &= ~DO_DEVICE_INITIALIZING;

return STATUS_SUCCESS;
}

VOID DriverUnload(IN PDRIVER_OBJECT DriverObject)
{
UNICODE_STRING ustrLinkName;

KdPrint(("DriverUnload ...\n"));

RtlInitUnicodeString(&ustrLinkName, L"\\DosDevices\\demo0");

IoDeleteSymbolicLink(&ustrLinkName);
IoDeleteDevice(DriverObject->DeviceObject);
}

NTSTATUS MyCreateClose(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
Irp->iostatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return STATUS_SUCCESS;
}

NTSTATUS MyDefaultHandler(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
Irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return Irp->IoStatus.Status;
}

以上是关于基本NT式驱动代码结构的主要内容,如果未能解决你的问题,请参考以下文章

YJX_Driver_022_NT式驱动的安装

Android 平台电容式触摸屏的驱动基本原理

W驱开技详.过滤驱动测试

函数之基本结构

[转载]windows过滤驱动程序设计入门(驱动程序基本结构,设备栈,IRP栈和工作原理)

关于VS2013+WDK8.1写驱动文件配置问题