阅读:1879回复:8
请教高手ZwCreateFile的问题(附代码)
我想在我的驱动里创建d:\test.tmp这样一个文件,写了下面的代码,在XP下使用正常,但在2K下,如果文件不存在,则能创建成功,如果原来存在这个文件,就创建不成功,提示STATUS_ACCESS_DENIED,我想使用ZwDeleteFile先把文件删了,再创建,但发现2K不支持此函数。请教各位老大,这是何原因,如何解决。
NTSTATUS status; OBJECT_ATTRIBUTES oa; UNICODE_STRING usname; IO_STATUS_BLOCK iostatus; WCHAR wcFileName[MAX_PATH]; HANDLE hCacheFile = NULL; swprintf(wcFileName,L"\\??\\d:\\test.tmp"); RtlInitUnicodeString(&usname, wcFileName); InitializeObjectAttributes(&oa, &usname, OBJ_CASE_INSENSITIVE|OBJ_KERNEL_HANDLE , NULL, NULL); status = ZwCreateFile(&hCacheFile, GENERIC_ALL, &oa, &iostatus, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_WRITE|FILE_SHARE_READ|FILE_SHARE_DELETE, FILE_OVERWRITE_IF, FILE_SYNCHRONOUS_IO_NONALERT|FILE_NO_INTERMEDIATE_BUFFERING, NULL, 0); if(!NT_SUCCESS(status)) { //STATUS_INVALID_PARAMETER DbgPrint("\n ZwCreateFile=%d,iostatus=%d",status,iostatus.Information); return FALSE; } |
|
沙发#
发布于:2008-11-24 19:16
调用自己实现的NtDeleteFile:
typedef struct _OPEN_PACKET { CSHORT Type; CSHORT Size; PFILE_OBJECT FileObject; NTSTATUS FinalStatus; ULONG_PTR Information; ULONG ParseCheck; PFILE_OBJECT RelatedFileObject; LARGE_INTEGER AllocationSize; ULONG CreateOptions; USHORT FileAttributes; USHORT ShareAccess; PVOID EaBuffer; ULONG EaLength; ULONG Options; ULONG Disposition; PFILE_BASIC_INFORMATION BasicInformation; PFILE_NETWORK_OPEN_INFORMATION NetworkInformation; CREATE_FILE_TYPE CreateFileType; PVOID ExtraCreateParameters; BOOLEAN Override; BOOLEAN QueryOnly; BOOLEAN DeleteOnly; BOOLEAN FullAttributes; PVOID LocalFileObject; BOOLEAN TraversedMountPoint; ULONG InternalFlags; PDEVICE_OBJECT TopDeviceObjectHint; } OPEN_PACKET, *POPEN_PACKET; #define OPEN_PACKET_PATTERN 0xbeaa0251x NTSTATUS MyZwDeleteFile ( POBJECT_ATTRIBUTES ObjectAttributes ) { NTSTATUS status; OPEN_PACKET openPacket; PVOID localFileObject; HANDLE handle; RtlZeroMemory( &openPacket, sizeof( OPEN_PACKET ) ); openPacket.Type = IO_TYPE_OPEN_PACKET; openPacket.Size = sizeof( OPEN_PACKET ); openPacket.CreateOptions = FILE_DELETE_ON_CLOSE; openPacket.ShareAccess = (USHORT) FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; openPacket.Disposition = FILE_OPEN; openPacket.DeleteOnly = TRUE; openPacket.TraversedMountPoint = FALSE; openPacket.LocalFileObject = &localFileObject; status = ObOpenObjectByName( ObjectAttributes, (POBJECT_TYPE) NULL, KernelMode, NULL, DELETE, &openPacket, &handle ); if (openPacket.ParseCheck != OPEN_PACKET_PATTERN) { return status; } else { return openPacket.FinalStatus; } } |
|
|
板凳#
发布于:2008-11-24 19:21
另外,一楼的也可以用FILE_SUPERSEDE来解决
delete file也可以先open,然后set dispostion information~ |
|
|
地板#
发布于:2008-11-25 10:15
感谢WQXNETQIQI提供的帮助。我发现我出现问题的分区是FAT32分区,如果是NTFS分区则无此情况。
我使用了WQXNETQIQI提供的MyZwDeleteFile函数,发现返回值是STATUS_OBJECT_TYPE_MISMATCH,使用FILE_SUPERSEDE参数,还是提示STATUS_ACCESS_DENIED,使用先open,然后set dispostion information,但在open时就提示STATUS_ACCESS_DENIED。请教老大们,这个大概是由何原因导致,我该如何解决? |
|
地下室#
发布于:2008-11-25 10:32
ZwCreateFile第二个参数DesiredAccess不要用GENERIC_ALL,用FILE_ALL_ACCESS,试试看。fastfat32对GENERIC_XXX的参数的处理,在某些平台上有问题。
|
|
5楼#
发布于:2008-11-25 11:26
用FILE_ALL_ACCESS试了,还是老样子。
|
|
6楼#
发布于:2008-11-26 09:21
是不是文件被其它程序独占打开了
|
|
7楼#
发布于:2008-11-26 14:19
没有,在应用层可以对文件进行操作,如删除,打开等操作。
|
|
8楼#
发布于:2008-12-01 11:29
回复
问题解决,参数不能用GENERIC_ALL和FILE_ALL_ACCESS,要用类似GENERIC_READ|GENERIC_WRITE的参数。谢谢楼上几位大虾的帮助。 |
|