91

I found a reference to a file in a log that had the following format:

\\?\C:\Path\path\file.log

I cannot find a reference to what the sequence of \?\ means. I believe the part between the backslashes refers to a hostname.

For instance, on my Windows computer, the following works just fine:

dir \\?\C:\

and also, just fine with same result:

dir \\.\C:\

Questions:

  1. Is there a reference to what the question mark means in this particular path format?
  2. What might generate a file path in such a format?
bjjer
  • 983
  • 1
  • 7
  • 7

2 Answers2

87

A long read, but worth reading if you are in this domain: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx

Extract:

The Windows API has many functions that also have Unicode versions to permit an extended-length path for a maximum total path length of 32,767 characters. This type of path is composed of components separated by backslashes, each up to the value returned in the lpMaximumComponentLength parameter of the GetVolumeInformation function (this value is commonly 255 characters). To specify an extended-length path, use the "\\?\" prefix. For example, "\\?\D:\very long path".

and:

The "\\?\" prefix can also be used with paths constructed according to the universal naming convention (UNC). To specify such a path using UNC, use the "\\?\UNC\" prefix. For example, "\\?\UNC\server\share", where "server" is the name of the computer and "share" is the name of the shared folder. These prefixes are not used as part of the path itself. They indicate that the path should be passed to the system with minimal modification, which means that you cannot use forward slashes to represent path separators, or a period to represent the current directory, or double dots to represent the parent directory. Because you cannot use the "\\?\" prefix with a relative path, relative paths are always limited to a total of MAX_PATH characters.

wovano
  • 4,543
  • 5
  • 22
  • 49
Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
  • 21
    I've known this for a while, but reading this today makes me wonder what Microsoft's rationale is for needing the special prefix to indicate "let me give you a path that's longer than `MAX_PATH` characters". Why wouldn't the fact that I passed in a path with a length that's longer than `MAX_PATH` characters be a good enough indication? I may have to post that as a question on SO... – Michael Burr Jan 17 '14 at 19:57
  • 1
    @MichaelBurr we are using cmake@windows (while building it generates long pathnames, in lots of deep directories), and in my windows command line experience if by any chance a generated path for a file is longer than `MAX_PATH` the system it will refuse to cooperate ... maybe for this cases you need to prepend them with `\\?\\` – Ferenc Deak Jan 17 '14 at 20:01
  • 3
    Oh - I understand that the Windows API doesn't like paths longer than `MAX_PATH` unless it has the special prefix - I just wonder why they didn't just fix the APIs to be OK with longer paths without the prefix. I think it has something to do with the ANSI versions of the APIs needing to optimize how buffers are handled for converting to the Unicode, but I don't really remember. It just seems unnecessary. – Michael Burr Jan 17 '14 at 20:08
  • 4
    Because programs have only allocated 260 characters of storage. This would cause problems if Windows returns names longer than this. Window's rules come from history. The file system doesn't adhere to those rules and can work with Unix conventions (although Window's programs won't be able to access) or anything. –  Jun 08 '15 at 01:10
  • 30
    @MichaelBurr: Fritzone forgot to quote the most relevant sentence from the [documentation](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247.aspx): *"For file I/O, the '\\?\' prefix to a path string tells the Windows APIs to disable all string parsing and to send the string that follows it straight to the file system."* Parsing includes translating forward slashes to backslashes, or interpreting the `.` and `..` pseudo directories. The relaxed path length restrictions are merely a side effect (albeit the one for which the prefix is most commonly used). – IInspectable Nov 16 '16 at 17:54
  • 6
    Late comment, but another reason that I haven't seen mentioned for using this notation is to access disks that don't have drive letters assigned to them using their Volume ID instead. – kilkenny Dec 06 '17 at 15:22
  • another late reply the reason for having two syntax is for backward compatibility to make the API function in the same behavior it used to do before the UNC come in play. – Muhammad May 22 '21 at 09:54
  • 2
    Does anyone know if there is a name for this `"\\?\"` sequence? – Gershom Maes Sep 12 '22 at 16:55
  • Thanks, saved my bacon! – lalilulelo Aug 02 '23 at 22:22
19

The Windows API parses input strings for file I/O. Among other things, it translates / to \ as part of converting the name to an NT-style name, or interpreting the . and .. pseudo directories. With few exceptions, the Windows API also limits path names to 260 characters.

The documented purpose of the \\?\ prefix is:

For file I/O, the "\\?\" prefix to a path string tells the Windows APIs to disable all string parsing and to send the string that follows it straight to the file system.

Among other things, this allows using otherwise reserved symbols in path names (such as . or ..). Opting out of any translations, the system no longer has to maintain an internal buffer, and the arbitrary limit of 260 characters can also be lifted (as long as the underlying filesystem supports it). Note, that this is not the purpose of the \\?\ prefix, rather than a corollary, even if the prefix is primarily used for its corollary.

IInspectable
  • 46,945
  • 8
  • 85
  • 181
  • Why don't you just edit the other answer to add this part? – David Balažic May 28 '22 at 14:22
  • @dav I cannot edit the other proposed answer without making it read something completely different. That answer assumes that relaxing the path limit were the sole reason to have this particular prefix, and completely misses the *actual* purpose of the prefix. I should probably update this answer to include that relaxing the path limit is just a coincidental corollary. – IInspectable May 28 '22 at 16:43