1

So we all know that Windows programs by default are limited to dealing with a maximum path length of 260 characters. However, this limit can easily be overcome by prefixing the path by the \\?\ character sequence.

For some reason, however, this isn't possible with relative paths, as MSDN says:

Because you cannot use the \\?\ prefix with a relative path, relative paths are always limited to a total of MAX_PATH characters.

(source)

I don't really understand the reason why Microsoft decided to forbid relative paths to be prefixed with \\?\ so if there is some sort of rationale behind this decision, I'd be really glad to hear about it because it doesn't really make sense to me that \\?\ is only allowed for full paths.

My real question, though, is how to deal with this limitation: Should I simply call GetFullPathName() on relative paths to extend them to full paths, then add the \\?\ prefix, and then pass that path to fopen() etc., or what is the recommended way of dealing with this limitation?

Andreas
  • 9,245
  • 9
  • 49
  • 97
  • 2
    Relative paths is a "feature" that the winapi layer inherited from MS-Dos and earlier operating systems that inspired MS-Dos, like RSX and Unix. Scary quotes appropriate, this has caused an enormous number of bugs. The kernel has no support for it. You invoke kernel path handling with that prefix, avoiding the MAXPATH limitation built into winapi is the benefit. – Hans Passant May 28 '19 at 15:26
  • yes, you can extend path by *GetFullPathName* and then add \\?\ prefix. https://stackoverflow.com/a/38038887/6401656 – RbMm May 28 '19 at 15:48
  • It's really not difficult, surely, to convert a relative path into an absolute one ...... – David Heffernan May 28 '19 at 15:49
  • @DavidHeffernan: Yeah, that's why I was wondering whether there was a more straightforward solution without going the extra mile and call `GetFullPathName()` first but if that's the way it is done, that's fine with me. – Andreas May 28 '19 at 15:52
  • @Andreas - here main problem - relative path from what ? if you know this can simply merge 2 strings and \\?\ prefix – RbMm May 28 '19 at 15:55
  • @RbMm: Yes, I know how to do it, I was just curious if there was a way to avoid `GetFullPathName()`. – Andreas May 28 '19 at 16:01
  • @Andreas at least on Windows 10, you can opt-in to remove `MAX_PATH` restrictions so you don't need to use ``\\?\`` anymore – Remy Lebeau May 28 '19 at 17:34
  • On Windows 8 and up you can also use `PathCchCanonicalizeEx()` to combine the current path with the relative path to get the final path without actually opening any files. – Jonathan Potter May 28 '19 at 21:18

1 Answers1

3

You cannot use the \\?\ prefix with a relative path.

When relative path is passed to the system, it is parsed as absolute paths and then passed to the system. And as it is mentioned in the source:

The 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.

Drake Wu
  • 6,927
  • 1
  • 7
  • 30
  • Thanks for the extra information! It's definitely good to know that using `\\?\` also means that forward slashes and periods/double dots can't be used. – Andreas May 29 '19 at 13:00