Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

c - Could Not Find Path Specified: CreateDirectoryW

VS2015, Unicode: Getting the "Could Not Find the Path Specified" error running the following code in a listbox:

wchar_t *currPath, *cumPath;
int listTotal = 5;
int pathLength = 32760;
listTotal = SendMessageW(hList, LB_GETCOUNT, 0, 0);
wcscpy_s(cumPath, pathLength, L"\\?\C:");
//wcscpy_s(cumPath, pathLength, L"C:"); //Tried this, no difference
wcscpy_s(currPath, MAX_PATH - 3, L"");
for (int i = 0; i < listTotal; i++) {
    SendMessageW(hList, LB_GETTEXT, i, (LPARAM) currPath); //"My Nested Path" picked up from textbox OK
    wcscat_s(cumPath, pathLength, (wchar_t *) currPath);
    \OK but doubled backslashes
    wcscat_s(cumPath, MAX_PATH - 3, __TEXT(""));
    \appear in debugger variable contents
}
int errorcode = CreateDirectoryW(cumPath, NULL);
if (errorcode == 0) {
    ErrorExit(TEXT("CreateDirectoryW"));
    //GetLastError courtesy [MSDN][1]

}

Meh, have I missed something fundamental here?
The double backslash is not parsed out of the variable name. Is there a way to construct a macro using a verbatim or "as is" prefix that works in conjunction with TEXT or L?

Edit1 The following two lines preceded the code:

currPath = (wchar_t *)calloc(pathLength, sizeof(wchar_t));
cumPath = (wchar_t *)calloc(pathLength, sizeof(wchar_t));

Both these vars are declared module wide. However, prior to entry on this sub there was:

 currPath = (wchar_t *)calloc(pathLength, sizeof(wchar_t));
 ...
 free(currPath); 

Would the "re-calloc" of currPath have upset anything?

Edit2: No, tried with another variable. The value of cumPath before CreateDirectoryW is as expected?

cumPath = 0x005b4fe8 L"\\?\C:\My Nested Path\My Nested Path\My Nested Path\My Nested Path\My Nested Path"

Eureka! commenting out this line, the function worked!

  //wcscat_s(cumPath, MAX_PATH - 3, __TEXT(""));

But now there are no nested directories, as was the original requirement.

cumPath = 0x00644fe8 L"\?C:My Nested PathMy Nested PathMy Nested PathMy Nested PathMy Nested Path"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

the first problem is that cumPath is not pointed to any allocated memory.

and the wcscpy_s() function expects the destination to be a char array large enough to hold the source bytes

here is a relevant extract from the wcscpy() man page

DESCRIPTION The wcscpy() function is the wide-character equivalent of the strcpy(3) function. It copies the wide-character string pointed to by src, including the terminating null wide character (L''), to the array pointed to by dest.

   The strings may not overlap.

   The  programmer  must  ensure  that  there  is  room   for   at   least
   wcslen(src)+1 wide characters at dest.  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...