Mensaje    Asunto: GET_DISK_SPACE y GET_WIN_VERSION Responder citando

Info detallada en: http://www.neurophys.wisc.edu/ravi/software/diskspace/

Código:
//  Exam50.cpp
//  Created: 3/20/2001  (rk)
//  Last modified: 4/25/2001  (rk)
//  Use GetDiskFreeSpaceEx (or GetDiskFreeSpace)
//  to get free space on any disk

#include <windows.h>
#include <iostream.h>

int GET_DISK_SPACE(char *,float *,float *,float *);
int GET_WIN_VERSION(int *,int *,int *,int *);

int main(int argc, char *argv[])
{
   char szDrive[10]="c:\\";
   int iRes;
   float fTotalMB,fAvailMB,fFreeMB;

   cout << "Enter any drive letter, (e.g. C)" << endl;
   cin >> szDrive;

    // ignore anything else in input buffer until first newline
    cin.ignore(1024, '\n');
   
    strcat(szDrive,":\\");

   cout << "Root directory=" << szDrive << endl;

   iRes=GET_DISK_SPACE(szDrive,&fAvailMB,&fTotalMB,&fFreeMB);
   if(iRes!=0)
   {
      cout << "error code=" << iRes << endl;
      return 0;
   }

   // Truncate to two decimal places
   fTotalMB=float(double(int(fTotalMB*100.))/100.);

   // Truncate to two decimal places
   fFreeMB=float(double(int(fFreeMB*100.))/100.);

   // Truncate to two decimal places
   fAvailMB=float(double(int(fAvailMB*100.))/100.);

   cout << "Total space     = " << fTotalMB << " Mbytes" << endl;
   cout << "Free space      = " << fFreeMB << " Mbytes" << endl;
   cout << "Available space = " << fAvailMB << " Mbytes"
      << endl << endl;

   cout << "Press ENTER key to quit" << endl;
   cin.get();
   return 0;
}

int GET_DISK_SPACE(char szDrive[],float *fAvailMB,
               float *fTotalMB, float *fFreeMB)
//  Created: 3/20/2001  (rk)
//  Last modified: 3/28/2001  (rk)
//  Please report any problems or bugs to kochhar@physiology.wisc.edu
//  The latest version of this routine can be found at:
//     http://www.neurophys.wisc.edu/ravi/software/diskspace/
//  Get the amount of free space and other information
//  about specified disk
//     szDrive   : Root directory on disk (e.g. c:\)
//     fAvailMB  : Amount of available space for current user (MB)
//     fTotalMB  : Total space on disk (MB)
//     fFreeMB   : Free space on disk (MB)
//  The return code can be one of the following:
//     0    : Normal, no error
//     606  : Unable to identify system type
//     648  : Unable to load library
//     649  : Disk name may be invalid or Disk not accessible
//  Note: for Win/2000 or later, the amount of available
//  space may be less than the amount of free space if
//  disk quotas are enabled.
//  This routine loads GetDiskFreeSpaceEx at run-time to
//  allow this to work with all 32-bit versions of Windows
//  (including all versions of Win/95)
{
   BOOL bRes;
    ULARGE_INTEGER ilBytesAvail,ilFreeBytesTotal,ilBytesTotal;
    HINSTANCE hInstLib;
   int iRes,iSys,iMaj,iMin,iPack,iOSflag;
   DWORD dwSectorsPerCluster,dwBytesPerSector,dwNumFreeClusters,
      dwTotalClusters;

    // Function Pointers
    BOOL (WINAPI *lpfGetDiskFreeSpaceExA)(LPCTSTR,
      PULARGE_INTEGER,PULARGE_INTEGER,PULARGE_INTEGER);

   // Determine Windows OS version
   iRes=GET_WIN_VERSION(&iSys,&iMaj,&iMin,&iPack);
   if(iRes!=0) return iRes;

   // iOSflag=1 for versions before Win95B(OSR2)
   // iOSflag=2 for all NT versions, and Win versions Win/95B
   // and higher (i.e. Win/98, Win/ME etc.)

   iOSflag=1;
   if(iSys>2) iOSflag=2;
   if((iSys=2)&&(iPack>0)) iOSflag=2;    // Win/95(OSR2)

   // Case of NT or Windows after and including Win/95(OSR2)
   // Here we can use GetDiskFreeSpaceEx

   if(iOSflag==2)
   {
      // Load the library dynamically
        hInstLib = LoadLibraryA("KERNEL32.DLL");
        if(hInstLib == NULL) return 648;
        // Get procedure address.
        lpfGetDiskFreeSpaceExA=(BOOL(WINAPI *)(LPCTSTR,
          PULARGE_INTEGER,PULARGE_INTEGER,PULARGE_INTEGER))
            GetProcAddress(hInstLib, "GetDiskFreeSpaceExA");

       bRes=lpfGetDiskFreeSpaceExA(szDrive,&ilBytesAvail,
          &ilBytesTotal,&ilFreeBytesTotal);
        FreeLibrary(hInstLib);

      // If GetDiskFreeSpaceExA failed, most certainly
      // it is because drive name was invalid
      if(!bRes) return 649;

       // Total Space on drive (Mbytes)
        *fTotalMB=float(double(__int64(ilBytesTotal.QuadPart))/(1024.*1024.));

       // Free Space on drive (in Mbytes)
        *fFreeMB=float(double(__int64(ilFreeBytesTotal.QuadPart))/(1024.*1024.));

       // Available Space on drive (in Mbytes)
        *fAvailMB=float(double(__int64(ilBytesAvail.QuadPart))/(1024.*1024.));
   }

   // Case of Windows older than Win/95B(OSR2)
   // Here we have to use GetDiskFreeSpace

   if(iOSflag==1)
   {
      bRes=GetDiskFreeSpaceA(szDrive,&dwSectorsPerCluster,
         &dwBytesPerSector,&dwNumFreeClusters,&dwTotalClusters);

      if(!bRes) return 649;

      // Compute free space in Megabytes
      *fAvailMB=(float(dwNumFreeClusters)*float(dwSectorsPerCluster)
         *float(dwBytesPerSector)/(float(1024.*1024.)));

      // Compute total space in Megabytes
      *fTotalMB=(float(dwTotalClusters)*float(dwSectorsPerCluster)
         *float(dwBytesPerSector)/(float(1024.*1024.)));

      // For older windows, free space and available space are same
      *fFreeMB=*fAvailMB;
   }

   return 0;
}

