问题描述
我有这个
WCHAR 文件名[1];
WCHAR fileName[1];
作为函数的返回值(它是一个 sys 32 函数,因此我无法更改返回的类型).我需要将 fileName 设置为空终止,因此我试图将 '\0' 附加到它,但似乎没有任何效果.
as a returned value from a function (it's a sys 32 function so I am not able to change the returned type). I need to make fileName to be null terminated so I am trying to append '\0' to it, but nothing seems to work.
一旦我得到一个以空字符结尾的 WCHAR,我就需要将它传递给另一个 sys 32 函数,因此我需要它保持为 WCHAR.
Once I get a null terminated WCHAR I will need to pass it to another sys 32 function so I need it to stay as WCHAR.
有人能给我任何建议吗?
Could anyone give me any suggestion please?
=================================================
================================================
非常感谢您的帮助.看起来我的问题不仅仅是缺少一个空终止的字符串.
Thanks a lot for all your help. Looks like my problem has to do with more than missing a null terminated string.
//这有效:
WCHAR szPath1[50] = L"\\Invalid2.txt.txt";
dwResult = FbwfCommitFile(szDrive, pPath1); //Successful
//这不会:
std::wstring l_fn(L"\\");
//Because Cache_detail->fileName is \Invalid2.txt.txt and I need two
l_fn.append(Cache_detail->fileName);
l_fn += L""; //To ensure null terminated
fprintf(output, "l_fn.c_str: %ls\n", l_fn.c_str()); //Prints "\\Invalid2.txt.txt"
iCommitErr = FbwfCommitFile(L"C:", (WCHAR*)l_fn.c_str()); //Unsuccessful
//然后当我对这两个进行比较时,它们是不相等的.
//Then when I do a comparison on these two they are unequal.
int iCompareResult = l_fn.compare(pPath1); // returns -1
所以我需要弄清楚这两者最终是如何不同的.
So I need to figure out how these two ended up to be different.
非常感谢!
推荐答案
既然你在评论中提到了 fbwffindfirst/fbwffindnext,你就是在谈论 FbwfCacheDetail.因此,从 fileNameLength 字段中您可以知道 fileName 的长度(以字节为单位).WCHAR 中 fileName 的长度是 fileNameLength/sizeof(WCHAR).所以简单的答案是你可以设置
Since you mentioned fbwffindfirst/fbwffindnext in a comment, you're talking about the file name returned in FbwfCacheDetail. So from the fileNameLength field you know length for the fileName in bytes. The length of fileName in WCHAR's is fileNameLength/sizeof(WCHAR). So the simple answer is that you can set
fileName[fileNameLength/sizeof(WCHAR)+1] = L'\0'
现在这是重要,您需要确保您为缓存细节参数发送到 fbwffindfirst/fbwffindnext 的缓冲区比您需要的 sizeof(WCHAR) 字节大,上面的代码片段可能会在外部运行你的数组的边界.所以对于fbwffindfirst/fbwffindnext的size参数传入缓冲区大小——sizeof(WCHAR).
Now this is important you need to make sure that the buffer you send for the cacheDetail parameter into fbwffindfirst/fbwffindnext is sizeof(WCHAR) bytes larger than you need, the above code snippet may run outside the bounds of your array. So for the size parameter of fbwffindfirst/fbwffindnext pass in the buffer size - sizeof(WCHAR).
例如这个:
// *** Caution: This example has no error checking, nor has it been compiled ***
ULONG error;
ULONG size;
FbwfCacheDetail *cacheDetail;
// Make an intial call to find how big of a buffer we need
size = 0;
error = FbwfFindFirst(volume, NULL, &size);
if (error == ERROR_MORE_DATA) {
// Allocate more than we need
cacheDetail = (FbwfCacheDetail*)malloc(size + sizeof(WCHAR));
// Don't tell this call about the bytes we allocated for the null
error = FbwfFindFirstFile(volume, cacheDetail, &size);
cacheDetail->fileName[cacheDetail->fileNameLength/sizeof(WCHAR)+1] = L"\0";
// ... Use fileName as a null terminated string ...
// Have to free what we allocate
free(cacheDetail);
}
当然,您必须对代码进行大量更改以适应您的代码(此外,您还必须调用 fbwffindnext)
Of course you'll have to change a good bit to fit in with your code (plus you'll have to call fbwffindnext as well)
如果您对 FbwfCacheDetail 结构为何以 WCHAR[1] 字段结尾感兴趣,请参阅此 博客文章.这是 Windows API 中非常常见的模式.
If you are interested in why the FbwfCacheDetail struct ends with a WCHAR[1] field, see this blog post. It's a pretty common pattern in the Windows API.
这篇关于使 WCHAR 空终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!