7

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?

xmllmx
  • 39,765
  • 26
  • 162
  • 323

2 Answers2

8

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.

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
  • Why does this work? C: isn't defined in the device namespace. Surely you'd have to use `L"\\??\\C:"` – Lewis Kelsey Apr 21 '20 at 16:55
  • @Lewis, I've never found any documentation that clearly explains the way the device namespace works, but see [this answer](https://superuser.com/a/884359/96662) on Super User. C: is an alias in `GLOBAL??` as can be confirmed using WinObj. – Harry Johnston Apr 21 '20 at 21:12
  • that's interesting, maybe it searches both global and the device namespace. I will have to look at the source code of the object manager function that does it I think – Lewis Kelsey Apr 21 '20 at 21:37
1

Try dropping the trailing slash:

L"\\\\.\\C:"
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • I think you misconstrued what I mean. I know it will be OK if the trailing backslash is dropped. However, although the documentation explicitly declares the trailing backslash is a valid argument, CreateFile always returns an error. That's the key point of my question. – xmllmx Jan 08 '13 at 04:08