Detect USB drive and its related information.
-
wrote on 24 Aug 2012, 04:59 last edited by
I need to develop a software that detects when a USB drive is plugged in the a computer, get it's friendly name & drive letter.
i am trying with GetVolumeInformation().how to i do get drive information with it.
-
wrote on 24 Aug 2012, 07:24 last edited by
Hi,
if you want do to that on Windows:
- register to device notification
@
DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE, 0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED);
// ...
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE;HDEVNOTIFY hDevNotify = RegisterDeviceNotification(winId, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
@- in winEvent check the message:
@
if (WM_DEVICECHANGE == message->message) {
switch(message->wParam) {
case DBT_DEVICEARRIVAL:
msgType = "WM_DEVICECHANGE DBT_DEVICEARRIVAL: ";
interest = true;
// device plugged in
break;case DBT_DEVICEREMOVECOMPLETE : msgType = "WM_DEVICECHANGE DBT_DEVICEREMOVECOMPLETE: "; interest = true; // device plugged out break; }
}
if (interest) {
DEV_BROADCAST_HDR* pMsgPtr = (DEV_BROADCAST_HDR*)message->lParam;
if (pMsgPtr) {
switch (pMsgPtr->dbch_devicetype) {
case DBT_DEVTYP_DEVICEINTERFACE:
// retrieve vid and pid
break;
case DBT_DEVTYP_VOLUME:
DEV_BROADCAST_VOLUME* volume = (DEV_BROADCAST_VOLUME*)pMsgPtr;
// volume gives you all information on mount point
break;
}
}
}
@ - register to device notification
-
wrote on 24 Aug 2012, 08:23 last edited by
i want to get
- list of drives
2.total size
3.used space
4.free space
- list of drives
-
wrote on 24 Aug 2012, 08:26 last edited by
Dear can you please let me know all the respective header files .i am unable to register it.
-
wrote on 24 Aug 2012, 08:30 last edited by
Hi,
these are the headers:
@
#include <initguid.h>
#include <dbt.h>
@for information on disk space you can use GetDiskFreeSpaceEx (see "MSDN":http://msdn.microsoft.com/en-us/library/windows/desktop/aa364937.aspx for documentation)
-
wrote on 8 Sept 2012, 02:45 last edited by
I am probably missing something obvious, but when trying to compile this code, I get the following errors:
mainwindow.cpp: In constructor 'MainWindow::MainWindow(QWidget*)':
mainwindow.cpp:19: error: 'DEV_BROADCAST_DEVICEINTERFACE' was not declared in this scope
mainwindow.cpp:24: error: 'DEVICE_NOTIFY_WINDOW_HANDLE' was not declared in this scope
mainwindow.cpp:24: error: 'RegisterDeviceNotificationW' was not declared in this scopeI've included initguid.h, dbt.h, as well as windows.h and winuser.h. Am I missing something?
-
wrote on 8 Sept 2012, 06:38 last edited by
That is of course not at all portable. If you care about other platforms you could put this code behind a nice interface that can get extended to other platforms later.
-
wrote on 8 Sept 2012, 16:27 last edited by
That is what I intend to do but the errors I'm getting are on Windows 7 professional SP1.
-
wrote on 17 Apr 2013, 16:34 last edited by
You need to specify which minimum version of Windows you are building for by setting WINVER. These routines were not available on older versions windows so they are only defined in the header files if WINVER is greater than or equal to 0x500.
Try adding the following line before you include the files windows.h and dbt.h:
#define WINVER 0x0501
You could also add the define to the project file.
See the following MSDN article for details on setting WINVER:
http://msdn.microsoft.com/en-gb/library/windows/desktop/aa383745 -
wrote on 18 Apr 2013, 05:04 last edited by
this function give u the infomation of usb drive plz pass drive letter as parameter .
#include <winbase.h>
#include <windows.h>
#include <initguid.h>
#include <dbt.h>@bool IsUsbDevice( wchar_t letter )
{
wchar_t volumeAccessPath[] = L"\\.\X:";
volumeAccessPath[4] = letter;HANDLE deviceHandle = CreateFileW( volumeAccessPath, 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; //query.AdditionalParameters. // 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; //qint64 abc=devd.Size; //GET_LENGTH_INFORMATION *sizeInfo; //DeviceIoControl(lDevice, IOCTL_DISK_GET_LENGTH_INFO, NULL, 0, sizeInfo, sizeof(sizeInfo), &junk, (LPOVERLAPPED) NULL); } else { std::wcout << L"Failed to define bus type for: " << letter; } CloseHandle(deviceHandle); return BusTypeUsb == busType;
}
@ -
wrote on 24 Sept 2013, 20:00 last edited by
Hi, please note that this thread deals with the USB class MSC, Mass Storage, only (right?) and so if the thread title were to be adjsted it would be more clear.
I'm searching for the HID class, but cannot find any Qt interface to it so far on Windows .