[RESOLVED] problem with enum of hdd/partitions and opening a handle
Hi, im trying to get information about all hard disks and partitions of computer that im running on. I using
Code:
hDevInfo = SetupDiGetClassDevs( (GUID*)&DiskClassGuid, NULL, NULL, (DIGCF_PRESENT | DIGCF_INTERFACEDEVICE ) );
(some logging)
SP_INTERFACE_DEVICE_DATA hDeviceInterfaceData;
ZeroMemory( &hDeviceInterfaceData, sizeof( SP_INTERFACE_DEVICE_DATA ));
hDeviceInterfaceData.cbSize = sizeof( SP_INTERFACE_DEVICE_DATA );
result = SetupDiEnumDeviceInterfaces( hDevInfo, 0, (GUID*)&DiskClassGuid, index, &hDeviceInterfaceData );
SetupDiGetDeviceInterfaceDetail( hDevInfo, &hDeviceInterfaceData, pInterfaceDetail, dwDetailSize, &dwRequiredSize, 0);
And then in pInterfaceDetail->DevicePath I should have a name of the hard disk that Imable to use in CreateFile(). So I use that name, use CreateFile() and get that handle (no error here), but when using DeviceIoControl with IOCTL_DISK_GET_DRIVE_GEOMETRY_EX I get error INVALID_HANDLE.
Does anyone get a clue what is wrong?
If necessary I can paste all my code here.
Thanks!
Re: problem with enum of hdd/partitions and opening a handle
#1 You do not show your CreateFile(), to get the device handle.
the name you have to pass to it is"\\.\PHYSICALDRIVE<x>" where x will be the drive letter.
#2 None of your DeviceIOControl()s are going to work ever if this is not right.
You just muffed the device name.
HTH,
Re: problem with enum of hdd/partitions and opening a handle
My CreateFile
Code:
gDiskArray[index] = CreateFile( pInterfaceDetail->DevicePath ), GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0 );
And following MSDN
Quote:
DevicePath
A NULL-terminated string that contains the device interface path. This path can be passed to Win32 functions such as CreateFile.
Maybe there is an other way to get inforamtions about count of disk drivers and partitons of that drivers?
Thanks for reply.
//edit
Based on many forums (simple code)
Code:
do()
{
HardDisk.Format("\\\\.\\PhysicalDrive%d",index);
hDevice=CreateFile(HardDisk,0,FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,OPEN_EXISTING,0,NULL);
if(hDevice)
doStuff();
index++;
}
while( hDevice!=INVALID_HANDLE_VALUE )
Re: problem with enum of hdd/partitions and opening a handle
I've done it!!!
The problem was with DeviceIoControl, correct one is
Code:
DeviceIoControl(gDiskArray[index], IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,NULL, 0, &geoEx, sizeof(geoEx), &junk, NULL)
Previously I did use the OVERLAPPED struct. When removed everything seems to work correctly.
Re: [RESOLVED] problem with enum of hdd/partitions and opening a handle
And a little code
Code:
void describePartitions()
{
TCHAR drive[128] = TEXT("");
TCHAR letter = TEXT('');
TCHAR _out[256] = TEXT("");
DWORD _outSize = 256;
BOOL result = false;
DWORD volCount = 0;
for( letter = TEXT('c'); letter <= TEXT('z'); letter++ )
{
_stprintf( drive, TEXT("%c:\\"), letter );
result = GetVolumeNameForVolumeMountPoint( drive, _out, 256 );
if( result )
{
if( DRIVE_FIXED != GetDriveType( drive ) )
{
continue;
}
_stprintf( drive, TEXT("\\\\.\\%c:"), letter );
HANDLE h = CreateFile( drive, STANDARD_RIGHTS_READ|STANDARD_RIGHTS_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_ALWAYS, 0, 0 );
if( NULL == h )
{
printErr();
continue;
}
else
{
addText(_T("h-ok"));
}
VOLUME_DISK_EXTENTS vde;
DWORD junk;
if( TRUE == DeviceIoControl( h, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, 0, 0, &vde, sizeof( VOLUME_DISK_EXTENTS ), &junk, 0 ) )
{
TCHAR _buf[256];
_stprintf( _buf, TEXT("%s: disks: %d"), drive, vde.NumberOfDiskExtents );
addText( _buf );
for( DWORD j=0; j<vde.NumberOfDiskExtents; ++j )
{
_stprintf( _buf, TEXT("%s placed on disk %d"), drive, vde.Extents[j].DiskNumber );
addText( _buf );
}
}
else
{
printErr();
}
addText( drive );
addText( _out );
++volCount;
CloseHandle( h );
}
}
}
This should print out all hard drives and partitions on it. Some USB will also be visible.
No warranty! ;)