首页
社区
课程
招聘
[求助]又蓝了?
发表于: 2012-10-7 13:44 3512

[求助]又蓝了?

2012-10-7 13:44
3512
为什么这一段代码老是蓝屏呢?用dbg调试,发现是DispatchUnload例程里面的IoDeleteDevice蓝的

#include <Ntifs.h>
#include <Ntstrsafe.h>
#include <Ntimage.h>

typedef struct _DEVICE_EXTENSION
{
	UNICODE_STRING	ustrDeviceName;//设备名称
	UNICODE_STRING	ustrSymLinkName;//符号链接名

}DEVICE_EXTENSION, *PDEVICE_EXTENSION;

NTSTATUS MyCreateDevice(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING pustrDeviceName, PUNICODE_STRING pustrSymLinkName);
VOID DispatchUnload (IN PDRIVER_OBJECT pDriverObject);
NTSTATUS DispatchCreate(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp);
NTSTATUS DispatchDeviceIOControl(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp);
NTSTATUS DispatchClose(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp);

#pragma code_seg("INIT")
NTSTATUS DriverEntry( IN PDRIVER_OBJECT pDriverObject, IN PUNICODE_STRING pRegistryPath )
{
	NTSTATUS			status = STATUS_UNSUCCESSFUL;
	UNICODE_STRING		ustrDeviceName;
	UNICODE_STRING		ustrSymLinkName;

	//注册其他驱动调用函数入口
	pDriverObject->DriverUnload = DispatchUnload;
	pDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DispatchDeviceIOControl;
	pDriverObject->MajorFunction[IRP_MJ_CREATE] = DispatchCreate;
	pDriverObject->MajorFunction[IRP_MJ_CLOSE] = DispatchClose;

	//创建设备名称
	RtlInitUnicodeString(&ustrDeviceName, L"\\Device\\DevAAD");
	//创建符号链接
	RtlInitUnicodeString(&ustrSymLinkName, L"\\??\\DevAAD");

	//创建设备
	status = MyCreateDevice(pDriverObject, &ustrDeviceName, &ustrSymLinkName);
	if (!NT_SUCCESS(status)) 
		return status;

	return status;
}

#pragma code_seg("PAGE")
NTSTATUS MyCreateDevice(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING pustrDeviceName, PUNICODE_STRING pustrSymLinkName)
{
	NTSTATUS			status = STATUS_UNSUCCESSFUL;
	PDEVICE_OBJECT		pDevObj;
	PDEVICE_EXTENSION	pDevExt;

	if(!pustrDeviceName || !pustrSymLinkName)
		return status;

	//创建设备
	status = IoCreateDevice( pDriverObject,
		sizeof(DEVICE_EXTENSION),
		pustrDeviceName,
		FILE_DEVICE_UNKNOWN,
		0,
		TRUE,
		&pDevObj );
	if (!NT_SUCCESS(status))
		return status;
	
	//创建符号链接
	status = IoCreateSymbolicLink(pustrSymLinkName, pustrDeviceName );
	if (!NT_SUCCESS(status)) 
	{
		IoDeleteDevice( pDevObj );
		return status;
	}
	
	//取消设备正在初始化标志
	pDevObj->Flags &= ~DO_DEVICE_INITIALIZING;
	//直接读写设备
	pDevObj->Flags |= DO_DIRECT_IO;
	//获取设备扩展
	pDevExt = (PDEVICE_EXTENSION)(pDevObj->DeviceObjectExtension);

	//填充设备扩展
	pDevExt->ustrDeviceName = *pustrDeviceName;
	pDevExt->ustrSymLinkName = *pustrSymLinkName;

	return status;
}

#pragma code_seg("PAGE")
VOID DispatchUnload (IN PDRIVER_OBJECT pDriverObject) 
{
	PDEVICE_OBJECT		pDevObj;
	PDEVICE_OBJECT		pNextDevObj;
	PDEVICE_EXTENSION	pDevExt;

	pNextDevObj = pDriverObject->DeviceObject;
	while(pDevObj = pNextDevObj)
	{
		//下一个设备
		pNextDevObj = pDevObj->NextDevice;

		//获取设备扩展
		pDevExt = (PDEVICE_EXTENSION)(pDevObj->DeviceObjectExtension);
		//删除符号链接
		IoDeleteSymbolicLink(&pDevExt->ustrSymLinkName);

		//删除设备
		IoDeleteDevice( pDevObj );
	}
}

#pragma code_seg("PAGE")
NTSTATUS DispatchCreate(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp) 
{
	NTSTATUS			status = STATUS_UNSUCCESSFUL;

	return status;
}

#pragma code_seg("PAGE")
NTSTATUS DispatchDeviceIOControl(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp)
{
	NTSTATUS			status = STATUS_UNSUCCESSFUL;

	return status;
}

#pragma code_seg("PAGE")
NTSTATUS DispatchClose(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp) 
{
	NTSTATUS			status = STATUS_UNSUCCESSFUL;

	return status;
}

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (2)
雪    币: 446
活跃值: (186)
能力值: ( LV12,RANK:230 )
在线值:
发帖
回帖
粉丝
2
我已经解决了
2012-10-7 14:18
0
雪    币: 8835
活跃值: (2404)
能力值: ( LV12,RANK:760 )
在线值:
发帖
回帖
粉丝
3
while(pDevObj = pNextDevObj)

如此代码我纠结蛋痛
2012-10-7 15:07
0
游客
登录 | 注册 方可回帖
返回
//