【发布时间】:2011-07-29 01:50:54
【问题描述】:
我的动态分配变量用 SecureZeroMemory 修整,然后 ReadFile 用一个短的 5char 字符串和一堆剩余的方块填充它。问题是字符串末尾的垃圾字符:
“电机驱动器”
ReadFile 的 lpNumberOfBytesRead 参数显示字符串是 10 个字符,可能是因为它是 Unicode?p>
有人可以帮我看看如何删除这些尾随的垃圾字符吗?有没有像 ZeroMemory 这样的函数来清除它们?
TCHAR *sIncoming;
sIncoming = (TCHAR *) malloc(sizeof(TCHAR) * 4096 + sizeof(TCHAR));
RtlZeroMemory(sIncoming ,sizeof(sIncoming));
// (a string array with no characters in it: "")
bSuccess = ReadFile(hPipe,sIncoming ,BUFSIZE*sizeof(TCHAR),&dwBytesRead,NULL);
// Now the string array has the incoming string plus extra characters in it:
// "motor췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍"
free(sIncoming);
谢谢!
【问题讨论】:
-
在对 malloc 的调用中也使用 BUFSIZE,而不仅仅是在对 ReadFile 的调用中。