+bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree)
+{
+ if ( path.empty() )
+ return FALSE;
+
+// old w32api don't have ULARGE_INTEGER
+#if defined(__WIN32__) && \
+ (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION( 0, 3 ))
+ // GetDiskFreeSpaceEx() is not available under original Win95, check for
+ // it
+ typedef BOOL (WINAPI *GetDiskFreeSpaceEx_t)(LPCTSTR,
+ PULARGE_INTEGER,
+ PULARGE_INTEGER,
+ PULARGE_INTEGER);
+
+ GetDiskFreeSpaceEx_t
+ pGetDiskFreeSpaceEx = (GetDiskFreeSpaceEx_t)::GetProcAddress
+ (
+ ::GetModuleHandle(_T("kernel32.dll")),
+#if wxUSE_UNICODE
+ "GetDiskFreeSpaceExW"
+#else
+ "GetDiskFreeSpaceExA"
+#endif
+ );
+
+ if ( pGetDiskFreeSpaceEx )
+ {
+ ULARGE_INTEGER bytesFree, bytesTotal;
+
+ // may pass the path as is, GetDiskFreeSpaceEx() is smart enough
+ if ( !pGetDiskFreeSpaceEx(path,
+ &bytesFree,
+ &bytesTotal,
+ NULL) )
+ {
+ wxLogLastError(_T("GetDiskFreeSpaceEx"));
+
+ return FALSE;
+ }
+
+ // ULARGE_INTEGER is a union of a 64 bit value and a struct containing
+ // two 32 bit fields which may be or may be not named - try to make it
+ // compile in all cases
+#if defined(__BORLANDC__) && !defined(_ANONYMOUS_STRUCT)
+ #define UL(ul) ul.u
+#else // anon union
+ #define UL(ul) ul
+#endif
+ if ( pTotal )
+ {
+ *pTotal = wxLongLong(UL(bytesTotal).HighPart, UL(bytesTotal).LowPart);
+ }
+
+ if ( pFree )
+ {
+ *pFree = wxLongLong(UL(bytesFree).HighPart, UL(bytesFree).LowPart);
+ }
+ }
+ else
+#endif // Win32
+ {
+ // there's a problem with drives larger than 2GB, GetDiskFreeSpaceEx()
+ // should be used instead - but if it's not available, fall back on
+ // GetDiskFreeSpace() nevertheless...
+
+ DWORD lSectorsPerCluster,
+ lBytesPerSector,
+ lNumberOfFreeClusters,
+ lTotalNumberOfClusters;
+
+ // FIXME: this is wrong, we should extract the root drive from path
+ // instead, but this is the job for wxFileName...
+ if ( !::GetDiskFreeSpace(path,
+ &lSectorsPerCluster,
+ &lBytesPerSector,
+ &lNumberOfFreeClusters,
+ &lTotalNumberOfClusters) )
+ {
+ wxLogLastError(_T("GetDiskFreeSpace"));
+
+ return FALSE;
+ }
+
+ wxLongLong lBytesPerCluster = lSectorsPerCluster;
+ lBytesPerCluster *= lBytesPerSector;
+
+ if ( pTotal )
+ {
+ *pTotal = lBytesPerCluster;
+ *pTotal *= lTotalNumberOfClusters;
+ }
+
+ if ( pFree )
+ {
+ *pFree = lBytesPerCluster;
+ *pFree *= lNumberOfFreeClusters;
+ }
+ }
+
+ return TRUE;
+}
+