#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <string.h>
DWORD main()
{
DWORD dwPrimaryLength = MAX_PATH ;
DWORD dwNewLength = 0;
LPTSTR lpDriveStrings = (LPTSTR) malloc(sizeof(TCHAR)*dwPrimaryLength);
dwNewLength = GetLogicalDriveStrings(dwPrimaryLength,lpDriveStrings);
_tprintf(TEXT("%d\n"),dwNewLength);
if (dwNewLength = 0)
{
return 1;
}
int index = 1 ;
while(dwNewLength > MAX_PATH)
{
free(lpDriveStrings);
printf("%d\n",index);
lpDriveStrings = (LPTSTR)malloc(sizeof(TCHAR)* dwNewLength);
_tprintf(TEXT("%d\n"),dwNewLength);
dwNewLength = GetLogicalDriveStrings(dwNewLength,lpDriveStrings) ;
index++;
}
_tprintf(TEXT("%s,%d,%d\n"),lpDriveStrings,dwNewLength,_tcslen(lpDriveStrings));
puts(lpDriveStrings);
puts(TEXT("c:\,d:\,\0,123"));
return 0;
}
将以上代码分段注释后,我才发现是错在哪儿了,原来是if语句中
的if (dwNewLength = 0),竟然犯了这么严重的错误,太可怕了。