Why CreateDirectoryEx with long folder name is not working?

852 Views Asked by At

This is a sample code i have written to check if i am able to create a folder with name length greater than MAX_PATH -

wstring s = L"D:\\Test";
wstring s2 = L"\\?\D:\\datafffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffr700000000000000datafffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffr700000000000000datafffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffr700000000000000datafffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffr700000000000000datafffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffr700000000000000";

int ret = CreateDirectoryEx(s.c_str(), s2.c_str(), NULL);

int error = GetLastError();

It did not work, the returned error is ERROR_PATH_NOT_FOUND. Can anyone please tell me whats the problem in the code?

Note: "D:\Test" folder is an existing folder. I am using Windows 7.

3

There are 3 best solutions below

5
On BEST ANSWER

need not confuse Maximum file name length (path component) and Maximum path length - see Limits

the Maximum file name length is <= 255 Unicode characters for all file systems

and Maximum path length 32,760 Unicode characters with each path component no more than 255 characters


initial error was by using L"\\?\" prefix - really it must be L"\\\\?\\" because c/c++ translate "\\" to \ - but this already only language specific error.

if fix it - must be error ERROR_INVALID_NAME (converted from NTSTATUS STATUS_OBJECT_NAME_INVALID ) because path component which you use more than 255 characters

2
On

Because the path sizes are limited (to 160 caracters I think on W7 but not sure)

3
On

Because the syntax is simply wrong. You have to escape the backslash. So the prefix should be L"\\\\?\\".

wstring s2 = L"\\\\?\\D:\\dataff...";