Skip to content

Commit

Permalink
[WIN32] fixed: USB drives are detected as DRIVE_FIXED by windows and …
Browse files Browse the repository at this point in the history
…thus not added in our query for removable drives. This adds an additional query if a DRIVE_FIXED is a usb device.
  • Loading branch information
wsoltys committed Nov 8, 2011
1 parent 2890af7 commit 3e5a4e3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
55 changes: 55 additions & 0 deletions xbmc/win32/WIN32Util.cpp
Expand Up @@ -787,6 +787,15 @@ void CWIN32Util::GetDrivesByType(VECSOURCES &localDrives, Drive_Types eDriveType
iPos += (wcslen( pcBuffer + iPos) + 1 );
continue;
}

// usb hard drives are reported as DRIVE_FIXED and won't be returned by queries with REMOVABLE_DRIVES set
// so test for usb hard drives
if(uDriveType == DRIVE_FIXED)
{
if(IsUsbDevice(strWdrive))
uDriveType = DRIVE_REMOVABLE;
}

share.strPath= share.strName= "";

bool bUseDCD= false;
Expand Down Expand Up @@ -1494,3 +1503,49 @@ extern "C"
return NULL;
}
}

// detect if a drive is a usb device
// code taken from http://banderlogi.blogspot.com/2011/06/enum-drive-letters-attached-for-usb.html

bool CWIN32Util::IsUsbDevice(const CStdStringW &strWdrive)
{
CStdStringW strWDevicePath;
strWDevicePath.Format(L"\\\\.\\%s",strWdrive.Left(2));

/* wchar_t volumeAccessPath[] = L"\\\\.\\X:";
volumeAccessPath[4] = strWdrive.Left(1).c_str();*/

HANDLE deviceHandle = CreateFileW(
strWDevicePath.c_str(),
0, // no access to the drive
FILE_SHARE_READ | // share mode
FILE_SHARE_WRITE,
NULL, // default security attributes
OPEN_EXISTING, // disposition
0, // file attributes
NULL); // do not copy file attributes

// setup query
STORAGE_PROPERTY_QUERY query;
memset(&query, 0, sizeof(query));
query.PropertyId = StorageDeviceProperty;
query.QueryType = PropertyStandardQuery;

// issue query
DWORD bytes;
STORAGE_DEVICE_DESCRIPTOR devd;
STORAGE_BUS_TYPE busType = BusTypeUnknown;

if (DeviceIoControl(deviceHandle,
IOCTL_STORAGE_QUERY_PROPERTY,
&query, sizeof(query),
&devd, sizeof(devd),
&bytes, NULL))
{
busType = devd.BusType;
}

CloseHandle(deviceHandle);

return BusTypeUsb == busType;
}
2 changes: 2 additions & 0 deletions xbmc/win32/WIN32Util.h
Expand Up @@ -83,6 +83,8 @@ class CWIN32Util
static bool GetFocussedProcess(CStdString &strProcessFile);
static void CropSource(CRect& src, CRect& dst, CRect target);

static bool IsUsbDevice(const CStdStringW &strWdrive);

private:
#if _MSC_VER > 1400
static DEVINST GetDrivesDevInstByDiskNumber(long DiskNumber);
Expand Down

0 comments on commit 3e5a4e3

Please sign in to comment.