-
Notifications
You must be signed in to change notification settings - Fork 18
/
deviceenumerator_windows.cpp
71 lines (61 loc) · 2.01 KB
/
deviceenumerator_windows.cpp
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
#include "deviceenumerator_windows.h"
#include <windows.h>
#include "diskwriter_windows.h"
// Adapted from http://skilinium.com/blog/?p=134
QStringList DeviceEnumerator_windows::getRemovableDeviceNames() const
{
QStringList names;
WCHAR *szDriveLetters;
WCHAR szDriveInformation[1024];
GetLogicalDriveStrings(1024, szDriveInformation);
szDriveLetters = szDriveInformation;
while (*szDriveLetters != '\0') {
if (GetDriveType(szDriveLetters) == DRIVE_REMOVABLE) {
names << QString::fromWCharArray(szDriveLetters);
}
szDriveLetters = &szDriveLetters[wcslen(szDriveLetters) + 1];
}
return names;
}
QStringList DeviceEnumerator_windows::getUserFriendlyNames(const QStringList &devices) const
{
const QStringList suffixes = QStringList() << "bytes" << "kB" << "MB" << "GB" << "TB";
QStringList names;
foreach (const QString &dev, devices) {
quint64 size = getSizeOfDevice(dev);
if (size != 0) {
int idx = 0;
qreal fSize = size;
while ((fSize > 1024.0) && ((idx + 1) < suffixes.size())) {
++idx;
fSize /= 1024.0;
}
names << dev + " (" + QString::number(fSize, 'f', 2) + " " + suffixes[idx] + ")";
}
else {
names << dev;
}
}
return names;
}
quint64 DeviceEnumerator_windows::getSizeOfDevice(const QString &device)
{
HANDLE handle = DiskWriter_windows::getHandleOnDevice(device, GENERIC_READ);
if (handle == INVALID_HANDLE_VALUE) {
return 0;
}
DWORD junk;
quint64 size = 0;
GET_LENGTH_INFORMATION lenInfo;
bool ok = DeviceIoControl(handle,
IOCTL_DISK_GET_LENGTH_INFO,
NULL, 0,
&lenInfo, sizeof(lenInfo),
&junk,
(LPOVERLAPPED) NULL);
if (ok) {
size = lenInfo.Length.QuadPart;
}
CloseHandle(handle);
return size;
}