In fact, I write like this to get volume capacity.
NTSTATUS
HDGetVolumeInfoEx(
IN HD_DEVICE_TYPE DeviceType,
IN PUNICODE_STRING DeviceName,
OUT LONGLONG *Capacity
)
/***
***Routine Description:
***Get the volume capacity of the volume with the name of “DeveciName”. And the capacity is
***stored in the Capacity.
***/
{
HANDLE handle;
NTSTATUS status;
IO_STATUS_BLOCK ioStatus;
OBJECT_ATTRIBUTES objectAttributes;
PDISK_GEOMETRY_EX diskGeom;
ASSERT(DeviceName != NULL);
//Allocate buffer to store the geometry info of the volume.
diskGeom = (PDISK_GEOMETRY_EX)ExAllocatePoolWithTag(NonPagedPool, 512, SFLT_POOL_TAG);
if( diskGeom == NULL )
return STATUS_UNSUCCESSFUL;
//
// Open the device file according to the device name.
//
DbgPrint(“SFilter(HDGetVolumeInfoEx): %wZ\n”, DeviceName );
InitializeObjectAttributes( &objectAttributes,
DeviceName,
OBJ_KERNEL_HANDLE|OBJ_CASE_INSENSITIVE,
NULL,
NULL
);
status = ZwCreateFile( &handle,
GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE,
&objectAttributes,
&ioStatus,
NULL,
FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_READ | FILE_SHARE_WRITE,
0,
0,
NULL,
0
);
if( !NT_SUCCESS(status) ){
ExFreePool( diskGeom );
DbgPrint( “Sfilter(HDGetVolumeInfo): Fail to open device\n” );
return status;
}
//
// Send IOCTL according to the specific type of device object.
//
switch( DeviceType ){
case HD_HDO_DISK:
status = ZwDeviceIoControlFile( handle,
NULL,
NULL,
NULL,
&ioStatus,
IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
NULL,
0,
diskGeom,
512
);
break;
case HD_HDO_CDROM:
case HD_HDO_DVDROM:
status = ZwDeviceIoControlFile( handle,
NULL,
NULL,
NULL,
&ioStatus,
IOCTL_CDROM_GET_DRIVE_GEOMETRY_EX,
NULL,
0,
diskGeom,
512
);
break;
}
if( !NT_SUCCESS(ioStatus.Status) ){
*Capacity = 0x00;
ExFreePool( diskGeom );
return ioStatus.Status;
}
else{
*Capacity = diskGeom->DiskSize.HighPart;
*Capacity = *Capacity<< 32;
*Capacity += diskGeom->DiskSize.LowPart;
}
DbgPrint( “SFilter(HDGetVolumeInfoEx): Capacity is %X\n”, (*Capacity)>>20 ); //Output the capacity(MB)
ZwClose( handle );
ExFreePool( diskGeom );
return STATUS_SUCCESS;
}
Is there anything wrong? By the way, FatQueryFsSizeInfo can be called in filter driver?