Create a directory and get the descriptor by issuing one IRP

When we create a file using CreateFile, the file is created, and we get a handle.
But CreateDirectory does not return a directory handle.

I also want to get a handle when I create the directory.
I want to handle this by issuing only one I / O request packet.

So, 'Do CreateDirectory, then CreateFile with FILE_FLAG_BACKUP_SEMANTICS.' there will be no answer.
It will produce two Irps files for the file system.

Is there an Api that I can use in Usermode (Win32 Api)?

+4
source share
1 answer

NT can do this, but Win32 does not open it. For this you need to use the NT API. NtCreateFile in particular. It must follow the same ZwCreateFile parameters.

Here is an illustrative example (hacked in a hurry inside a web form - YMMV):

 HANDLE CreateDirectoryAndGetHandle(PWSTR pszFileName) { NTSTATUS Status; UNICODE_STRING FileName; HANDLE DirectoryHandle; IO_STATUS_BLOCK IoStatus; OBJECT_ATTRIBUTES ObjectAttributes; RtlInitUnicodeString(&FileName, pszFileName); InitializeObjectAttributes(&ObjectAtributes, &FileName, 0, NULL, NULL); Status = NtCreateFile(&DirectoryHandle, GENERIC_READ | GENERIC_WRITE, &ObjectAttributes, &IoStatus, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_CREATE, FILE_DIRECTORY_FILE, NULL, 0); if (NT_SUCCESS(Status)) { return DirectoryHandle; } else { SetLastError(RtlNtStatusToDosError(Status)); return INVALID_HANDLE_VALUE; } } 

Some notes ...

  • NT paths have slightly different conventions than Win32 paths ... You may need to sanitize the path.

  • Speaking of HANDLE s, NT APIs usually deal with NULL , not INVALID_HANDLE_VALUE .

  • I did not do this here, but by changing the call to InitializeObjectAttributes , you can do interesting things, for example, create relative to another directory descriptor. Of course, you can also change all the flags that I added here. For best results, refer to the documentation and / or website.

+4
source

Source: https://habr.com/ru/post/1332856/


All Articles