为什么我的测试程序测试不成功?
测试程序的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;
}
最新内容请见作者的GitHub页:http://qaseven.github.io/