找不到指定的路径:CreateDirectoryW

找不到指定的路径:CreateDirectoryW,c,winapi,path,nested,prefix,C,Winapi,Path,Nested,Prefix,VS2015,Unicode:获取无法找到指定路径的错误,请在列表框中运行以下代码: 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:

VS2015,Unicode:获取无法找到指定路径的错误,请在列表框中运行以下代码:

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]

}
这两个变量都声明为模块范围。然而,在进入该潜艇之前,有:

 currPath = (wchar_t *)calloc(pathLength, sizeof(wchar_t));
 ...
 free(currPath); 
重新调用currPath会有什么影响吗

Edit2:没有,尝试使用另一个变量。CreateDirectoryW之前的cumPath值是否与预期值相同

cumPath=0x005b4fe8 L\\\\?\\C:\\My嵌套路径\\My嵌套路径\\My嵌套路径\\My嵌套路径\\My嵌套路径\\My嵌套路径\\My嵌套路径\\

尤里卡!注释掉这一行,函数工作了

  //wcscat_s(cumPath, MAX_PATH - 3, __TEXT("\\"));
但是现在没有嵌套目录,这是最初的要求

cumPath=0x00644fe8 L\\?\C:\My嵌套路径My嵌套路径My嵌套路径My嵌套路径My嵌套路径My嵌套路径My嵌套路径My嵌套路径My嵌套路径


第一个问题是cumPath没有指向任何分配的内存

wcscpy_s函数期望目标是一个足够大的字符数组,可以容纳源字节

以下是wcscpy手册页的相关摘录

描述 wcscpy函数是strcpy3的宽字符等价物 作用它复制src指向的宽字符串, 将终止的空宽字符L'\0'包括到数组中 由dest指向

   The strings may not overlap.

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

系统处理C编译的嵌套目录字符串的方式似乎有问题。 C++中没有类似的词,

这里的解决方案是在每次新目录迭代中更改当前目录:

#include <Windows.h>
#include <Strsafe.h>


void ErrorExit(LPCTSTR lpszFunction)
{

    DWORD dww = 0;
    LPVOID lpMsgBuf;
    LPVOID lpDisplayBuf;

    dww = GetLastError();
    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dww,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),  (LPTSTR)&lpMsgBuf,0, NULL);

    lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));


    StringCchPrintf((LPTSTR)lpDisplayBuf, LocalSize(lpDisplayBuf) / sizeof(TCHAR), TEXT("%s failed with error %lu: %s"), lpszFunction, dww, lpMsgBuf);
    MessageBox(NULL, (LPCTSTR)lpDisplayBuf, "Error", MB_OK);
    LocalFree(lpDisplayBuf);
    LocalFree(lpMsgBuf);
}



int main()
{

wchar_t *currPathW, *cumPathW;
int listTotal = 2;
int errorcode;
int pathLength = MAX_PATH - 3;
int stripLen;
currPathW = (wchar_t *)calloc(pathLength, sizeof(wchar_t));
cumPathW = (wchar_t *)calloc(pathLength, sizeof(wchar_t));
wcscpy_s(cumPathW, pathLength, L"\\\\?\\C:\\");

wcscpy_s(currPathW, MAX_PATH - 3, L"NestedPath");
for (int i = 0; i < listTotal; i++) {

    wcscat_s(cumPathW, pathLength, (wchar_t *) currPathW);
    //OK but doubled backslashes
    wcscat_s(cumPathW, MAX_PATH - 3, L"\\");
    //appear in debugger variable contents
    int errorcode = CreateDirectoryW(cumPathW, NULL);
    if (errorcode == 0) ErrorExit("Failed: ");

}
//wchar_t * currPathWtmp = (wchar_t *)calloc(pathLength, sizeof(wchar_t));
wchar_t * currPathWtmp;
currPathWtmp = cumPathW;
stripLen = wcslen(currPathWtmp)- wcslen(L"NestedPath") - 1;

for (int i = 0; i < listTotal; i++) {

    int errorcode = RemoveDirectoryW(currPathWtmp);
    if (errorcode == 0) ErrorExit("Failed: ");
    currPathWtmp [stripLen] = 0; 

}
//currPathWtmp = NULL;


free(currPathW); 
free(cumPathW); 

}
编辑:函数失败,无法在256个字符的屏障上再次找到指定的路径,因此看起来像是跳过了更多的编码环,以使其实际工作。 Edit2:在本例中,将预处理定义从WIN32更改为WIN32 64 AMD64没有任何区别。
Edit3:现在应该立即编译这个。一切正常!实际上,它适用于listTotal的许多值。还有别的事情发生。

这行:wcscpy\u scumPath,pathLength,L\\\?\\C:\\;正在复制到未指向特定位置的指针。结果是未定义的行为。您真正需要的是学习一些调试技巧。首先计算出传递给api函数的值。哎哟:我需要的是更好的解决SO问题的技巧。给listTotal一个值。添加了两个变量的callocs。然而,callocs做了一些不合法的事情。问题已更新。是否在代码中使用MAX_路径是问题所在?这只是一个简单的问题。您自己的路径长度要大得多,警告!随机猜测!在未来十年内可能足够了。这是一个想法:通过将\\放在列表框字符串中来摆脱wscat。但不是,同样的问题+1.为了这个想法:想知道如果资源管理器终止,这是否有效。
#include <Windows.h>
#include <Strsafe.h>


void ErrorExit(LPCTSTR lpszFunction)
{

    DWORD dww = 0;
    LPVOID lpMsgBuf;
    LPVOID lpDisplayBuf;

    dww = GetLastError();
    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dww,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),  (LPTSTR)&lpMsgBuf,0, NULL);

    lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));


    StringCchPrintf((LPTSTR)lpDisplayBuf, LocalSize(lpDisplayBuf) / sizeof(TCHAR), TEXT("%s failed with error %lu: %s"), lpszFunction, dww, lpMsgBuf);
    MessageBox(NULL, (LPCTSTR)lpDisplayBuf, "Error", MB_OK);
    LocalFree(lpDisplayBuf);
    LocalFree(lpMsgBuf);
}



int main()
{

wchar_t *currPathW, *cumPathW;
int listTotal = 2;
int errorcode;
int pathLength = MAX_PATH - 3;
int stripLen;
currPathW = (wchar_t *)calloc(pathLength, sizeof(wchar_t));
cumPathW = (wchar_t *)calloc(pathLength, sizeof(wchar_t));
wcscpy_s(cumPathW, pathLength, L"\\\\?\\C:\\");

wcscpy_s(currPathW, MAX_PATH - 3, L"NestedPath");
for (int i = 0; i < listTotal; i++) {

    wcscat_s(cumPathW, pathLength, (wchar_t *) currPathW);
    //OK but doubled backslashes
    wcscat_s(cumPathW, MAX_PATH - 3, L"\\");
    //appear in debugger variable contents
    int errorcode = CreateDirectoryW(cumPathW, NULL);
    if (errorcode == 0) ErrorExit("Failed: ");

}
//wchar_t * currPathWtmp = (wchar_t *)calloc(pathLength, sizeof(wchar_t));
wchar_t * currPathWtmp;
currPathWtmp = cumPathW;
stripLen = wcslen(currPathWtmp)- wcslen(L"NestedPath") - 1;

for (int i = 0; i < listTotal; i++) {

    int errorcode = RemoveDirectoryW(currPathWtmp);
    if (errorcode == 0) ErrorExit("Failed: ");
    currPathWtmp [stripLen] = 0; 

}
//currPathWtmp = NULL;


free(currPathW); 
free(cumPathW); 

}