/
/
卸载驱动执行
VOID UnDriver(PDRIVER_OBJECT pDriver)
{
PDEVICE_OBJECT pDev;
/
/
用来取得要删除设备对象
UNICODE_STRING SymLinkName;
/
/
局部变量symLinkName
pDev
=
pDriver
-
>DeviceObject;
IoDeleteDevice(pDev);
/
/
调用IoDeleteDevice用于删除设备
RtlInitUnicodeString(&SymLinkName, L
"\\??\\LySharkDriver"
);
/
/
初始化字符串将symLinkName定义成需要删除的符号链接名称
IoDeleteSymbolicLink(&SymLinkName);
/
/
调用IoDeleteSymbolicLink删除符号链接
DbgPrint(
"驱动卸载完毕..."
);
}
/
/
创建设备连接
/
/
LyShark.com
NTSTATUS CreateDriverObject(IN PDRIVER_OBJECT pDriver)
{
NTSTATUS Status;
PDEVICE_OBJECT pDevObj;
UNICODE_STRING DriverName;
UNICODE_STRING SymLinkName;
/
/
创建设备名称字符串
RtlInitUnicodeString(&DriverName, L
"\\Device\\LySharkDriver"
);
Status
=
IoCreateDevice(pDriver,
0
, &DriverName, FILE_DEVICE_UNKNOWN,
0
, TRUE, &pDevObj);
/
/
指定通信方式为缓冲区
pDevObj
-
>Flags |
=
DO_BUFFERED_IO;
/
/
创建符号链接
RtlInitUnicodeString(&SymLinkName, L
"\\??\\LySharkDriver"
);
Status
=
IoCreateSymbolicLink(&SymLinkName, &DriverName);
return
STATUS_SUCCESS;
}
/
/
创建回调函数
NTSTATUS DispatchCreate(PDEVICE_OBJECT pDevObj, PIRP pIrp)
{
pIrp
-
>IoStatus.Status
=
STATUS_SUCCESS;
/
/
返回成功
DbgPrint(
"派遣函数 IRP_MJ_CREATE 执行 \n"
);
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
/
/
指示完成此IRP
return
STATUS_SUCCESS;
/
/
返回成功
}
/
/
关闭回调函数
NTSTATUS DispatchClose(PDEVICE_OBJECT pDevObj, PIRP pIrp)
{
pIrp
-
>IoStatus.Status
=
STATUS_SUCCESS;
/
/
返回成功
DbgPrint(
"派遣函数 IRP_MJ_CLOSE 执行 \n"
);
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
/
/
指示完成此IRP
return
STATUS_SUCCESS;
/
/
返回成功
}
/
/
默认派遣函数
NTSTATUS DriverDefaultHandle(PDEVICE_OBJECT pDevObj, PIRP pIrp)
{
NTSTATUS status
=
STATUS_SUCCESS;
pIrp
-
>IoStatus.Status
=
status;
pIrp
-
>IoStatus.Information
=
0
;
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
return
status;
}
/
/
入口函数
/
/
By: LyShark
NTSTATUS DriverEntry(PDRIVER_OBJECT pDriver, PUNICODE_STRING RegistryPath)
{
DbgPrint(
"hello lyshark \n"
);
/
/
调用创建设备
CreateDriverObject(pDriver);
pDriver
-
>DriverUnload
=
UnDriver;
/
/
卸载函数
pDriver
-
>MajorFunction[IRP_MJ_CREATE]
=
DispatchCreate;
/
/
创建派遣函数
pDriver
-
>MajorFunction[IRP_MJ_CLOSE]
=
DispatchClose;
/
/
关闭派遣函数
/
/
初始化其他派遣
for
(ULONG i
=
0
; i < IRP_MJ_MAXIMUM_FUNCTION; i
+
+
)
{
DbgPrint(
"初始化派遣: %d \n"
, i);
pDriver
-
>MajorFunction[i]
=
DriverDefaultHandle;
}
DbgPrint(
"驱动加载完成..."
);
return
STATUS_SUCCESS;
}