Search code examples
winapimax-path

Why CreateDirectoryEx with long folder name is not working?


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.


Solution

  • 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