Determining Cluster Size

Occasionally, your program may need more low-level information about the computer it's running
on than is readily available with MFC. Usually, this can be solved by a simple API call, which, with
a small amount of practice and patience, can become as easy to deal with as MFC. This appears to be
the case with determining the cluster size of a local hard disk. As you may know, hard drives are
divided up into partitions, which can be in either the FAT16 or FAT32 format on a Windows/DOS machine.
(There are more, including those used by Windows NT and other operating systems) Files in the partition
are stored in clusters, which are a set size depending on the type (16 or 32) and the size of the partition.
The number of clusters a file uses can be figured using this formula: File Size / Cluster Size, rounded
up. For example, a 20,000 byte file on a partition with 4,096 byte clusters would use 5 clusters becuase
20000 / 4096 = 4.8, rounded = 5.

Problem

As mentioned earlier, it would seem that getting the cluster size under Windows95/98 is an easy task. The
Win32 API provides the ::GetDiskFreeSpace() function, which returns the cluster size of the drive you pass
to it, along with other information about it. However, things aren't that simple when you
start dealing with newer hardware. As of Windows95 OSR2, there is a new partition format available, called
FAT32. FAT32 breaks FAT16's 2GB partition size limit, which makes it particularly useful for today's high
capacity drives. However, the ::GetDiskFreeSpace() function does not return correct values for partitions
larger than 2GB. There is a replacement function provided in the KERNEL32.DLL included with OSR2+
(including Windows98) called ::GetDiskFreeSpaceEx(). However, this function was made to simplify getting
disk space values and no longer returns cluster size as one of its parameters. So begins the ponderence of
how to get the cluster size on drives larger than 2GB.

Solution

Unfortunately, there is no other API function to deal with the disk like this. Instead, you must delve deeper
into the operating system to find the answer. Hidden in the volumes of documentation with Developer Studio is
a list of new FAT32 functions. But these are not functions like you're used to seeing, unless you are an
experienced Assembly language programmer. The specific function we will make use of is Int 21h Function
7303h Get_ExtFreeSpace (FAT32) which you can lookup in Developer Studio. This is an interrupt call
to the operating system that returns a structure containing lots of information about the disk, including
cluster size. But implementing this function is not what you might call a simple task. Rather, you need
to make a few definitions, and then do some rather low-level messing around. But, because you are reading this
it obviously means I have gone through this work for you, and you get this function wrapped up in a nice class
and member function. But if you would like to know more about it, read on. If not, skip to the end and download!

First, you need a few defintions to work with. Part of making an Int 21 call invloves setting some CPU
registers explicitly, so we need a structure to work with. Although it would seem these structures *should*
be defined somewhere, I could not find them, so I put them in the .H file. The following allows us to
work with all the applicable registers:

Next, we need to define the strucure the function will return the data to use in. It needs to be packed
on 1 byte boundaries, and we also define the DeviceIoControl (more on this later) parameter constant which
again, was *supposed* to be defined already.

Thats it for definitions. Now the implementation. One of the first things to do is decide if we even need
to use the Int 21 function. If this is NOT an OSR2+ machine, we can still use the ::GetDiskFreeSpace() function,
and save some work. The following checks the OS version and sets the flag for OSR2.

Now, if it turns out that this IS an OSR2+ machine, we have to use the Int 21 function. To do this, first
you have to create a device handle to the VWIN32 device, which basically makes all this possible. It creates
a sort of virtual machine that these calls can be made though, behaving much more like a DOS environment
than Windows. Once that device is created, you initialize the registers structure to tell it exactly what to
do and where to put the data. The EAX member is like using the AX register, so the actual function number
gets put in there (0x7303). Setting the structure does not change the CPU registers though. This happens
once DeviceIoControl() is called. What DeviceIoControl() does is perform the Interrupt operation after copying our
registers structure. It then takes the state of the CPU registers AFTER the call, and returns them in the
same structure.

More important than the new register states, is if the operation went correctly, our ExtGetDskFreSpcStruc should
be filled with correct values. All that's left to do if find the cluster size by multiplying the Sectors Per
Cluster by the Bytes per Sector. (Of course, they couldn't even make THAT easy on us)

Conclusion

That's about it for getting the cluster size. However, I DID leave out some code on this page. In order
to use this, you SHOULD download the full code (come on, it's only 2 files). There is also a simply sample
application that shows an easy way of implementing this using the class I created, CClusterSize. To use this
in your project, simply add "ClusterSize.h" and "ClusterSize.cpp" to your project, include the .H file appropriately,
use either the 1 or 2 step initialization (see the header file), and call GetClusterSize().

Who can solve the the trouble? I very very thank you!

How can I get the position of cluster and sector where a file locate? I mean that I want to get the position of a file in a partition.But the position must use "cluster" and
sector to appear.
At last please use English to answer!Or I can't understand.

Re DeviceIoControl

It would be very enlightening if you can wite a code that uses DeviceIoControl,to get the _EA_DEVICEPARAMETERS structure informations.
I found some inline assembly code to do that.
I am not familiar with assembly,and I wonder if it's possible to have the arguments of the function
DeviceIoControl in order to call it.
Thanks

Top White Papers and Webcasts

U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there is simply not enough quality talent to go around. Tiempo Development is a nearshore software development company. Our headquarters are in AZ, but we are a pioneer and leader in outsourcing to Mexico, based on our three software development centers there. We have a proven process and we are experts at providing our customers with powerful solutions. We transform ideas into reality.

When individual departments procure cloud service for their own use, they usually don't consider the hazardous organization-wide implications. Read this paper to learn best practices for setting up an internal, IT-based cloud brokerage function that service the entire organization. Find out how this approach enables you to retain top-down visibility and control of network security and manage the impact of cloud traffic on your WAN.