赞
踩
NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject,PUNICODE_STRING pRegistryPath)
#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
#define NT_INFORMATION(Status) ((((ULONG)(Status)) >> 30) == 1) #define NT_WARNING(Status) ((((ULONG)(Status)) >> 30) == 2) #define NT_ERROR(Status) ((((ULONG)(Status)) >> 30) == 3)
typedef struct _UNICODE_STRING { USHORT Length; USHORT MaximumLength; PWSTR Buffer; } UNICODE_STRING, *PUNICODE_STRING;
typedef struct _DRIVER_OBJECT { CSHORT Type; CSHORT Size; PDEVICE_OBJECT DeviceObject; ULONG Flags; PVOID DriverStart; ULONG DriverSize; PVOID DriverSection; PDRIVER_EXTENSION DriverExtension; UNICODE_STRING DriverName; PUNICODE_STRING HardwareDatabase; PFAST_IO_DISPATCH FastIoDispatch; PDRIVER_INITIALIZE DriverInit; PDRIVER_STARTIO DriverStartIo; PDRIVER_UNLOAD DriverUnload; PDRIVER_DISPATCH MajorFunction[IRP_MJ_MAXIMUM_FUNCTION + 1]; } DRIVER_OBJECT;
typedef struct _DEVICE_OBJECT { CSHORT Type; USHORT Size; LONG ReferenceCount; struct _DRIVER_OBJECT *DriverObject; struct _DEVICE_OBJECT *NextDevice; struct _DEVICE_OBJECT *AttachedDevice; struct _IRP *CurrentIrp; PIO_TIMER Timer; ULONG Flags; ULONG Characteristics; __volatile PVPB Vpb; PVOID DeviceExtension; DEVICE_TYPE DeviceType; CCHAR StackSize; union { LIST_ENTRY ListEntry; WAIT_CONTEXT_BLOCK Wcb; } Queue; ULONG AlignmentRequirement; KDEVICE_QUEUE DeviceQueue; KDPC Dpc; ULONG ActiveThreadCount; PSECURITY_DESCRIPTOR SecurityDescriptor; KEVENT DeviceLock; USHORT SectorSize; USHORT Spare1; struct _DEVOBJ_EXTENSION * DeviceObjectExtension; PVOID Reserved; } DEVICE_OBJECT, *PDEVICE_OBJECT;
#ifdef __cplusplus extern "C" { #endif #include <NTDDK.h> #ifdef __cplusplus }; #endif #define PAGECODE code_seg("PAGE") #define LOCKEDCODE code_seg() #define INITCODE code_seg("INIT") #define PAGEDATA data_seg("PAGE") #define LOCKEDDATA data_seg() #define INITDATA data_seg("INIT") typedef struct _DEVICE_EXTENSION { PDEVICE_OBJECT pDevice; UNICODE_STRING ustrDeviceName; UNICODE_STRING ustrSymLinkName; } DEVICE_EXTENSION , *PDEVICE_EXTENSION; //函数声明 NTSTATUS DispatchRoutine(__in struct _DEVICE_OBJECT *DeviceObject, __in struct _IRP *Irp); VOID UnloadRoutine(__in struct _DRIVER_OBJECT *DriverObject); /// #pragma INITCODE extern "C" NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING pRegistryPath) { NTSTATUS status; PDEVICE_EXTENSION pDevExt; PDEVICE_OBJECT pDevObj; KdPrint(("\n--------------------------------------!\n")); KdPrint(("Enter DriverEntry!\n")); //注册相关例程 pDriverObject->DriverUnload = UnloadRoutine; pDriverObject->MajorFunction[IRP_MJ_CREATE] = DispatchRoutine; pDriverObject->MajorFunction[IRP_MJ_READ] = DispatchRoutine; pDriverObject->MajorFunction[IRP_MJ_WRITE] = DispatchRoutine; pDriverObject->MajorFunction[IRP_MJ_CLOSE] = DispatchRoutine; //初始化相关字符串 UNICODE_STRING ustrDeviceName; //设备名 UNICODE_STRING ustrSymLinkName; //符号链接名 RtlInitUnicodeString(&ustrDeviceName,L"\\Device\\MyDDKDevice1"); RtlInitUnicodeString(&ustrSymLinkName,L"\\??\\MyDDKDriver1"); //创建设备对象 status = IoCreateDevice(pDriverObject,sizeof(DEVICE_EXTENSION),&ustrDeviceName,FILE_DEVICE_UNKNOWN,0,TRUE,&pDevObj); if (!NT_SUCCESS(status)) { KdPrint(("Create Device Failure!\n")); return status; } pDevObj->Flags |= DO_BUFFERED_IO; pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension; pDevExt->ustrDeviceName = ustrDeviceName; pDevExt->pDevice = pDevObj; //创建符号链接 pDevExt->ustrSymLinkName = ustrSymLinkName; status = IoCreateSymbolicLink(&ustrSymLinkName,&ustrDeviceName); if (!NT_SUCCESS(status)) { IoDeleteDevice(pDevObj); return status; } KdPrint(("Leave DriverEntry! stauts=%d",status)); return status; }
#define KdPrint(_x_) DbgPrint _x_
VOID RtlInitUnicodeString(PUNICODE_STRING DestinationString,PCWSTR SourceString);
NTSTATUS IoCreateDevice( IN PDRIVER_OBJECT DriverObject, IN ULONG DeviceExtensionSize, IN PUNICODE_STRING DeviceName OPTIONAL, IN DEVICE_TYPE DeviceType, IN ULONG DeviceCharacteristics, IN BOOLEAN Exclusive, OUT PDEVICE_OBJECT *DeviceObject );
NTSTATUS IoCreateSymbolicLink( IN PUNICODE_STRING SymbolicLinkName, IN PUNICODE_STRING DeviceName );
VOID UnloadRoutine(__in struct _DRIVER_OBJECT *DriverObject);
#pragma PAGECODE VOID UnloadRoutine(__in struct _DRIVER_OBJECT *pDriverObject) { PDEVICE_OBJECT pNextObj; PDEVICE_EXTENSION pDevExt; pNextObj = pDriverObject->DeviceObject; KdPrint(("Enter unload routine!\n")); while(pNextObj != NULL) { pDevExt = (PDEVICE_EXTENSION)pNextObj->DeviceExtension; //删除符号链接 IoDeleteSymbolicLink(&pDevExt->ustrSymLinkName); pNextObj = pNextObj->NextDevice; //删除设备 IoDeleteDevice(pDevExt->pDevice); } KdPrint(("Leave unload routine!\n")); KdPrint(("--------------------------------------!\n")); }
#pragma PAGECODE NTSTATUS DispatchRoutine(__in struct _DEVICE_OBJECT *pDeviceObject, __in struct _IRP *pIrp) { KdPrint(("Enter dispatch routine!\n")); NTSTATUS status = STATUS_SUCCESS; //完成IRP pIrp->IoStatus.Status = status; pIrp->IoStatus.Information = 0; IoCompleteRequest(pIrp,IO_NO_INCREMENT); KdPrint(("Leave dispatch routine!\n")); return status; }
#ifdef __cplusplus extern "C" { #endif #include <NTDDK.h> #ifdef __cplusplus }; #endif #define PAGECODE code_seg("PAGE") #define LOCKEDCODE code_seg() #define INITCODE code_seg("INIT") #define PAGEDATA data_seg("PAGE") #define LOCKEDDATA data_seg() #define INITDATA data_seg("INIT") #define arrarysize(arr) (sizeof(arr)/sizeof(arr)[0]) typedef struct _DEVICE_EXTENSION { PDEVICE_OBJECT pDevice; UNICODE_STRING ustrDeviceName; UNICODE_STRING ustrSymLinkName; } DEVICE_EXTENSION , *PDEVICE_EXTENSION; //函数声明 NTSTATUS DispatchRoutine(__in struct _DEVICE_OBJECT *DeviceObject, __in struct _IRP *Irp); VOID UnloadRoutine(__in struct _DRIVER_OBJECT *DriverObject); /// #pragma INITCODE extern "C" NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING pRegistryPath) { NTSTATUS status; PDEVICE_EXTENSION pDevExt; PDEVICE_OBJECT pDevObj; KdPrint(("\n--------------------------------------!\n")); KdPrint(("pRegistryPath value:%ws",pRegistryPath)); KdPrint(("Enter DriverEntry!\n")); //注册相关例程 pDriverObject->DriverUnload = UnloadRoutine; pDriverObject->MajorFunction[IRP_MJ_CREATE] = DispatchRoutine; pDriverObject->MajorFunction[IRP_MJ_READ] = DispatchRoutine; pDriverObject->MajorFunction[IRP_MJ_WRITE] = DispatchRoutine; pDriverObject->MajorFunction[IRP_MJ_CLOSE] = DispatchRoutine; //初始化相关字符串 UNICODE_STRING ustrDeviceName; //设备名 UNICODE_STRING ustrSymLinkName; //符号链接名 RtlInitUnicodeString(&ustrDeviceName,L"\\Device\\MyDDKDevice1"); RtlInitUnicodeString(&ustrSymLinkName,L"\\??\\MyDDKDriver1"); //创建设备对象 status = IoCreateDevice(pDriverObject,sizeof(DEVICE_EXTENSION),&ustrDeviceName,FILE_DEVICE_UNKNOWN,0,TRUE,&pDevObj); if (!NT_SUCCESS(status)) { KdPrint(("Create Device Failure!\n")); return status; } pDevObj->Flags |= DO_BUFFERED_IO; pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension; pDevExt->ustrDeviceName = ustrDeviceName; pDevExt->pDevice = pDevObj; //创建符号链接 pDevExt->ustrSymLinkName = ustrSymLinkName; status = IoCreateSymbolicLink(&ustrSymLinkName,&ustrDeviceName); if (!NT_SUCCESS(status)) { IoDeleteDevice(pDevObj); return status; } KdPrint(("Leave DriverEntry! stauts=%d",status)); return status; } #pragma PAGECODE NTSTATUS DispatchRoutine(__in struct _DEVICE_OBJECT *pDeviceObject, __in struct _IRP *pIrp) { KdPrint(("Enter dispatch routine!\n")); NTSTATUS status = STATUS_SUCCESS; //完成IRP pIrp->IoStatus.Status = status; pIrp->IoStatus.Information = 0; IoCompleteRequest(pIrp,IO_NO_INCREMENT); KdPrint(("Leave dispatch routine!\n")); return status; } #pragma PAGECODE VOID UnloadRoutine(__in struct _DRIVER_OBJECT *pDriverObject) { PDEVICE_OBJECT pNextObj; PDEVICE_EXTENSION pDevExt; pNextObj = pDriverObject->DeviceObject; KdPrint(("Enter unload routine!\n")); while(pNextObj != NULL) { pDevExt = (PDEVICE_EXTENSION)pNextObj->DeviceExtension; //删除符号链接 IoDeleteSymbolicLink(&pDevExt->ustrSymLinkName); pNextObj = pNextObj->NextDevice; //删除设备 IoDeleteDevice(pDevExt->pDevice); } KdPrint(("Leave unload routine!\n")); KdPrint(("--------------------------------------!\n")); }
NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject,PUNICODE_STRING pRegistryPath)
#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
#define NT_INFORMATION(Status) ((((ULONG)(Status)) >> 30) == 1) #define NT_WARNING(Status) ((((ULONG)(Status)) >> 30) == 2) #define NT_ERROR(Status) ((((ULONG)(Status)) >> 30) == 3)
typedef struct _UNICODE_STRING { USHORT Length; USHORT MaximumLength; PWSTR Buffer; } UNICODE_STRING, *PUNICODE_STRING;
typedef struct _DRIVER_OBJECT { CSHORT Type; CSHORT Size; PDEVICE_OBJECT DeviceObject; ULONG Flags; PVOID DriverStart; ULONG DriverSize; PVOID DriverSection; PDRIVER_EXTENSION DriverExtension; UNICODE_STRING DriverName; PUNICODE_STRING HardwareDatabase; PFAST_IO_DISPATCH FastIoDispatch; PDRIVER_INITIALIZE DriverInit; PDRIVER_STARTIO DriverStartIo; PDRIVER_UNLOAD DriverUnload; PDRIVER_DISPATCH MajorFunction[IRP_MJ_MAXIMUM_FUNCTION + 1]; } DRIVER_OBJECT;
typedef struct _DEVICE_OBJECT { CSHORT Type; USHORT Size; LONG ReferenceCount; struct _DRIVER_OBJECT *DriverObject; struct _DEVICE_OBJECT *NextDevice; struct _DEVICE_OBJECT *AttachedDevice; struct _IRP *CurrentIrp; PIO_TIMER Timer; ULONG Flags; ULONG Characteristics; __volatile PVPB Vpb; PVOID DeviceExtension; DEVICE_TYPE DeviceType; CCHAR StackSize; union { LIST_ENTRY ListEntry; WAIT_CONTEXT_BLOCK Wcb; } Queue; ULONG AlignmentRequirement; KDEVICE_QUEUE DeviceQueue; KDPC Dpc; ULONG ActiveThreadCount; PSECURITY_DESCRIPTOR SecurityDescriptor; KEVENT DeviceLock; USHORT SectorSize; USHORT Spare1; struct _DEVOBJ_EXTENSION * DeviceObjectExtension; PVOID Reserved; } DEVICE_OBJECT, *PDEVICE_OBJECT;
#ifdef __cplusplus extern "C" { #endif #include <NTDDK.h> #ifdef __cplusplus }; #endif #define PAGECODE code_seg("PAGE") #define LOCKEDCODE code_seg() #define INITCODE code_seg("INIT") #define PAGEDATA data_seg("PAGE") #define LOCKEDDATA data_seg() #define INITDATA data_seg("INIT") typedef struct _DEVICE_EXTENSION { PDEVICE_OBJECT pDevice; UNICODE_STRING ustrDeviceName; UNICODE_STRING ustrSymLinkName; } DEVICE_EXTENSION , *PDEVICE_EXTENSION; //函数声明 NTSTATUS DispatchRoutine(__in struct _DEVICE_OBJECT *DeviceObject, __in struct _IRP *Irp); VOID UnloadRoutine(__in struct _DRIVER_OBJECT *DriverObject); /// #pragma INITCODE extern "C" NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING pRegistryPath) { NTSTATUS status; PDEVICE_EXTENSION pDevExt; PDEVICE_OBJECT pDevObj; KdPrint(("\n--------------------------------------!\n")); KdPrint(("Enter DriverEntry!\n")); //注册相关例程 pDriverObject->DriverUnload = UnloadRoutine; pDriverObject->MajorFunction[IRP_MJ_CREATE] = DispatchRoutine; pDriverObject->MajorFunction[IRP_MJ_READ] = DispatchRoutine; pDriverObject->MajorFunction[IRP_MJ_WRITE] = DispatchRoutine; pDriverObject->MajorFunction[IRP_MJ_CLOSE] = DispatchRoutine; //初始化相关字符串 UNICODE_STRING ustrDeviceName; //设备名 UNICODE_STRING ustrSymLinkName; //符号链接名 RtlInitUnicodeString(&ustrDeviceName,L"\\Device\\MyDDKDevice1"); RtlInitUnicodeString(&ustrSymLinkName,L"\\??\\MyDDKDriver1"); //创建设备对象 status = IoCreateDevice(pDriverObject,sizeof(DEVICE_EXTENSION),&ustrDeviceName,FILE_DEVICE_UNKNOWN,0,TRUE,&pDevObj); if (!NT_SUCCESS(status)) { KdPrint(("Create Device Failure!\n")); return status; } pDevObj->Flags |= DO_BUFFERED_IO; pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension; pDevExt->ustrDeviceName = ustrDeviceName; pDevExt->pDevice = pDevObj; //创建符号链接 pDevExt->ustrSymLinkName = ustrSymLinkName; status = IoCreateSymbolicLink(&ustrSymLinkName,&ustrDeviceName); if (!NT_SUCCESS(status)) { IoDeleteDevice(pDevObj); return status; } KdPrint(("Leave DriverEntry! stauts=%d",status)); return status; }
#define KdPrint(_x_) DbgPrint _x_
VOID RtlInitUnicodeString(PUNICODE_STRING DestinationString,PCWSTR SourceString);
NTSTATUS IoCreateDevice( IN PDRIVER_OBJECT DriverObject, IN ULONG DeviceExtensionSize, IN PUNICODE_STRING DeviceName OPTIONAL, IN DEVICE_TYPE DeviceType, IN ULONG DeviceCharacteristics, IN BOOLEAN Exclusive, OUT PDEVICE_OBJECT *DeviceObject );
NTSTATUS IoCreateSymbolicLink( IN PUNICODE_STRING SymbolicLinkName, IN PUNICODE_STRING DeviceName );
VOID UnloadRoutine(__in struct _DRIVER_OBJECT *DriverObject);
#pragma PAGECODE VOID UnloadRoutine(__in struct _DRIVER_OBJECT *pDriverObject) { PDEVICE_OBJECT pNextObj; PDEVICE_EXTENSION pDevExt; pNextObj = pDriverObject->DeviceObject; KdPrint(("Enter unload routine!\n")); while(pNextObj != NULL) { pDevExt = (PDEVICE_EXTENSION)pNextObj->DeviceExtension; //删除符号链接 IoDeleteSymbolicLink(&pDevExt->ustrSymLinkName); pNextObj = pNextObj->NextDevice; //删除设备 IoDeleteDevice(pDevExt->pDevice); } KdPrint(("Leave unload routine!\n")); KdPrint(("--------------------------------------!\n")); }
#pragma PAGECODE NTSTATUS DispatchRoutine(__in struct _DEVICE_OBJECT *pDeviceObject, __in struct _IRP *pIrp) { KdPrint(("Enter dispatch routine!\n")); NTSTATUS status = STATUS_SUCCESS; //完成IRP pIrp->IoStatus.Status = status; pIrp->IoStatus.Information = 0; IoCompleteRequest(pIrp,IO_NO_INCREMENT); KdPrint(("Leave dispatch routine!\n")); return status; }
#ifdef __cplusplus extern "C" { #endif #include <NTDDK.h> #ifdef __cplusplus }; #endif #define PAGECODE code_seg("PAGE") #define LOCKEDCODE code_seg() #define INITCODE code_seg("INIT") #define PAGEDATA data_seg("PAGE") #define LOCKEDDATA data_seg() #define INITDATA data_seg("INIT") #define arrarysize(arr) (sizeof(arr)/sizeof(arr)[0]) typedef struct _DEVICE_EXTENSION { PDEVICE_OBJECT pDevice; UNICODE_STRING ustrDeviceName; UNICODE_STRING ustrSymLinkName; } DEVICE_EXTENSION , *PDEVICE_EXTENSION; //函数声明 NTSTATUS DispatchRoutine(__in struct _DEVICE_OBJECT *DeviceObject, __in struct _IRP *Irp); VOID UnloadRoutine(__in struct _DRIVER_OBJECT *DriverObject); /// #pragma INITCODE extern "C" NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING pRegistryPath) { NTSTATUS status; PDEVICE_EXTENSION pDevExt; PDEVICE_OBJECT pDevObj; KdPrint(("\n--------------------------------------!\n")); KdPrint(("pRegistryPath value:%ws",pRegistryPath)); KdPrint(("Enter DriverEntry!\n")); //注册相关例程 pDriverObject->DriverUnload = UnloadRoutine; pDriverObject->MajorFunction[IRP_MJ_CREATE] = DispatchRoutine; pDriverObject->MajorFunction[IRP_MJ_READ] = DispatchRoutine; pDriverObject->MajorFunction[IRP_MJ_WRITE] = DispatchRoutine; pDriverObject->MajorFunction[IRP_MJ_CLOSE] = DispatchRoutine; //初始化相关字符串 UNICODE_STRING ustrDeviceName; //设备名 UNICODE_STRING ustrSymLinkName; //符号链接名 RtlInitUnicodeString(&ustrDeviceName,L"\\Device\\MyDDKDevice1"); RtlInitUnicodeString(&ustrSymLinkName,L"\\??\\MyDDKDriver1"); //创建设备对象 status = IoCreateDevice(pDriverObject,sizeof(DEVICE_EXTENSION),&ustrDeviceName,FILE_DEVICE_UNKNOWN,0,TRUE,&pDevObj); if (!NT_SUCCESS(status)) { KdPrint(("Create Device Failure!\n")); return status; } pDevObj->Flags |= DO_BUFFERED_IO; pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension; pDevExt->ustrDeviceName = ustrDeviceName; pDevExt->pDevice = pDevObj; //创建符号链接 pDevExt->ustrSymLinkName = ustrSymLinkName; status = IoCreateSymbolicLink(&ustrSymLinkName,&ustrDeviceName); if (!NT_SUCCESS(status)) { IoDeleteDevice(pDevObj); return status; } KdPrint(("Leave DriverEntry! stauts=%d",status)); return status; } #pragma PAGECODE NTSTATUS DispatchRoutine(__in struct _DEVICE_OBJECT *pDeviceObject, __in struct _IRP *pIrp) { KdPrint(("Enter dispatch routine!\n")); NTSTATUS status = STATUS_SUCCESS; //完成IRP pIrp->IoStatus.Status = status; pIrp->IoStatus.Information = 0; IoCompleteRequest(pIrp,IO_NO_INCREMENT); KdPrint(("Leave dispatch routine!\n")); return status; } #pragma PAGECODE VOID UnloadRoutine(__in struct _DRIVER_OBJECT *pDriverObject) { PDEVICE_OBJECT pNextObj; PDEVICE_EXTENSION pDevExt; pNextObj = pDriverObject->DeviceObject; KdPrint(("Enter unload routine!\n")); while(pNextObj != NULL) { pDevExt = (PDEVICE_EXTENSION)pNextObj->DeviceExtension; //删除符号链接 IoDeleteSymbolicLink(&pDevExt->ustrSymLinkName); pNextObj = pNextObj->NextDevice; //删除设备 IoDeleteDevice(pDevExt->pDevice); } KdPrint(("Leave unload routine!\n")); KdPrint(("--------------------------------------!\n")); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。