我有这个代码,我在 Windows 中没有经验:
#include <windows.h>
#include <stdio.h>
typedef BOOL (WINAPI *P_GDFSE)(LPCTSTR, PULARGE_INTEGER,
PULARGE_INTEGER, PULARGE_INTEGER);
void main (int argc, char **argv)
{
BOOL fResult;
char *pszDrive = NULL,
szDrive[4];
DWORD dwSectPerClust,
dwBytesPerSect,
dwFreeClusters,
dwTotalClusters;
P_GDFSE pGetDiskFreeSpaceEx = NULL;
unsigned __int64 i64FreeBytesToCaller,
i64TotalBytes,
i64FreeBytes;
if (argc != 2)
{
printf ("usage: %s <drive|UNC path>\n", argv[0]);
printf ("\texample: %s C:\\\n", argv[0]);
return;
}
pszDrive = argv[1];
if (pszDrive[1] == ':')
{
szDrive[0] = pszDrive[0];
szDrive[1] = ':';
szDrive[2] = '\\';
szDrive[3] = '\0';
pszDrive = szDrive;
}
// FIRST ERROR kernel32.dll
pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress (
GetModuleHandle ("kernel32.dll"),
"GetDiskFreeSpaceExA");
// SECOND ERROR pszDrive
if (pGetDiskFreeSpaceEx)
{
fResult = pGetDiskFreeSpaceEx (pszDrive,
(PULARGE_INTEGER)&i64FreeBytesToCaller,
(PULARGE_INTEGER)&i64TotalBytes,
(PULARGE_INTEGER)&i64FreeBytes);
if (fResult)
{
printf ("\n\nGetDiskFreeSpaceEx reports\n\n");
printf ("Available space to caller = %I64u MB\n",
i64FreeBytesToCaller / (1024*1024));
printf ("Total space = %I64u MB\n",
i64TotalBytes / (1024*1024));
printf ("Free space on drive = %I64u MB\n",
i64FreeBytes / (1024*1024));
}
}
else
{
// ERROR 3 pszDrive
fResult = GetDiskFreeSpace (pszDrive,
&dwSectPerClust,
&dwBytesPerSect,
&dwFreeClusters,
&dwTotalClusters);
if (fResult)
{
/* force 64-bit math */
i64TotalBytes = (__int64)dwTotalClusters * dwSectPerClust *
dwBytesPerSect;
i64FreeBytes = (__int64)dwFreeClusters * dwSectPerClust *
dwBytesPerSect;
printf ("GetDiskFreeSpace reports\n\n");
printf ("Free space = %I64u MB\n",
i64FreeBytes / (1024*1024));
printf ("Total space = %I64u MB\n",
i64TotalBytes / (1024*1024));
}
}
if (!fResult)
printf ("error: %lu: could not get free space for \"%s\"\n",
GetLastError(), argv[1]);
}
我收到了这些错误(visual studio 2010 终极版):
在 kernel32.dll:
pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress (GetModuleHandle ("kernel32.dll"), "GetDiskFreeSpaceExA");
错误:const char* 类型的参数与“LPCWSTR”类型的参数不兼容
在 pszDrive:
fResult = pGetDiskFreeSpaceEx (pszDrive,
(PULARGE_INTEGER)&i64FreeBytesToCaller,
(PULARGE_INTEGER)&i64TotalBytes,
(PULARGE_INTEGER)&i64FreeBytes);
错误:char* 类型的参数与“LPCTSTR”类型的参数不兼容
在 pszDrive:
fResult = GetDiskFreeSpace (pszDrive,
&dwSectPerClust,
&dwBytesPerSect,
&dwFreeClusters,
&dwTotalClusters);
错误:char* 类型的参数与“LPCWSTR”类型的参数不兼容
多谢