?? vcd.c
字號:
#include <ntddk.h>
#include <ntdef.h>
#include <ntdddisk.h>
#include <ntddcdrm.h>
#include <ntddcdvd.h>
#include <ntverp.h>
#include <stdio.h>
#include <ntifs.h>
#include <DeviceThread.h>
#include <VCD.h>
HANDLE device_root_handle;
NTSTATUS
DriverEntry (
IN PDRIVER_OBJECT DriverObject,
IN PUNICODE_STRING RegistryPath
)
{
NTSTATUS status = STATUS_SUCCESS;
int i;
UNICODE_STRING device_dir_name;
OBJECT_ATTRIBUTES object_attributes;
DbgPrint("IrpUnload\n");
//創建設備目錄 \\Device
RtlInitUnicodeString(&device_dir_name, DEVICE_DIR_NAME);
InitializeObjectAttributes(
&object_attributes,
&device_dir_name,
OBJ_PERMANENT,
NULL,
NULL
);
status = ZwCreateDirectoryObject(
&device_root_handle,
DIRECTORY_ALL_ACCESS, //全部能力
&object_attributes
);
if (!NT_SUCCESS(status))
{
DbgPrint("ZwCreateDirectoryObject Failed\n");
return status;
}
//創建臨時對象
ZwMakeTemporaryObject(device_root_handle);
for (i=0;i< DEVICE_COUNT;i++)
{
status = IoCreateCDROMDevice(DriverObject, i);
/*
if (NT_SUCCESS(status))
DbgPrint("IoCreateCDROMDevice OK\n");
else
DbgPrint("IoCreateCDROMDevice Failed\n");
*/
}
DriverObject->MajorFunction[IRP_MJ_CREATE] =
DriverObject->MajorFunction[IRP_MJ_CLOSE] = IrpCreateClose;
DriverObject->MajorFunction[IRP_MJ_READ] =
DriverObject->MajorFunction[IRP_MJ_WRITE] = IrpReadWrite;
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = IrpDeviceControl;
DriverObject->DriverUnload = IrpUnload;
return STATUS_SUCCESS;
}
//創建CDROM設備,每個設備都會有一個處理線程
NTSTATUS
IoCreateCDROMDevice(
IN PDRIVER_OBJECT DriverObject,
IN ULONG Number
)
{
NTSTATUS status = STATUS_SUCCESS;
WCHAR device_name_buffer[MAXIMUM_FILENAME_LENGTH] = {0};
UNICODE_STRING device_name;
ANSI_STRING adevice_name;
PDEVICE_OBJECT device_object;
PDEVICE_EXTENSION device_extension;
HANDLE thread_handle;
swprintf(
device_name_buffer,
DEVICE_FULL_NAME L"%u",
Number
);
RtlInitUnicodeString(&device_name, device_name_buffer);
RtlUnicodeStringToAnsiString(&adevice_name, &device_name, TRUE);
DbgPrint("adevice_name:%s\n", adevice_name.Buffer);
RtlFreeAnsiString(&adevice_name);
status = IoCreateDevice(
DriverObject,
sizeof(DEVICE_EXTENSION),
&device_name,
FILE_DEVICE_CD_ROM,
0,
FALSE,
&device_object
);
if (!NT_SUCCESS(status))
{
DbgPrint("IoCreateDevice Failed!\n");
return status;
}
device_object->Flags |= DO_DIRECT_IO;
device_extension = (PDEVICE_EXTENSION) device_object->DeviceExtension;
device_extension->index = Number;
device_extension->media_in_device = FALSE;
device_object->Characteristics |= FILE_READ_ONLY_DEVICE; //CDROM只能是只讀的
//初始化線程List
InitializeListHead(&device_extension->list_head);
//初始化自旋鎖
KeInitializeSpinLock(&device_extension->list_lock);
//初始化事件
KeInitializeEvent(
&device_extension->k_event,
SynchronizationEvent,
FALSE
);
device_extension->terminate_thread = FALSE;
//創建系統線程
status = PsCreateSystemThread(
&thread_handle,
(ACCESS_MASK) 0L,
NULL,
NULL,
NULL,
DeviceThread,
device_object
);
if (!NT_SUCCESS(status))
{
IoDeleteDevice(device_object);
DbgPrint("PsCreateSystemThread Failed!\n");
return status;
}
status = ObReferenceObjectByHandle(
thread_handle,
THREAD_ALL_ACCESS,
NULL,
KernelMode,
&device_extension->thread_pointer,
NULL
);
if (!NT_SUCCESS(status))
{
ZwClose(thread_handle);
device_extension->terminate_thread = TRUE;
KeSetEvent(
&device_extension->k_event,
(KPRIORITY) 0,
FALSE
);
IoDeleteDevice(device_object);
DbgPrint("ObReferenceObjectByHandle Failed!\n");
return status;
}
ZwClose(thread_handle);
return STATUS_SUCCESS;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -