开发驱动基本框架源代码
开发驱动基本框架源代码,发一篇驱动的基本框架吧!// DriverDemo.cpp文件
extern "C"
{
#include <ntddk.h>
}
//自定义函数的声明
NTSTATUS DispatchCreateClose(PDEVICE_OBJECT pDevObj,PIRP pIrp);
void DriverUnload(PDRIVER_OBJECT pDriverObj);
//驱动内部名称和连接名称
#define DEVICE_NAME L"\\Device\\devDriverDemo "
#define LINK_NAME L“\\??\\sldevDriverDemo ”
驱动程序加载时调用DriverEntry例程
NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObj, PUNICODE_STRING pRegistryString)
{
NTSTATUS status=STATUS_SUCCESS;
DbgPrint("DriverDemo:DriverEntry...\n");
//初始化各派遣例程
pDriverObj->MajorFunction=DispatchCreateClose;
pDriverObj->MajorFunction=DispatchCreateClose;
pDriverObj->DriverUnload=DriverUnload;
//设备名称
UNICODE_STRING ustrDeviceName;
RtlInitUnicodeString(&ustrDeviceName,DEVICE_NAME);
//创建设备对象
PDEVICE_OBJECT pDevObj;
status = IoCreateDevice(pDriverObj,0,&ustrDeviceName,FILE_DEVICE_UNKNOWN,0,false,&pDevObj);
if(!NT_SUCCESS(status))
{
DbgPrint("IoCreateDevice创建设备失败! \n");
return status;
}
//符号连接名称
UNICODE_STRING ustrLinkName;
RtlInitUnicodeString(&ustrLinkName,LINK_NAME);
//创建关联
status=IoCreateSymbolicLink(&ustrLinkName,&ustrDeviceName);
if(!NT_SUCCESS(status))
{
IoDeleteDevice(pDevObj);
DbgPrint("IoCreateSymbolicLink创建关联失败! \n");
return status;
}
return STATUS_SUCCESS;
}
//自定义驱动卸载函数
void DriverUnload(PDRIVER_OBJECT pDriverObj)
{
DbgPrint("DriverDemo:DriverUnload...\n");
//删除符号连接名称
UNICODE_STRING strLink;
RtlInitUnicodeString(&strLink,LINK_NAME);
IoDeleteSymbolicLink(&strLink);
IoDeleteDevice(pDriverObj->DeviceObject);
}
//处理IRP_MJ_CREATE
NTSTATUS DispatchCreateClose(PDEVICE_OBJECT pDevObj,PIRP pIrp)
{
DbgPrint("DriverDemo:DriverUnload...\n");
pIrp->IoStatus.Status=STATUS_SUCCESS;
IoCompleteRequest(pIrp,IO_NO_INCREMENT);
return STATUS_SUCCESS;
}
页:
[1]