Discussion:
USB Storage Device Detetcion
(too old to reply)
Timothy Jewett
2010-05-27 20:45:13 UTC
Permalink
I am trying to wite a service that will detect when a USB storage device is
inserted to display a warning message. I have written the service that
detects USB device insertions but I get my message when any USB device is
inserted. I would like to narrow that down to just storage devices, thumb
drives and external hard disks...

Can someone point me in the correct direction as to how I can narrow that
down ?

Thanks,
--
Timothy Jewett
***@online.nospam
Pavel A.
2010-05-28 03:15:41 UTC
Permalink
Post by Timothy Jewett
I am trying to wite a service that will detect when a USB storage device is
inserted to display a warning message. I have written the service that
detects USB device insertions but I get my message when any USB device is
inserted. I would like to narrow that down to just storage devices, thumb
drives and external hard disks...
Can someone point me in the correct direction as to how I can narrow that
down ?
Thanks,
--
Timothy Jewett
When you register for hardware change notification, maybe you specify a
wrong id in the filter.
You need DBT_DEVTYP_VOLUME, or DBT_DEVTYP_DEVICEINTERFACE
with class = DiskDrive or Volume.

-- pa
Microsoft Corporation
2010-06-01 12:16:24 UTC
Permalink
//Add these code to the initialization part

DEV_BROADCAST_HDR NotificationFilter;
NotificationFilter.dbch_size = sizeof(DEV_BROADCAST_HDR);
NotificationFilter.dbch_devicetype = DBT_DEVTYP_VOLUME;

hDevNotify = RegisterDeviceNotification(hMainWnd, &NotificationFilter,
DEVICE_NOTIFY_WINDOW_HANDLE);

///<\Custom Initialization Code>

//Application_Run(), can also be a message loop.

UnregisterDeviceNotification(hDevNotify);

//So that you can get notifications only when Mass Storage Device is
inserted.


----- Original Message -----
From: "Timothy Jewett" <***@online.nospam>
Newsgroups: microsoft.public.win32.programmer.kernel
Sent: Friday, May 28, 2010 04:45
Subject: USB Storage Device Detetcion
Post by Timothy Jewett
I am trying to wite a service that will detect when a USB storage device is
inserted to display a warning message. I have written the service that
detects USB device insertions but I get my message when any USB device is
inserted. I would like to narrow that down to just storage devices, thumb
drives and external hard disks...
Can someone point me in the correct direction as to how I can narrow that
down ?
Thanks,
--
Timothy Jewett
Timothy Jewett
2010-06-04 19:23:05 UTC
Permalink
I tried your example and I get a 0x42A returned from
RegisterDeviceNotification(), any idea ?
--
Timothy Jewett
Post by Microsoft Corporation
//Add these code to the initialization part
DEV_BROADCAST_HDR NotificationFilter;
NotificationFilter.dbch_size = sizeof(DEV_BROADCAST_HDR);
NotificationFilter.dbch_devicetype = DBT_DEVTYP_VOLUME;
hDevNotify = RegisterDeviceNotification(hMainWnd, &NotificationFilter,
DEVICE_NOTIFY_WINDOW_HANDLE);
///<\Custom Initialization Code>
//Application_Run(), can also be a message loop.
UnregisterDeviceNotification(hDevNotify);
//So that you can get notifications only when Mass Storage Device is
inserted.
----- Original Message -----
Newsgroups: microsoft.public.win32.programmer.kernel
Sent: Friday, May 28, 2010 04:45
Subject: USB Storage Device Detetcion
Post by Timothy Jewett
I am trying to wite a service that will detect when a USB storage device is
inserted to display a warning message. I have written the service that
detects USB device insertions but I get my message when any USB device is
inserted. I would like to narrow that down to just storage devices, thumb
drives and external hard disks...
Can someone point me in the correct direction as to how I can narrow that
down ?
Thanks,
--
Timothy Jewett
.
Timothy Jewett
2010-06-09 21:32:44 UTC
Permalink
I did figure out the 42a was because you cannot register for a broadcast
volume. I do receive the broadcast volume on thumb drives but do not receive
anything for CD/DVD media insertion. Is ther some other method to receive the
change for media on cd/dvd devices ?
--
Timothy Jewett
Post by Microsoft Corporation
//Add these code to the initialization part
DEV_BROADCAST_HDR NotificationFilter;
NotificationFilter.dbch_size = sizeof(DEV_BROADCAST_HDR);
NotificationFilter.dbch_devicetype = DBT_DEVTYP_VOLUME;
hDevNotify = RegisterDeviceNotification(hMainWnd, &NotificationFilter,
DEVICE_NOTIFY_WINDOW_HANDLE);
///<\Custom Initialization Code>
//Application_Run(), can also be a message loop.
UnregisterDeviceNotification(hDevNotify);
//So that you can get notifications only when Mass Storage Device is
inserted.
----- Original Message -----
Newsgroups: microsoft.public.win32.programmer.kernel
Sent: Friday, May 28, 2010 04:45
Subject: USB Storage Device Detetcion
Post by Timothy Jewett
I am trying to wite a service that will detect when a USB storage device is
inserted to display a warning message. I have written the service that
detects USB device insertions but I get my message when any USB device is
inserted. I would like to narrow that down to just storage devices, thumb
drives and external hard disks...
Can someone point me in the correct direction as to how I can narrow that
down ?
Thanks,
--
Timothy Jewett
.
Jialiang Ge [MSFT]
2010-06-15 09:32:54 UTC
Permalink
Hello Timothy

If you were on Windows 7, you can actually try the new trigger start
feature. For example, your service is started only when a USB *storage*
device is inserted. You can find the sample code here:
http://support.microsoft.com/kb/975425/en-us

Regarding 0x42A set by RegisterDeviceNotification, could you please do this
test to confirm that 0x42A was not set by other functions?

SetLastError(0);
hDevNotify = RegisterDeviceNotification(hMainWnd, &NotificationFilter,
DEVICE_NOTIFY_WINDOW_HANDLE);
if (hDevNotify == NULL)
{
printf("%d\n", GetLastError());
}


Regards,
Jialiang Ge
Microsoft Online Community Support

=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
Timothy Jewett
2010-07-23 18:17:35 UTC
Permalink
Sorry for the late response, I no longer received the error when registered
the broadcast. I haven't changed anything except received some updates from
MS. I still do not receive any additional broadcasts though. I ended up
walking though devices and using the filter on handle receive the
notifications that way.

Thanks,
--
Timothy Jewett
Post by Jialiang Ge [MSFT]
Hello Timothy
If you were on Windows 7, you can actually try the new trigger start
feature. For example, your service is started only when a USB *storage*
http://support.microsoft.com/kb/975425/en-us
Regarding 0x42A set by RegisterDeviceNotification, could you please do this
test to confirm that 0x42A was not set by other functions?
SetLastError(0);
hDevNotify = RegisterDeviceNotification(hMainWnd, &NotificationFilter,
DEVICE_NOTIFY_WINDOW_HANDLE);
if (hDevNotify == NULL)
{
printf("%d\n", GetLastError());
}
Regards,
Jialiang Ge
Microsoft Online Community Support
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
.
Loading...