-
October 26th, 2009, 11:59 PM
#1
WM_DEVICECHANGE not working in Vista
I use WM_DEVICECHANGE to detect when a USB Key is inserted or removed.
Code:
BOOL PreTranslateMessage(MSG* pMsg)
{
// USB key plugged/unplugged?
if((pMsg->message == WM_DEVICECHANGE) || (pMsg->message == DBT_DEVNODES_CHANGED))
{
// Refresh
EnumerateDrives();
}
}
This works perfectly in Windows XP, but doesn't work in Vista.
So I did a lot of googling and codeguruing, and decided I needed to add:
Code:
// <Usbiodef.h> doesn't exist in VS2005, so define my own
static /*const*/ 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(NotificationFilter);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_reserved = 0;
NotificationFilter.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE;
HDEVNOTIFY hDevNotify = RegisterDeviceNotification(this->m_hWnd, &NotificationFilter, DEVICE_NOTIFY_SERVICE_HANDLE);
But it still wont detect when a USB key is added in Vista (it still works in XP)
What am I doing wrong?
-
October 27th, 2009, 02:46 AM
#2
Re: WM_DEVICECHANGE not working in Vista
-
May 12th, 2010, 04:32 AM
#3
Re: WM_DEVICECHANGE not working in Vista
Hi, thanks for the info.
I added RegisterDeviceNotification() exactly like in that article, but I still dont receive WM_DEVICECHANGE
The RegisterDeviceNotification() function was successful, I disabled my Antivirus and firewall, still nothing.
-
May 12th, 2010, 05:37 AM
#4
Re: WM_DEVICECHANGE not working in Vista
This is driving me nuts lol
I downloaded the HWDetect article sample from here: http://www.codeproject.com/KB/system/HwDetect.aspx
I recompiled the source in Visual Studio 2005 SP1 (because the binary downloaded gave an MFC7 dll error), and it works perfectly! I plug in my USB key and HWDetect displays the device.
So I thought... if I copy that code in my own app, it should work! No - it still doesnt work.
Then I created a fresh new VS2005 MFC Dialog App, and copied the code from HWDetectDlg.cpp/h... it still doesn't work!
Here is the code of my new MFC App:
My Header File
Code:
#include <Dbt.h>
#include <setupapi.h>
static const GUID GUID_DEVINTERFACE_LIST[] =
{
// GUID_DEVINTERFACE_USB_DEVICE
{ 0xA5DCBF10, 0x6530, 0x11D2, { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } },
// GUID_DEVINTERFACE_DISK
{ 0x53f56307, 0xb6bf, 0x11d0, { 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b } },
// GUID_DEVINTERFACE_HID,
{ 0x4D1E55B2, 0xF16F, 0x11CF, { 0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 } },
// GUID_NDIS_LAN_CLASS
{ 0xad498944, 0x762f, 0x11d0, { 0x8d, 0xcb, 0x00, 0xc0, 0x4f, 0xc3, 0x35, 0x8c } }
//// GUID_DEVINTERFACE_COMPORT
//{ 0x86e0d1e0, 0x8089, 0x11d0, { 0x9c, 0xe4, 0x08, 0x00, 0x3e, 0x30, 0x1f, 0x73 } },
//// GUID_DEVINTERFACE_SERENUM_BUS_ENUMERATOR
//{ 0x4D36E978, 0xE325, 0x11CE, { 0xBF, 0xC1, 0x08, 0x00, 0x2B, 0xE1, 0x03, 0x18 } },
//// GUID_DEVINTERFACE_PARALLEL
//{ 0x97F76EF0, 0xF883, 0x11D0, { 0xAF, 0x1F, 0x00, 0x00, 0xF8, 0x00, 0x84, 0x5C } },
//// GUID_DEVINTERFACE_PARCLASS
//{ 0x811FC6A5, 0xF728, 0x11D0, { 0xA5, 0x37, 0x00, 0x00, 0xF8, 0x75, 0x3E, 0xD1 } }
};
My Source File (OnInitDialog):
Code:
HDEVNOTIFY hDevNotify;
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
for(int i=0; i<sizeof(GUID_DEVINTERFACE_LIST)/sizeof(GUID); i++)
{
NotificationFilter.dbcc_classguid = GUID_DEVINTERFACE_LIST[i];
hDevNotify = RegisterDeviceNotification(this->GetSafeHwnd(), &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
if( !hDevNotify )
{
AfxMessageBox(CString("Can't register device notification ") , MB_ICONEXCLAMATION);
return FALSE;
}
}
I added PreTranslateMsg:
Code:
BOOL CWM_DEVDlg::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == WM_DEVICECHANGE)
{
AfxMessageBox("***** WM_DEVICECHANGE *****");
}
return CDialog::PreTranslateMessage(pMsg);
}
The RegisterDeviceNotification() function is successful, but my App simply doesn't receive WM_DEVICECHANGE. (It receives all other normal messages, so PreTranslateMessage is working)
I am absolutely clueless why it doesn't work
Can I upload my MFC app somewhere so you can have a look?
(I am using Windows 7 Pro)
Last edited by Anarchi; May 12th, 2010 at 05:46 AM.
-
May 12th, 2010, 07:11 AM
#5
Re: WM_DEVICECHANGE not working in Vista
Finally got it working!
PreTranslateMessage was not receiving WM_DEVICECHANGE (but all other messages appear to work fine).
I noticed HWDetect wasn't using PreTranslateMessage, so I used the ON_MESSAGE technique instead.
Code:
cpp..
BEGIN_MESSAGE_MAP
ON_MESSAGE(WM_DEVICECHANGE, OnMyDeviceChange)
h..
afx_msg LRESULT OnMyDeviceChange(WPARAM wParam, LPARAM lParam);
WM_DEVICECHANGE in PreTranslateMessage works in XP, why not in Win7?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|