【发布时间】:2015-10-08 14:31:05
【问题描述】:
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]
}
嗯,我在这里错过了一些基本的东西吗?
双反斜杠没有从变量名中解析出来。有没有办法使用与 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);
currPath 的“重新调用”会不会有什么不好的地方?
Edit2:不,尝试使用另一个变量。 CreateDirectoryW之前的cumPath的值是否符合预期?
cumPath = 0x005b4fe8 L"\\\\?\\C:\\我的嵌套路径\\我的嵌套路径\\我的嵌套路径\\我的嵌套路径\\我的嵌套路径\\"
尤里卡!注释掉这一行,函数就起作用了!
//wcscat_s(cumPath, MAX_PATH - 3, __TEXT("\\"));
但现在没有嵌套目录,就像最初的要求一样。
cumPath = 0x00644fe8 L"\\?\C:\我的嵌套路径我的嵌套路径我的嵌套路径我的嵌套路径我的嵌套路径"
【问题讨论】:
-
这一行:
wcscpy_s(cumPath, pathLength, L"\\\\?\\C:\\");正在复制到一个没有特别指向任何地方的指针。结果是未定义的行为。 -
你真正需要的是学习一些调试技巧。首先计算传递给 api 函数的值。
-
哎呀:我需要的是更好的 SO 问题技能。给 listTotal 一个值。添加了两个变量的 callocs。但是,对 callocs 做了一些可能不合法的事情。问题已更新。
标签: c winapi path nested prefix