From: Sushma on
Hello All,

I am working on WIN 32 Application which gets USB device
arrival/removal notification. I used WM_DEVICECHANGE message in
WndProc() function.

I implemented the DoRegisterDeviceInterface() function when device is
opened using CreateFile():

BOOL DoRegisterDeviceInterface(
HDEVNOTIFY *hDevNotify
)
{
DEV_BROADCAST_HANDLE NotificationFilter;
DWORD Err;
TCHAR msg[MAX_PATH];

ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
NotificationFilter.dbch_size =
sizeof(DEV_BROADCAST_HANDLE);
NotificationFilter.dbch_devicetype = DBT_DEVTYP_HANDLE;
NotificationFilter.dbch_handle = hDevice;

// Register handle based notification to receive pnp
// device change notification on the handle.
*hDevNotify = RegisterDeviceNotification( m_hWnd,
&NotificationFilter,
DEVICE_NOTIFY_WINDOW_HANDLE
);

if(!*hDevNotify)
{
Err = GetLastError();
_stprintf(msg, "RegisterDeviceNotification failed: %lx.\n", Err);
MessageBox(0, msg, 0, 0);
return FALSE;
}

return TRUE;
}

WndProc(...)
{
case DEVICECHANGE:
{
if((wParam & 0xC000) == 0x8000)
{
if (pBroadcasthdr->dbch_devicetype ==
DBT_DEVTYP_DEVICEINTERFACE)
{
HandleDeviceInterfaceChange(hWnd, nEventType);
} else if (pBroadcasthdr->dbch_devicetype ==
DBT_DEVTYP_HANDLE) {
HandleDeviceChange(hWnd, nEventType);
}
}
break;
}
}

The wparam is always 0x0007, which notifies of DBT_DEVNODES_CHANGED
event only. i never get DBT_DEVICEARRIVAL or DBT_DEVICEREMOVECOMPLETE
notification.

I get the correct notification of Device Arrival and Removal when a CD
is placed or removed in CDROM drive but not for USB device.

Suggest me if im going wrong in my code.

Thank You,

Regards,
Sushma

From: Philipp Ineichen on
Hi

The following RegisterDeviceNotification function works perfect for me. I
mean I get all Arrival and Removal message for all INPUT devices. I test
it with my Logitech mouse.

BOOL RegisterForDevChange(HWND hDlg, PVOID *hNotifyDevNode)
{
BOOL bRet;

DEV_BROADCAST_DEVICEINTERFACE *pFilterData = new
(DEV_BROADCAST_DEVICEINTERFACE);

ZeroMemory(pFilterData, sizeof(DEV_BROADCAST_DEVICEINTERFACE));

pFilterData->dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
pFilterData->dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
pFilterData->dbcc_classguid = GUID_CLASS_INPUT;

*hNotifyDevNode = RegisterDeviceNotification(hDlg, pFilterData,
DEVICE_NOTIFY_WINDOW_HANDLE);

if(*hNotifyDevNode)
{
bRet = TRUE;
}
else
{
if (pFilterData)
delete (pFilterData);

bRet = FALSE;
}

return bRet;
}


