Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open the file system of a volume in CreateFile?

The MSDN page on CreateFile says: The string "\\.\C:\" can be used to open the file system of the C: volume. However, the following code always returns an error : ERROR_PATH_NOT_FOUND.

HANDLE h = CreateFile(L"\\\\.\\C:\\", FILE_READ_ATTRIBUTES, 
    FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, 0, OPEN_EXISTING, 0, 0);

How should I pass the arguments correctly?

like image 572
xmllmx Avatar asked Jan 08 '13 03:01

xmllmx


1 Answers

If you wanted a volume handle (for use with I/O control codes) you would have needed to drop the trailing slash.

In order to get a handle to the root directory, you need to keep the trailing slash and pass the FILE_FLAG_BACKUP_SEMANTICS flag in the dwFlagsAndAttributes argument. This is documented on the MSDN page under the heading "Directories". For example, this is what you want to do if you're planning to call GetFileInformationByHandle or GetFileInformationByHandleEx.

Ordinarily, however, you wouldn't open a handle to the root directory in order to list the files. Instead, you would use FindFirstFile/FindNextFile or one of the related functions.

like image 64
Harry Johnston Avatar answered Nov 14 '22 23:11

Harry Johnston