给你一段代码,不知道有没有用:
size_t szDriveString = GetLogicalDriveStrings( 0, NULL );
char * pDriveString = new char[szDriveString];
GetLogicalDriveStrings( szDriveString, pDriveString );
size_t szOffset = 0;
while( szOffset < szDriveString - 1 )
{
size_t szStringLen = strlen( pDriveString + szOffset );
char * pString = new char[szStringLen + 1];
strncpy( pString, pDriveString + szOffset, szStringLen + 1 );
szOffset += szStringLen + 1;
cout << pString << '\t';
UINT nType = GetDriveType( pString ); switch( nType )
{
case DRIVE_UNKNOWN:
cout << "DRIVE_UNKNOWN\n";
break;
case DRIVE_NO_ROOT_DIR:
cout << "DRIVE_NO_ROOT_DIR\n";
break;
case DRIVE_REMOVABLE:
cout << "DRIVE_REMOVABLE\n";
break;
case DRIVE_FIXED:
cout << "DRIVE_FIXED\n";
break;
case DRIVE_REMOTE:
cout << "DRIVE_REMOTE\n";
break;
case DRIVE_CDROM:
cout << "DRIVE_CDROM\n";
break;
case DRIVE_RAMDISK:
cout << "DRIVE_RAMDISK\n";
break;
default:
cout << "ERROR!\n";
} char lpVolumeNameBuffer[MAX_PATH];
DWORD dwVolumeSerialNumber;
DWORD dwMaximumComponentLength;
DWORD dwFileSystemFlag;
char lpFileSystemNameBuffer[MAX_PATH];
if( GetVolumeInformation( pString, lpVolumeNameBuffer, MAX_PATH, &dwVolumeSerialNumber, &dwMaximumComponentLength, &dwFileSystemFlag, lpFileSystemNameBuffer, MAX_PATH ) )
{
cout << "\tVolumeName: " << lpVolumeNameBuffer << endl;
cout << "\tVolumeSerialNumber: " << dwVolumeSerialNumber << endl;
cout << "\tMaximumComponentLength: " << dwMaximumComponentLength << endl;
cout << "\tFileSystemName: " << lpFileSystemNameBuffer <<endl; DWORD dwSectorsPerCluster;
DWORD dwBytesPerSector;
DWORD dwNumberOfFreeClusters;
DWORD dwTotalNumberOfClusters;
GetDiskFreeSpace( pString, &dwSectorsPerCluster, &dwBytesPerSector, &dwNumberOfFreeClusters, &dwTotalNumberOfClusters );
cout << "\tSectors Per Cluster: " << dwSectorsPerCluster << endl;
cout << "\tBytes Per Sector: " << dwBytesPerSector << endl;
double dTotalSize = dwTotalNumberOfClusters / 1024.0 / 1024.0 / 1024.0 * dwSectorsPerCluster * dwBytesPerSector;
double dFreeSize = dwNumberOfFreeClusters / 1024.0 / 1024.0 / 1024.0 * dwSectorsPerCluster * dwBytesPerSector;
cout.precision(4);
cout << "\tTotal size: " << dTotalSize << "GB\n";
cout << "\tFree size: " << dFreeSize << "GB\n";
} delete[] pString;
}
delete[] pDriveString;

解决方案 »

  1.   

    用下面两个api应该可以,我没有具体实现过
     BOOL   GetDiskFreeSpace(   
          LPCTSTR   lpRootPathName,         //   pointer   to   root   path   
          LPDWORD   lpSectorsPerCluster,     //   pointer   to   sectors   per   cluster   
          LPDWORD   lpBytesPerSector,     //   pointer   to   bytes   per   sector   
          LPDWORD   lpNumberOfFreeClusters,   
                                                                //   pointer   to   number   of   free   clusters   
          LPDWORD   lpTotalNumberOfClusters     
                                                                //   pointer   to   total   number   of   clusters   
      );    
    BOOL   GetVolumeInformation(   
          LPCTSTR   lpRootPathName,                       //   root   directory   
          LPTSTR   lpVolumeNameBuffer,                 //   volume   name   buffer   
          DWORD   nVolumeNameSize,                         //   length   of   name   buffer   
          LPDWORD   lpVolumeSerialNumber,           //   volume   serial   number   
          LPDWORD   lpMaximumComponentLength,   //   maximum   file   name   length   
          LPDWORD   lpFileSystemFlags,                 //   file   system   options   
          LPTSTR   lpFileSystemNameBuffer,         //   file   system   name   buffer   
          DWORD   nFileSystemNameSize                   //   length   of   file   system   name   buffer   
      );   
      

  2.   

    BOOL DeviceIoControl(
      (HANDLE) hDevice,                        // handle to device
      IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,  // dwIoControlCode
      NULL,                                    // lpInBuffer
      0,                                       // nInBufferSize
      (LPVOID) lpOutBuffer,                    // output buffer
      (DWORD) nOutBufferSize,                  // size of output buffer
      (LPDWORD) lpBytesReturned,               // number of bytes returned
      (LPOVERLAPPED) lpOverlapped              // OVERLAPPED structure
    );
    Parameters
    hDevice 
    [in] Handle to the volume. To retrieve a device handle, call the CreateFile function. 
    dwIoControlCode 
    [in] Control code for the operation. Use IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS for this operation. 
    lpInBuffer 
    Not used with this operation; set to NULL. 
    nInBufferSize 
    Not used with this operation; set to zero. 
    lpOutBuffer 
    [out] Pointer to a buffer that receives a VOLUME_DISK_EXTENTS structure specifying the physical location of the disk. 
    nOutBufferSize 
    [in] Size of the output buffer. 
    lpBytesReturned 
    [out] Pointer to a variable that receives the size of the data stored in the output buffer, in bytes. 
    If the output buffer is too small, the call fails, GetLastError returns ERROR_INSUFFICIENT_BUFFER, and lpBytesReturned is zero.If lpOverlapped is NULL, lpBytesReturned cannot be NULL. Even when an operation returns no output data and lpOutBuffer is NULL, DeviceIoControl makes use of lpBytesReturned. After such an operation, the value of lpBytesReturned is meaningless.If lpOverlapped is not NULL, lpBytesReturned can be NULL. If this parameter is not NULL and the operation returns data, lpBytesReturned is meaningless until the overlapped operation has completed. To retrieve the number of bytes returned, call GetOverlappedResult. If hDevice is associated with an I/O completion port, you can retrieve the number of bytes returned by calling GetQueuedCompletionStatus.lpOverlapped 
    [in] Pointer to an OVERLAPPED structure. 
    If hDevice was opened without specifying FILE_FLAG_OVERLAPPED, lpOverlapped is ignored. If hDevice was opened with the FILE_FLAG_OVERLAPPED flag, the operation is performed as an overlapped (asynchronous) operation. In this case, lpOverlapped must point to a valid OVERLAPPED structure that contains a handle to an event object. Otherwise, the function fails in unpredicatable ways. For overlapped operations, DeviceIoControl returns immediately, and the event object is signaled when the operation has been completed. Otherwise, the function does not return until the operation has been completed or an error occurs.Return Values
    If the operation succeeds, DeviceIoControl returns zero.If the operation fails, DeviceIoControl returns a nonzero value. To get extended error information, call GetLastError.
      

  3.   

    关键就是返回的VOLUME_DISK_EXTENTS结构体
      

  4.   

    VOLUME_DISK_EXTENTS结构里的信息都是物理分区的信息,与逻辑分区的信息无关