LRESULT CALLBACK MainDlgProc(HWND hDlg, UINT message, WPARAM wParam,
LPARAM lParam)
{

:
:
:

case WM_DEVICECHANGE:

switch ((UINT)wParam)
{
case DBT_DEVICEARRIVAL:
SendMessage(hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Device Arrival");
break;

case DBT_DEVICEQUERYREMOVE:
SendMessage(hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Device Query
Remove");
break;

case DBT_DEVICEREMOVECOMPLETE:
SendMessage(hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Device Remove
Complete");
break;
}
break;

:
:
:
}

- PI
--------------------
Philipp Ineichen
www.skagitsystems.ch
From: Sushma on
Hi Philipp,

Thanks for your code. It works now but only question i have now is i
get the arrival/removal notification for my device and the control
enters into DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE statements
also, but it happens to enter more than once when i plugin or remove
the device.

To verify this i put a debug out put message in these statements, the
debug message appears more than once.

Could you tell me why this is happening? Do i need to handle anything
else in my code?

Thank You.

Regards,
Sushma

Philipp Ineichen wrote:
> Hi
>
> The following RegisterDeviceNotification function works perfect for me. I
> mean I get all Arrival and Removal message for all INPUT devices. I test
> it with my Logitech mouse.
>
> BOOL RegisterForDevChange(HWND hDlg, PVOID *hNotifyDevNode)
> {
> BOOL bRet;
>
> DEV_BROADCAST_DEVICEINTERFACE *pFilterData = new
> (DEV_BROADCAST_DEVICEINTERFACE);
>
> ZeroMemory(pFilterData, sizeof(DEV_BROADCAST_DEVICEINTERFACE));
>
> pFilterData->dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
> pFilterData->dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
> pFilterData->dbcc_classguid = GUID_CLASS_INPUT;
>
> *hNotifyDevNode = RegisterDeviceNotification(hDlg, pFilterData,
> DEVICE_NOTIFY_WINDOW_HANDLE);
>
> if(*hNotifyDevNode)
> {
> bRet = TRUE;
> }
> else
> {
> if (pFilterData)
> delete (pFilterData);
>
> bRet = FALSE;
> }
>
> return bRet;
> }
>
>
> LRESULT CALLBACK MainDlgProc(HWND hDlg, UINT message, WPARAM wParam,
> LPARAM lParam)
> {
>
> :
> :
> :
>
> case WM_DEVICECHANGE:
>
> switch ((UINT)wParam)
> {
> case DBT_DEVICEARRIVAL:
> SendMessage(hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Device Arrival");
> break;
>
> case DBT_DEVICEQUERYREMOVE:
> SendMessage(hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Device Query
> Remove");
> break;
>
> case DBT_DEVICEREMOVECOMPLETE:
> SendMessage(hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Device Remove
> Complete");
> break;
> }
> break;
>
> :
> :
> :
> }
>
> - PI
> --------------------
> Philipp Ineichen
> www.skagitsystems.ch

From: hs2 on
Sushma wrote:
> Hi Philipp,
>
> Thanks for your code. It works now but only question i have now is i
> get the arrival/removal notification for my device and the control
> enters into DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE statements
> also, but it happens to enter more than once when i plugin or remove
> the device.
>
> To verify this i put a debug out put message in these statements, the
> debug message appears more than once.
>
> Could you tell me why this is happening? Do i need to handle anything
> else in my code?
>
> Thank You.
>
> Regards,
> Sushma
>
> Philipp Ineichen wrote:
>> Hi
>>
>> The following RegisterDeviceNotification function works perfect for me. I
>> mean I get all Arrival and Removal message for all INPUT devices. I test
>> it with my Logitech mouse.
>>
>> BOOL RegisterForDevChange(HWND hDlg, PVOID *hNotifyDevNode)
>> {
>> BOOL bRet;
>>
>> DEV_BROADCAST_DEVICEINTERFACE *pFilterData = new
>> (DEV_BROADCAST_DEVICEINTERFACE);
>>
>> ZeroMemory(pFilterData, sizeof(DEV_BROADCAST_DEVICEINTERFACE));
>>
>> pFilterData->dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
>> pFilterData->dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
>> pFilterData->dbcc_classguid = GUID_CLASS_INPUT;
>>
>> *hNotifyDevNode = RegisterDeviceNotification(hDlg, pFilterData,
>> DEVICE_NOTIFY_WINDOW_HANDLE);
>>
>> if(*hNotifyDevNode)
>> {
>> bRet = TRUE;
>> }
>> else
>> {
>> if (pFilterData)
>> delete (pFilterData);
>>
>> bRet = FALSE;
>> }
>>
>> return bRet;
>> }
>>
>>
>> LRESULT CALLBACK MainDlgProc(HWND hDlg, UINT message, WPARAM wParam,
>> LPARAM lParam)
>> {
>>
>> :
>> :
>> :
>>
>> case WM_DEVICECHANGE:
>>
>> switch ((UINT)wParam)
>> {
>> case DBT_DEVICEARRIVAL:
>> SendMessage(hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Device Arrival");
>> break;
>>
>> case DBT_DEVICEQUERYREMOVE:
>> SendMessage(hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Device Query
>> Remove");
>> break;
>>
>> case DBT_DEVICEREMOVECOMPLETE:
>> SendMessage(hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Device Remove
>> Complete");
>> break;
>> }
>> break;
>>
>> :
>> :
>> :
>> }
>>
>> - PI
>> --------------------
>> Philipp Ineichen
>> www.skagitsystems.ch
>

Check (DEV_BROADCAST_DEVICEINTERFACE*)lParam)->dbcc_name in e.g. MainDlgProc
Provides the name of the device related to the message.

HS2
From: Nyamagoud on
Hi,
You should refer to the lparam member of the WindowProc. It points to
DEV_BROADCAST_HDR structure, then check its dbch_devicetype member to
determine the device type. This way you can handle the request which you are
interested in and ignore the other. r they same in both the case?


"Sushma" wrote:

> Hi Philipp,
>
> Thanks for your code. It works now but only question i have now is i
> get the arrival/removal notification for my device and the control
> enters into DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE statements
> also, but it happens to enter more than once when i plugin or remove
> the device.
>
> To verify this i put a debug out put message in these statements, the
> debug message appears more than once.
>
> Could you tell me why this is happening? Do i need to handle anything
> else in my code?
>
> Thank You.
>
> Regards,
> Sushma
>
> Philipp Ineichen wrote:
> > Hi
> >
> > The following RegisterDeviceNotification function works perfect for me. I
> > mean I get all Arrival and Removal message for all INPUT devices. I test
> > it with my Logitech mouse.
> >
> > BOOL RegisterForDevChange(HWND hDlg, PVOID *hNotifyDevNode)
> > {
> > BOOL bRet;
> >
> > DEV_BROADCAST_DEVICEINTERFACE *pFilterData = new
> > (DEV_BROADCAST_DEVICEINTERFACE);
> >
> > ZeroMemory(pFilterData, sizeof(DEV_BROADCAST_DEVICEINTERFACE));
> >
> > pFilterData->dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
> > pFilterData->dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
> > pFilterData->dbcc_classguid = GUID_CLASS_INPUT;
> >
> > *hNotifyDevNode = RegisterDeviceNotification(hDlg, pFilterData,
> > DEVICE_NOTIFY_WINDOW_HANDLE);
> >
> > if(*hNotifyDevNode)
> > {
> > bRet = TRUE;
> > }
> > else
> > {
> > if (pFilterData)
> > delete (pFilterData);
> >
> > bRet = FALSE;
> > }
> >
> > return bRet;
> > }
> >
> >
> > LRESULT CALLBACK MainDlgProc(HWND hDlg, UINT message, WPARAM wParam,
> > LPARAM lParam)
> > {
> >
> > :
> > :
> > :
> >
> > case WM_DEVICECHANGE:
> >
> > switch ((UINT)wParam)
> > {
> > case DBT_DEVICEARRIVAL:
> > SendMessage(hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Device Arrival");
> > break;
> >
> > case DBT_DEVICEQUERYREMOVE:
> > SendMessage(hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Device Query
> > Remove");
> > break;
> >
> > case DBT_DEVICEREMOVECOMPLETE:
> > SendMessage(hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Device Remove
> > Complete");
> > break;
> > }
> > break;
> >
> > :
> > :
> > :
> > }
> >
> > - PI
> > --------------------
> > Philipp Ineichen
> > www.skagitsystems.ch
>
>