最近开发驱动遇到一个严重的问题:内核创建设备,正常运行后,应用层CreateFile失败,返回错误码:2。但是如果把应用层重启后,就可以成功打开设备了。好奇怪。。。。不知道什么原因
[C] 纯文本查看 复制代码 //驱动代码:
#define NTDEVICE_NAME_STRING L"\\Device\\myDevice"
#define SYMBOLIC_NAME_STRING L"\\DosDevices\\myDevice"
UNICODE_STRING deviceName;
UNICODE_STRING symbolicLinkName;
PDEVICE_OBJECT deviceObject = NULL;
RtlInitUnicodeString(&deviceName, NTDEVICE_NAME_STRING);
status = IoCreateDevice(
DriverObject,
0,
&deviceName,
FILE_DEVICE_UNKNOWN,
FILE_DEVICE_SECURE_OPEN,
FALSE,
&deviceObject);
if (!NT_SUCCESS(status))
{
return status;
}
RtlInitUnicodeString(&symbolicLinkName, SYMBOLIC_NAME_STRING);
status = IoCreateSymbolicLink(&symbolicLinkName, &deviceName);
if (!NT_SUCCESS(status))
{
IoDeleteDevice(deviceObject);
return status;
}
[Asm] 纯文本查看 复制代码 //应用层代码
#define LINK_NAME_STRING L"\\\\.\\myDevice"
HANDLE hDevice = CreateFileW(
LINK_NAME_STRING,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_SYSTEM,
NULL);
if (INVALID_HANDLE_VALUE == hDevice)
{
return GetLastError();
}
|