我正在为输出变量动态分配内存。我在循环中运行函数,函数的输出根据我声明输出变量的位置而不同。我不知道我在哪里弄错了。请指正。 我已经包含了两种方案的代码和输出截图。
情景:1
void __cdecl wmain(void)
{
HANDLE FindHandle = INVALID_HANDLE_VALUE;
WCHAR VolumeName[MAX_PATH] = L"";
DWORD CharCount = MAX_PATH + 1;
FindHandle = FindFirstVolume(VolumeName, ARRAYSIZE(VolumeName));
while(1)
{
WCHAR *vp = (WCHAR*)malloc(CharCount * sizeof(WCHAR));
GetVolumePathNamesForVolumeNameW(VolumeName, vp, CharCount, &CharCount);
wprintf(L"%s\n", vp);
if(!FindNextVolumeW(FindHandle, VolumeName, ARRAYSIZE(VolumeName)))
break;
}
FindVolumeClose(FindHandle);
return;
}
方案:2
void __cdecl wmain(void)
{
HANDLE FindHandle = INVALID_HANDLE_VALUE;
WCHAR VolumeName[MAX_PATH] = L"";
DWORD CharCount = MAX_PATH + 1;
FindHandle = FindFirstVolume(VolumeName, ARRAYSIZE(VolumeName));
WCHAR *vp = (WCHAR*)malloc(CharCount * sizeof(WCHAR));
while(1)
{
GetVolumePathNamesForVolumeNameW(VolumeName, vp, CharCount, &CharCount);
wprintf(L"%s\n", vp);
if(!FindNextVolumeW(FindHandle, VolumeName, ARRAYSIZE(VolumeName)))
break;
}
FindVolumeClose(FindHandle);
return;
}
提前致谢。