Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

WinAPI to get total disk drive count

I would like to check if there is any WinAPI which is able to query/obtain the total number of disk drive devices.

Currently, I am doing this in the following way which is not ideal and not the best implementation:

[code]
HANDLE handle;
int numDrives = 0;
char szDevname[256];

while(1)
{
sprintf( szDevname, "\\.\PHYSICALDRIVE%d", pdNumber );

// create a handle to the device
handle = CreateFileA( szDevname,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL );

if ( handle == INVALID_HANDLE_VALUE )
{
// Not a good way to check because there could be cases
// where a drive could be removed, for example a USB drive,
// which might result in the \.PHYSICALDRIVE%d numbering
// to have a gap
break;
}
else
{
numDrives++;
}
}
[/code]

Any ideas of any WinAPI that can do this?

Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : I would like to check if there is any WinAPI which is able to
    : query/obtain the total number of disk drive devices.
    :
    : Currently, I am doing this in the following way which is not ideal
    : and not the best implementation:
    :
    : [code]:
    : HANDLE handle;
    : int numDrives = 0;
    : char szDevname[256];
    :
    : while(1)
    : {
    : sprintf( szDevname, "\\.\PHYSICALDRIVE%d", pdNumber );
    :
    : // create a handle to the device
    : handle = CreateFileA( szDevname,
    : GENERIC_READ | GENERIC_WRITE,
    : FILE_SHARE_READ | FILE_SHARE_WRITE,
    : NULL,
    : OPEN_EXISTING,
    : 0,
    : NULL );
    :
    : if ( handle == INVALID_HANDLE_VALUE )
    : {
    : // Not a good way to check because there could be cases
    : // where a drive could be removed, for example a USB drive,
    : // which might result in the \.PHYSICALDRIVE%d numbering
    : // to have a gap
    : break;
    : }
    : else
    : {
    : numDrives++;
    : }
    : }
    : [/code]:
    :
    : Any ideas of any WinAPI that can do this?
    :
    [color=Blue]From here:

    [link=http://msdn.microsoft.com/en-us/library/aa383686(VS.85).aspx]http://msdn.microsoft.com/en-us/library/aa383686(VS.85).aspx[/link]

    Find a link to "Volume Management" and there "GetLogicalDrives".
    [/color]
  • carlycarly Member Posts: 46
    : [link=http://msdn.microsoft.com/en-us/library/aa383686(VS.85).aspx]ht
    : tp://msdn.microsoft.com/en-us/library/aa383686(VS.85).aspx[/link]
    :
    : Find a link to "Volume Management" and there "GetLogicalDrives".
    : [/color]

    No, it has nothing to do with logical drives.


  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : : [link=http://msdn.microsoft.com/en-us/library/aa383686(VS.85).aspx]ht
    : : tp://msdn.microsoft.com/en-us/library/aa383686(VS.85).aspx[/link]
    : :
    : : Find a link to "Volume Management" and there "GetLogicalDrives".
    : : [/color]
    :
    : No, it has nothing to do with logical drives.
    :
    :
    :
    [color=Blue]I see... you need a physical Hard Drives on a system? Maybe, try "System Information" link on Win32 API.[/color]
Sign In or Register to comment.