Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Windows VS2010
Hi,
 
I am trying to save a file whose path exceeds MAX_PATH (about 255) characters. I read that putting '\\?\' at the start of the path and using CreateFileW would work. However, this seems to work when the file path is short, which wouldn't be a problem without the extra '\\?\':
LPCWSTR path = "\\\\?\\C:\\test\testFile.txt";
Handle h = CreateFileW(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
CloseHandle(h);
 
However, when I changed the filename to a really long name so that the total path exceeded MAX_PATH, and I still had the \\?\ at the start, the file was not created!!! The handle was invalid and the last error code is 6 which is ERROR_INVALID_HANDLE.
 
So it seems that the work-around does not work. Does anyone know why?
 
Also, if I ever got this to work, is there a similar way to access the contents of a file whose path exceeds MAX_PATH?
 
I have thought about using symbolic links but would this CreateFileW method of it works as it seems less messy.
 
Many thanks for taking the time to read this Smile | :)
Posted 16-Jan-13 0:54am
Edited 16-Jan-13 1:37am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
Maybe this article can lead you to a solution.
Hope this helps.
  Permalink  
Comments
Jackie Lloyd at 16-Jan-13 10:13am
   
Thank you - this is very useful but doesn't get round my problem. I appreciate your input.
phil.o at 16-Jan-13 10:54am
   
Sorry, I'm not good at all with C++, but since I've read this article on yesterday I thought maybe you could get something useful out of it.
Jackie Lloyd at 16-Jan-13 11:04am
   
Your suggestion was very usefu, but as I have since found there is no solution for what I want to do, it just isn't possible on NTFS. I will keep th elink to this for future :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

There is a double drive specification in your path. Change it to:
LPCWSTR path = "\\\\?\\C:test\testFile.txt";
 
Then it should work. Once the file has been opened, you are accessing it by a handle and can read and write as usual.
 
But you should avoid using long file / path names if possible. They are not supported by the MFC, C-runtime functions (e.g. fopen(), chdir(), rename()), and Shell functions (those beginning with 'SH' like SHBrowseForFolder()).
 
When creating files, you must also check if the file system on the destination drive or share supports long file names.
 
Consider also that most applications don't support long names so that such files can't be opened by those.
 
[UPDATE]
By the comments it has been found the error occurs by using path components that exceed the component limit. The limit can be determined with the lpMaximumComponentLength parameter of the GetVolumeInformation() function.
 
See also the Naming Files, Paths, and Namespaces[^] MSDN article.
  Permalink  
v2
Comments
Jackie Lloyd at 16-Jan-13 7:41am
   
Hello Jochen,
I am so sorry, I mistyped that line of code in the here, I couldn't copy and paste as its on a different machine. It was as you say, so that isn't the problem. My path really is path = L"\\\\?\\C:\\test\\testFile.txt" which works, it just doesn't work if I change testFile.txt to a really long name.
 
If the file is a .png, are you saying that I will not be able to do so if the file path is greater than 255?
 
Many thanks.
Jochen Arndt at 16-Jan-13 7:56am
   
I just tested it here and it works (CreateFile() creates a file with a total path length of 280 characters). To get the error, call GetLastError() when CreateFile() returns INVALID_HANDLE_VALUE.
 
Possible error sources are:
The file system on the destination does not support long names (FAT is not supported, NTFS is).
The path does not exist yet. The path to the file must exist already.
Jackie Lloyd at 16-Jan-13 9:33am
   
Thank you, I realised from your comment that mine would probably work if the entire path exceeds 255 but not (as I had been trying) if any one component in the (a folder or the file name) exceeds 255. This seems to be the case. So I guess there is no way to save files with a name that exceeds 255 or within a folder that exceeds 255.
Jochen Arndt at 16-Jan-13 9:44am
   
You are right. There is also a limit for the components. See my updated answer with MSDN link.
Jackie Lloyd at 16-Jan-13 10:15am
   
I have looked and couldn't see any way to exceed the 25 limit for each part of the path - is that what you think?
Jochen Arndt at 16-Jan-13 10:25am
   
Yes. The limit is set by the file system. The common Windows file systems support a maximum of 255 characters per component. See also http://msdn.microsoft.com/en-us/library/windows/desktop/ee681827%28v=vs.85%29.aspx.
Jackie Lloyd at 16-Jan-13 10:33am
   
Thank you, that makes it very clear.
I have found this very interesting, even though I have found that there is no solution to my problem :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 428
1 Sergey Alexandrovich Kryukov 253
2 PhilLenoir 115
3 RajeeshMenoth 110
4 Peter Leow 100
0 Sergey Alexandrovich Kryukov 6,465
1 OriginalGriff 5,926
2 CPallini 2,473
3 Richard MacCutchan 1,687
4 Abhinav S 1,540


Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100