借花献佛,给你贴上一个ReactOS的SetFileAttributesW实现。
- BOOL STDCALL
- SetFileAttributesW(LPCWSTR lpFileName,
- DWORD dwFileAttributes)
- {
- FILE_BASIC_INFORMATION FileInformation;
- OBJECT_ATTRIBUTES ObjectAttributes;
- IO_STATUS_BLOCK IoStatusBlock;
- UNICODE_STRING FileName;
- HANDLE FileHandle;
- NTSTATUS Status;
- DPRINT ("SetFileAttributeW(%S, 0x%lx) called\n", lpFileName, dwFileAttributes);
- /* Validate and translate the filename */
- if (!RtlDosPathNameToNtPathName_U (lpFileName,
- &FileName,
- NULL,
- NULL))
- {
- DPRINT ("Invalid path\n");
- SetLastError (ERROR_BAD_PATHNAME);
- return FALSE;
- }
- DPRINT ("FileName: \'%wZ\'\n", &FileName);
- /* build the object attributes */
- InitializeObjectAttributes (&ObjectAttributes,
- &FileName,
- OBJ_CASE_INSENSITIVE,
- NULL,
- NULL);
- /* Open the file */
- Status = NtOpenFile (&FileHandle,
- SYNCHRONIZE | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
- &ObjectAttributes,
- &IoStatusBlock,
- FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
- FILE_SYNCHRONOUS_IO_NONALERT);
- RtlFreeUnicodeString (&FileName);
- if (!NT_SUCCESS (Status))
- {
- DPRINT ("NtOpenFile() failed (Status %lx)\n", Status);
- SetLastErrorByStatus (Status);
- return FALSE;
- }
- Status = NtQueryInformationFile(FileHandle,
- &IoStatusBlock,
- &FileInformation,
- sizeof(FILE_BASIC_INFORMATION),
- FileBasicInformation);
- if (!NT_SUCCESS(Status))
- {
- DPRINT ("SetFileAttributes NtQueryInformationFile failed with status 0x%08x\n", Status);
- NtClose (FileHandle);
- SetLastErrorByStatus (Status);
- return FALSE;
- }
- FileInformation.FileAttributes = dwFileAttributes;
- Status = NtSetInformationFile(FileHandle,
- &IoStatusBlock,
- &FileInformation,
- sizeof(FILE_BASIC_INFORMATION),
- FileBasicInformation);
- NtClose (FileHandle);
- if (!NT_SUCCESS(Status))
- {
- DPRINT ("SetFileAttributes NtSetInformationFile failed with status 0x%08x\n", Status);
- SetLastErrorByStatus (Status);
- return FALSE;
- }
- return TRUE;
- }