http://bbs.driverdevelop.com/read.php?tid-21125.html-------------
我在AddDevice里调用ZwWriteFile想写一点东西到一个文件里(只是一个练习),但是失败了,为什么?我从设备管理器里看到错误代码是31,是什么意思?什么地方有相关的资料?
相关代码:
NTSTATUS AddDevice (IN PDRIVER_OBJECT pDriverObject,IN PDEVICE_OBJECT pPdo)
{
PDEVICE_OBJECT pFdo;
NTSTATUS ntStatus = IoCreateDevice(pDriverObject,
sizeof(DEVICE_EXTENSION),NULL,FILE_DEVICE_UNKNOWN,FILE_DEVICE_SECURE_OPEN,FALSE,&pFdo);
if (!NT_SUCCESS(ntStatus))
{
KdPrint((\"IoCreateDevice failed - %X\\n\",ntStatus));
return ntStatus;
}
PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION)pFdo->DeviceExtension;
pdx->pDriverObject = pDriverObject;
pdx->pFdo = pFdo;
pdx->pPdo = pPdo;
UNICODE_STRING unName;
RtlInitUnicodeString(&unName,L\"\\\\??\\\\D:\\\\Stone\\\\LogFile.txt\");
OBJECT_ATTRIBUTES ObjectrAttributes;
InitializeObjectAttributes(&ObjectrAttributes,&unName,OBJ_OPENIF,NULL,NULL);
IO_STATUS_BLOCK ioStatusBlock;
ntStatus =
ZwCreateFile(&pdx->hLogFileHandle,FILE_WRITE_DATA | FILE_READ_DATA | FILE_APPEND_DATA,&ObjectrAttributes,
&ioStatusBlock,NULL,FILE_ATTRIBUTE_NORMAL,0,FILE_OPEN_IF,FILE_RANDOM_ACCESS,NULL,0);
if (!NT_SUCCESS(ntStatus))
{
KdPrint((\"ZwCreateFile fialed - %X\\n\",ntStatus));
IoDeleteDevice(pFdo);
return ntStatus;
}
ntStatus = IoRegisterDeviceInterface(pPdo,&GUID_TEST,NULL,&pdx->unLinkName);
if (!NT_SUCCESS(ntStatus))
{
KdPrint((\"IoRegisterDeviceInterface failed - %X\\n\",ntStatus));
ZwClose(pdx->hLogFileHandle);
IoDeleteDevice(pFdo);
return ntStatus;
}
TCHAR pContent[] = \"AddDevice is called\";
ntStatus = WriteToLogFile(pdx->hLogFileHandle,pContent,strlen(pContent)*sizeof(TCHAR));
if (!NT_SUCCESS(ntStatus))
{
KdPrint((\"WritetoLogFile failed - %X\\n\",ntStatus));
ZwClose(pdx->hLogFileHandle);
IoDeleteDevice(pFdo);
return ntStatus;
}
pdx->pLowerDeviceObject = IoAttachDeviceToDeviceStack(pFdo,pPdo);
pFdo->Flags &= ~DO_DEVICE_INITIALIZING;
return ntStatus;
}
ZwCreateFile(FileHandle,
GENERIC_WRITE|SYNCHRONIZE|GENERIC_READ,
&ObjectAttributes,
&IoStatusBlock,
0,
FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_DELETE,
FILE_OPEN_IF,
FILE_SYNCHRONOUS_IO_NONALERT,
NULL,
0);