R
royzlife
Hi,
I am trying to incorporate a code snippet which would calculate the
free disk space of any drive passed to it as argument but presently
stuck with it.
Can anyone help me out with sample code?
I have already tried with the follwoing:
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;
/*
Command line parsing.
If the drive is a drive letter and not a UNC path, append a
trailing backslash to the drive letter and colon. This is
required on Windows 95 and 98.
*/
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;
}
/*
Use GetDiskFreeSpaceEx if available; otherwise, use
GetDiskFreeSpace.
Note: Since GetDiskFreeSpaceEx is not in Windows 95 Retail,
we
dynamically link to it and only call it if it is present.
We
don't need to call LoadLibrary on KERNEL32.DLL because it is
already loaded into every Win32 process's address space.
*/
pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress ( ****************
GetModuleHandle ("kernel32.dll"),
"GetDiskFreeSpaceExA");
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
{
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]);
}
But this code works fine when i run it separately but when incorporate
the same in my code, its giving 3 warnings:
'function' : incompatible types - from 'char *' to 'const unsigned
short *' at the locations where i have given '*' marks
and the output comes from the "else" block and free space is shown as
0.
Please help me out!!!
I am trying to incorporate a code snippet which would calculate the
free disk space of any drive passed to it as argument but presently
stuck with it.
Can anyone help me out with sample code?
I have already tried with the follwoing:
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;
/*
Command line parsing.
If the drive is a drive letter and not a UNC path, append a
trailing backslash to the drive letter and colon. This is
required on Windows 95 and 98.
*/
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;
}
/*
Use GetDiskFreeSpaceEx if available; otherwise, use
GetDiskFreeSpace.
Note: Since GetDiskFreeSpaceEx is not in Windows 95 Retail,
we
dynamically link to it and only call it if it is present.
We
don't need to call LoadLibrary on KERNEL32.DLL because it is
already loaded into every Win32 process's address space.
*/
pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress ( ****************
GetModuleHandle ("kernel32.dll"),
"GetDiskFreeSpaceExA");
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
{
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]);
}
But this code works fine when i run it separately but when incorporate
the same in my code, its giving 3 warnings:
'function' : incompatible types - from 'char *' to 'const unsigned
short *' at the locations where i have given '*' marks
and the output comes from the "else" block and free space is shown as
0.
Please help me out!!!