int GET_WIN_VERSION(int *iSysType,int *iVerMajor,
               int *iVerMinor,int *iPack)
// Created: 1/4/2001  (RK)
// Last modified: 3/21/2001  (RK)
// Please report any problems or bugs to kochhar@physiology.wisc.edu
// The latest version of this routine can be found at:
//     http://www.neurophys.wisc.edu/ravi/software/winver/
// Get the system version number and service pack number
// This works for Win 3.x(Win32s) and Win/95/98/ME and also Win/NT/2000
// Return codes are as follows:
//   0   = Normal termination
//   606 = Unable to identify system type
// iSysType has the following values:
//   0=Unknown, 1=Win3.x, 2=Win/95, 3=Win/98 4=Win/ME, 5=Win/NT 6=Win/2000
// iVerMajor is (e.g.) 4 for Win/NT, 5 for Win/2000
// iVerMinor is (e.g.) 51 for NT v3.51, 0 for NT v 4.00
// iPack (service pack #) is meaningful for NT or 2000
// (and also for Win/95, where it is returned as 0 or 1 to
//  indicate Win/95 or Win/95B(OSR2) respectively)
{
   BOOL bResult;
   OSVERSIONINFO osvi;
   char szPack[4]="-1";

   // First check what version of Windows we're in
   osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    bResult=::GetVersionEx(&osvi);
   if(!bResult)     // Unable to identify system version
       return 606;

   // The system Type (1=Win3.1, 2=Win95, 3=Win98/ME,
   //    4=Win/ME, 5=Win/NT  6=Win/2000)
   if(osvi.dwPlatformId == VER_PLATFORM_WIN32s)
        *iSysType=1;
   if(osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
      *iSysType=2;
   if(osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
        *iSysType=5;

   // Major version number
   *iVerMajor=osvi.dwMajorVersion;
   if(*iVerMajor >= 5) *iSysType=6;

   // Minor version number
   *iVerMinor=osvi.dwMinorVersion;

   // For Win/98, minor version is greater than zero
   if((*iSysType==2) && (*iVerMinor>0)) *iSysType=3;
   // For Win/ME, minor version is 90 or greater
   if((*iSysType==3) && (*iVerMinor>=90)) *iSysType=4;

   // Service Pack info
   // For NT/2000, extract the service pack as a number
   *iPack=-1;
   if((*iSysType>=5) && (strcmp(osvi.szCSDVersion,"Service Pack")))
   {
       szPack[0]=osvi.szCSDVersion[12];
       szPack[1]=osvi.szCSDVersion[13];
       szPack[2]=osvi.szCSDVersion[14];
      *iPack=atoi(szPack);
   }

   // For Win/95, use Build Number to extract OSR info
   // and return in iPack

   if(*iSysType==2)
   {
      *iPack=0;
      if(osvi.dwBuildNumber>67109814) *iPack=1;   // OSR2 (4.00.950B)
   }

   return 0;
}