int Get_ndisks ()
{
HANDLE Hdevice; Handle to examined
int ndisks = 0;
Char path[20] = {"\\\\.\\physicaldrive0"};
do {
TRACE ("ndisks%s", path);
Hdevice = Createfilea (path,//drive to open
Generic_read,//access mode
File_share_read,//SHARE mode
NULL,//default security attributes
Open_existing,//disposition
0,//file attributes
NULL); Don ' t copy any file ' s attributes
if (hdevice! = INVALID_HANDLE_VALUE)
CloseHandle (Hdevice);
ndisks++;
PATH[17] = (char) (' 0 ' + ndisks);
}while (Hdevice! = INVALID_HANDLE_VALUE);
return ndisks-1;
}
Where path is the device name of the first hard disk, and the value of Ndisks indicates that there are currently several hard disks;
Do{}while (), Createfilea in the device is opened, if the return value is not Invalid_handle_value is open, then the ndisks+1, and path[17] is the number of the hard disk device 18th characters in the value, The corresponding character in "\\\\.\\physicaldrive0" is 0 because "\\\\.\\physicaldrive0" is "\\.\physicaldrive0" by escaping the actual string. The first hard disk has a sequence number of 0, the second is 1, and then it accumulates.
CreateFile This function can create or open a handle to an object that can be controlled by this handle:
Console object, communication resource object, directory object (only open), disk device object, file object, mail slot object, pipe object.
A more specific description of the CreateFile function is available for the MS MSDN query.
Chinese Introduction Rec:
Http://www.cnblogs.com/transcom/articles/1384946.html
Open source Ext2read code-How can I tell how many hard disk devices are in Windows?