Hello gtworek,
thank you for your reply. I agree that your way is a good way to enumerate volumes, but you cannot enumerate physical drives that way.
Meanwhile I found a solution to my problem. It uses the Setup API. See attached code.
Greetings,
Metacrawler
list<wstring> GetPhysicalDisks()
{
HDEVINFO hDeviceInfoSet;
ULONG ulMemberIndex;
ULONG ulErrorCode;
BOOL bFound = FALSE;
BOOL bOk;
list<wstring> disks;
// create a HDEVINFO with all present devices
hDeviceInfoSet = ::SetupDiGetClassDevs(&GUID_DEVINTERFACE_DISK, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (hDeviceInfoSet == INVALID_HANDLE_VALUE) {
_ASSERT(FALSE);
return disks;
}
// enumerate through all devices in the set
ulMemberIndex = 0;
while (TRUE)
{
// get device info
SP_DEVINFO_DATA deviceInfoData;
deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
if (!::SetupDiEnumDeviceInfo(hDeviceInfoSet, ulMemberIndex, &deviceInfoData))
{
if (::GetLastError() == ERROR_NO_MORE_ITEMS) {
// ok, reached end of the device enumeration
break;
} else {
// error
_ASSERT(FALSE);
::SetupDiDestroyDeviceInfoList(hDeviceInfoSet);
return disks;
}
}
// get device interfaces
SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
if (!::SetupDiEnumDeviceInterfaces(hDeviceInfoSet, NULL, &GUID_DEVINTERFACE_DISK, ulMemberIndex, &deviceInterfaceData))
{
if (::GetLastError() == ERROR_NO_MORE_ITEMS) {
// ok, reached end of the device enumeration
break;
} else {
// error
_ASSERT(FALSE);
::SetupDiDestroyDeviceInfoList(hDeviceInfoSet);
return disks;
}
}
// process the next device next time
ulMemberIndex++;
// get hardware id of the device
ULONG ulPropertyRegDataType = 0;
ULONG ulRequiredSize = 0;
ULONG ulBufferSize = 0;
BYTE *pbyBuffer = NULL;
if (!::SetupDiGetDeviceRegistryProperty(hDeviceInfoSet, &deviceInfoData, SPDRP_HARDWAREID, &ulPropertyRegDataType, NULL, 0, &ulRequiredSize))
{
if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
pbyBuffer = (BYTE *)::malloc(ulRequiredSize);
ulBufferSize = ulRequiredSize;
if (!::SetupDiGetDeviceRegistryProperty(hDeviceInfoSet, &deviceInfoData, SPDRP_HARDWAREID, &ulPropertyRegDataType, pbyBuffer, ulBufferSize, &ulRequiredSize))
{
// getting the hardware id failed
_ASSERT(FALSE);
::SetupDiDestroyDeviceInfoList(hDeviceInfoSet);
::free(pbyBuffer);
return disks;
}
} else {
// getting device registry property failed
_ASSERT(FALSE);
::SetupDiDestroyDeviceInfoList(hDeviceInfoSet);
return disks;
}
} else {
// getting hardware id of the device succeeded unexpectedly
_ASSERT(FALSE);
::SetupDiDestroyDeviceInfoList(hDeviceInfoSet);
return disks;
}
// pbyBuffer is initialized now!
LPCWSTR pszHardwareId = (LPCWSTR )pbyBuffer;
// retrieve detailed information about the device
// (especially the device path which is needed to create the device object)
SP_DEVICE_INTERFACE_DETAIL_DATA *pDeviceInterfaceDetailData = NULL;
ULONG ulDeviceInterfaceDetailDataSize = 0;
ulRequiredSize = 0;
bOk = ::SetupDiGetDeviceInterfaceDetail(hDeviceInfoSet, &deviceInterfaceData, pDeviceInterfaceDetailData, ulDeviceInterfaceDetailDataSize, &ulRequiredSize, NULL);
if (!bOk)
{
ulErrorCode = ::GetLastError();
if (ulErrorCode == ERROR_INSUFFICIENT_BUFFER) {
// insufficient buffer space
// => that's ok, allocate enough space and try again
pDeviceInterfaceDetailData = (SP_DEVICE_INTERFACE_DETAIL_DATA *)::malloc(ulRequiredSize);
pDeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
ulDeviceInterfaceDetailDataSize = ulRequiredSize;
deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
bOk = ::SetupDiGetDeviceInterfaceDetail(hDeviceInfoSet, &deviceInterfaceData, pDeviceInterfaceDetailData, ulDeviceInterfaceDetailDataSize, &ulRequiredSize, &deviceInfoData);
ulErrorCode = ::GetLastError();
}
if (!bOk) {
// retrieving detailed information about the device failed
_ASSERT(FALSE);
::free(pbyBuffer);
::free(pDeviceInterfaceDetailData);
::SetupDiDestroyDeviceInfoList(hDeviceInfoSet);
return disks;
}
} else {
// retrieving detailed information about the device succeeded unexpectedly
_ASSERT(FALSE);
::free(pbyBuffer);
::SetupDiDestroyDeviceInfoList(hDeviceInfoSet);
return disks;
}
disks.push_back(pDeviceInterfaceDetailData->DevicePath);
// free buffer for device interface details
::free(pDeviceInterfaceDetailData);
// free buffer
::free(pbyBuffer);
}
// destroy device info list
::SetupDiDestroyDeviceInfoList(hDeviceInfoSet);
return disks;
}
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139:





by: gtworekPosted on 2009-08-12 at 01:50:27ID: 25076769
Use FindFirstVolume - http://msdn.microsoft.com/ en-us/libr ary/aa3644 25(VS.85). aspx en-us/libr ary/aa3644 31(VS.85). aspx
en-us/libr ary/aa3649 39(VS.85). aspx
then FindNextVolume - http://msdn.microsoft.com/
and check the drive type with GetDriveType - http://msdn.microsoft.com/