首页
社区
课程
招聘
驱动程序测试问题()
发表于: 2014-1-18 16:11 9560

驱动程序测试问题()

2014-1-18 16:11
9560
为什么我的测试程序测试不成功?
测试程序的CreateFile的返回值-1, GetLastError 错误码是123(文件名、目录名或卷标语法不正确。)
我的驱动程序加载后用winObj是可以查看到FirstDevice的 ,测试程序运行时就是失败 ???

驱动程序:
#include "ntddk.h"

NTSTATUS CreateDevice(PDRIVER_OBJECT pDriverObject);
NTSTATUS CreateComplateRoutine(PDEVICE_OBJECT pDeviceObj, PIRP pIrp);
NTSTATUS ReadComplateRoutine(PDEVICE_OBJECT pDeviceObj, PIRP pIrp);
NTSTATUS WriteComplateRoutine(PDEVICE_OBJECT pDeviceObj, PIRP pIrp);
NTSTATUS CloseComplateRoutine(PDEVICE_OBJECT pDeviceObj, PIRP pIrp);
VOID MyDriverUnoad(PDRIVER_OBJECT pDriverObject);



NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject,PUNICODE_STRING pRegistryPath)
{
	NTSTATUS status;
	status = CreateDevice(pDriverObject);
	KdPrint(("CreateDevice."));
	
	if (!NT_SUCCESS(status))
	{
		KdPrint(("Create device faild."));
	}
	else
	{
		KdPrint(("Create device success."));
		KdPrint(("%wZ", pRegistryPath));
	}
	pDriverObject->MajorFunction[IRP_MJ_CREATE] = CreateComplateRoutine;
	pDriverObject->MajorFunction[IRP_MJ_READ] = ReadComplateRoutine;
	pDriverObject->MajorFunction[IRP_MJ_WRITE] = WriteComplateRoutine;
	pDriverObject->MajorFunction[IRP_MJ_CLOSE] = CloseComplateRoutine;

	pDriverObject->DriverUnload = MyDriverUnoad;

	return STATUS_SUCCESS;
}

NTSTATUS CreateDevice(PDRIVER_OBJECT pDriverObject)
{
	NTSTATUS status;
	PDEVICE_OBJECT pDevObj;
	UNICODE_STRING usDevName;
	UNICODE_STRING usSymName;
	RtlInitUnicodeString(&usDevName, L"\\Device\\FirstDevice");
	RtlInitUnicodeString(&usSymName, L"\\??\\FirstDevice");;

	status = IoCreateDevice(pDriverObject,
							0,
							&usDevName,
							FILE_DEVICE_UNKNOWN,
							0,
							TRUE,
							&pDevObj);
	pDevObj->Flags |= DO_BUFFERED_IO;

	if (!NT_SUCCESS(status))
	{
		KdPrint(("IoCreateDevice faild."));
		return status;
	}

	status = IoCreateSymbolicLink(&usSymName, &usDevName);
	if (!NT_SUCCESS(status))
	{
		KdPrint(("IoCreateSymbolicLink faild."));
		IoDeleteDevice(pDevObj);
		return status;
	}

	return STATUS_SUCCESS;
}

NTSTATUS CreateComplateRoutine (PDEVICE_OBJECT pDeviceObj, PIRP pIrp)
{
	NTSTATUS status;
	status = STATUS_SUCCESS;
	KdPrint(("Create"));
	pIrp->IoStatus.Status = status;
	pIrp->IoStatus.Information = 0;
	IoCompleteRequest(pIrp, IO_NO_INCREMENT);

	return status;
}

NTSTATUS ReadComplateRoutine (PDEVICE_OBJECT pDeviceObj, PIRP pIrp)
{
	NTSTATUS status;
	status = STATUS_SUCCESS;
	return status;
}

NTSTATUS WriteComplateRoutine (PDEVICE_OBJECT pDeviceObj, PIRP pIrp)
{
	NTSTATUS status;
	status = STATUS_SUCCESS;
	return status;
}

NTSTATUS CloseComplateRoutine (PDEVICE_OBJECT pDeviceObj, PIRP pIrp)
{
	NTSTATUS status;
	status = STATUS_SUCCESS;
	return status;
}

VOID MyDriverUnoad(PDRIVER_OBJECT pDriverObject)
{
	UNICODE_STRING usSymName;
	RtlInitUnicodeString(&usSymName, L"\\??\\FirstDevice");
	
	if (pDriverObject->DeviceObject != NULL)
	{
		IoDeleteSymbolicLink(&usSymName);
		IoDeleteDevice(pDriverObject->DeviceObject);
		KdPrint(("Delete device success!"));
	}
}



测试程序:
void CTestMyFirstDriverDlg::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here
	
	HANDLE hFile = CreateFile(L"\\??\\FirstDevice", 
								FILE_ALL_ACCESS,
								FILE_SHARE_READ,
								NULL,
								OPEN_EXISTING,
								FILE_ATTRIBUTE_NORMAL,NULL);
	if (hFile == INVALID_HANDLE_VALUE)
	{
		int err = GetLastError();
		MessageBox(L"Create File error!");
	}

	CloseHandle(hFile);

	return;
}

[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!

收藏
免费 0
支持
分享
最新回复 (7)
雪    币: 204
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
测试文件路径好像不能写成\\??\\...
2014-1-18 17:06
0
雪    币: 219
活跃值: (773)
能力值: (RANK:290 )
在线值:
发帖
回帖
粉丝
3
\\\\...\\名字
2014-1-18 18:12
0
雪    币: 468
活跃值: (52)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
驱动层符号链接是对的。
应用层createfile的时候,应该是4个连续的"\"
\\\\.\\FirstDevice
2014-1-19 07:19
0
雪    币: 45
活跃值: (26)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
\\\\   这个代表什么意思 ?
2014-1-20 21:24
0
雪    币: 185
活跃值: (25)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
C语言的转义符你不知道么?
2014-1-20 23:02
0
雪    币: 4
活跃值: (12)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
7
今天才装好了WDK8.1,看到你的程序就拿去测测环境是不是好的,用了你的驱动程序,然后给出了错误提示:
错误        1        error C2220: 警告被视为错误 - 没有生成“object”文件        C:\Users\Administrator\Desktop\C++\MyDriver1\MyDriver1\源.cpp        78        1        MyDriver1
错误        9        error MSB3030: 无法复制文件“C:\Users\Administrator\Desktop\C++\MyDriver1\Win7Debug\MyDriver1.sys”,原因是找不到该文件。        C:\Program Files (x86)\Windows Kits\8.1\build\WindowsDriver.common.targets        1366        5        MyDriver1 Package

求教原因,用的是VS2013,建的空的WDM 驱动解决方案,能告诉我怎么解决吗
2014-4-13 00:40
0
雪    币: 7651
活跃值: (523)
能力值: ( LV9,RANK:610 )
在线值:
发帖
回帖
粉丝
8
4楼正解~
2014-4-13 10:02
0
游客
登录 | 注册 方可回帖
返回
//