找不到指定的路径:CreateDirectoryW

时间:2015-10-08 14:31:05

标签: 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:\\"); //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 ,我错过了一些基本的东西吗?
双重反斜杠不会从变量名解析出来。有没有办法使用逐字或&#34;&#34;&#34;来构建宏。与TEXT或L一起使用的前缀?

Edit1 代码前面有以下两行:

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

这两个变量都是模块化的。 然而,在进入此子目录之前有:

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

&#34; re-calloc&#34; currPath有什么不安?

Edit2 :不,尝试使用其他变量。 CreateDirectoryW之前​​的cumPath值是否符合预期?

  

cumPath = 0x005b4fe8 L&#34; \\\\?\\ C:\\ My Nested Path \\ My Nested Path \\ My Nested Path \\ My Nested Path \\ My Nested Path \\&#34;

尤里卡!评论这一行,功能有效!

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

但现在没有嵌套目录,原始要求也是如此。

  

cumPath = 0x00644fe8 L&#34; \\?\ C:\我的嵌套PathMy嵌套PathMy嵌套PathMy嵌套PathMy嵌套路径&#34;

2 个答案:

答案 0 :(得分:1)

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

并且wcscpy_s()函数期望目标是一个足够大的char数组来保存源字节

这是wcscpy()手册页

的相关摘录

说明        wcscpy()函数是strcpy(3)的宽字符等价物        功能。它复制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.  

答案 1 :(得分:1)

系统处理C编译的嵌套目录字符串的方式似乎有所破坏。 C ++中没有类似的problem

此处的解决方案是使用每个新目录迭代更改当前目录:

#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); 

}

修改:功能失败,&#34;无法找到指定的路径&#34;再次在256个字符的屏障上,所以它看起来像跳了几个编码箍,让它实际上像宣传的那样工作 Edit2 :将预处理器定义从_WIN32更改为WIN64(本例中为_AMD64)并没有任何区别。
Edit3 :现在应该开箱即用。一切正常!实际上它适用于listTotal的许多值。还有别的事情发生了。