📄 driverentry.c
字号:
#include "Driver.h"
NTSTATUS CreateDevice(IN PDRIVER_OBJECT DriverObject, IN ULONG DeviceNumber);
#ifdef ALLOC_PRAGMA
#pragma alloc_text (INIT, DriverEntry)
#pragma alloc_text (INIT, CreateDevice)
#endif
PDEVICE_OBJECT Devices[16]; // Global variable to store pointers to the device objects we create.
/*************************************************************************************************/
NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING UniRegistryPath)
{
// Initialize the driver object with this driver's entry points.
DriverObject->MajorFunction[IRP_MJ_CREATE] = DispatchCreate;
DriverObject->MajorFunction[IRP_MJ_CLOSE] = DispatchClose;
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DispatchDevCtrl;
DriverObject->MajorFunction[IRP_MJ_READ] = DispatchRead;
DriverObject->DriverUnload = Unload;
// Create a device object.
CreateDevice(DriverObject, 0);
return STATUS_SUCCESS;
}
/*************************************************************************************************/
NTSTATUS CreateDevice(IN PDRIVER_OBJECT DriverObject, IN ULONG DeviceNumber)
{
NTSTATUS status;
PFDO_DATA deviceExtension;
UNICODE_STRING deviceName, portName, linkName, number;
WCHAR deviceNameBuffer[256];
WCHAR portNameBuffer[256];
WCHAR linkNameBuffer[256];
WCHAR numberBuffer[20];
HANDLE threadHandle;
// Initialise strings
number.Buffer = numberBuffer;
number.MaximumLength = 20;
deviceName.Buffer = deviceNameBuffer;
deviceName.MaximumLength = 256*2;
portName.Buffer = portNameBuffer;
portName.MaximumLength = 256*2;
linkName.Buffer = linkNameBuffer;
linkName.MaximumLength = 256*2;
// Form the base NT device name...
deviceName.Length = 0;
RtlAppendUnicodeToString(&deviceName, L"\\Device\\SAMPLEDEV");
number.Length = 0;
RtlIntegerToUnicodeString(DeviceNumber, 10, &number);
RtlAppendUnicodeStringToString(&deviceName, &number);
// Create a Device object for this device...
status = IoCreateDevice(DriverObject, sizeof(FDO_DATA), &deviceName, FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN, FALSE, &(Devices[DeviceNumber]));
if (!NT_SUCCESS(status))
{
Devices[DeviceNumber] = NULL;
return status;
}
Devices[DeviceNumber]->Flags |= DO_DIRECT_IO;
// Initialize the Device Extension
deviceExtension = Devices[DeviceNumber]->DeviceExtension;
RtlZeroMemory(deviceExtension, sizeof(FDO_DATA));
deviceExtension->DeviceObject = Devices[DeviceNumber];
// Form the Win32 symbolic link name.
linkName.Length = 0;
RtlAppendUnicodeToString(&linkName, L"\\DosDevices\\SAMPLEDEV");
number.Length = 0;
RtlIntegerToUnicodeString(DeviceNumber, 10, &number);
RtlAppendUnicodeStringToString(&linkName, &number);
// Create a symbolic link so our device is visible to Win32...
status = IoCreateSymbolicLink(&linkName, &deviceName);
if(!NT_SUCCESS(status))
{
IoDeleteDevice(Devices[DeviceNumber]);
Devices[DeviceNumber] = NULL;
return status;
}
// Initialize our custom IRP queues and create a thread for sending messages.
KeInitializeSpinLock(&deviceExtension->ReadQueueLock);
InitializeListHead(&deviceExtension->ReadIrpQueue);
IoCsqInitialize(&deviceExtension->CSReadQueue, CSReadInsertIrp, CSReadRemoveIrp, CSReadPeekNextIrp, CSReadAcquireLock, CSReadReleaseLock, CSReadCompleteCanceledIrp);
Devices[DeviceNumber]->Flags &= ~DO_DEVICE_INITIALIZING;
return status;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -