B2C2, Inc. - Software Development Kit

Plug-in/Removal Device Notification (Windows Only)

Receiving USB Device Plug In/Removal Notifications

In Windows, device management provides a way to notify applications when new resources become available or unavailable. This is useful for supporting USB Plug and Play. The Windows system broadcasts the WM_DEVICECHANGE message. The first argument passed to the message handler indicates the event type such as a DBT_DEVICEARRIVAL or DBT_DEVICEREMOVALCOMPLETE device event. Depending on the event type, the second argument can be used to determine which device's status has changed. In order to receive the WM_DEVICECHANGE message the receiving window must call RegisterDeviceNotification; it must call UnregisterDeviceNotification when closing the window. For more information on WM_DEVICECHANGE see the applicable Windows documentation.

Device Plug-in/Removal Notification Setup and Usage

To build an application that uses B2C2MPEG2Adapter's device notification support, perform the following:

The B2C2MPEG2Adapter class encapsulates the required Windows call. To use Device Notifications the application must perform the following call:

Device Plug-in/Removal Notifications Sample Code

Note: Device notification is not available for Windows 98 or Windows Me. Applications using device notifications can be run on Windows 98 and Me without recompiling, but will return an error at the RegisterDeviceNotification call.

For further information, see the TunerCtrl code sample.

Required Windows NT DDK library:

cfgmgr32.lib

At notification receiving Window class header file:

Declare the function OnDeviceChange(WPARAM wChangeEvent, LPARAM lData) as follows.

Insert

afx_msg void OnDeviceChange(WPARAM wChangeEvent, LPARAM lData);

before

DECLARE_MESSAGE_MAP()

At notification receiving Window class implementation file:

In the message map, map the WM_DEVICECHANGE message to the previously declared OnDeviceChange function as follows.

Insert

ON_MESSAGE(WM_DEVICECHANGE, OnDeviceChange)

before

END_MESSAGE_MAP()

Initialization code:

BOOL CNotificationReceiverWnd::OnInitDialog()
{
    ...

    // Create and initialize B2C2MPEG2Adapter object

    ...

    if( !m_pAdapter->IsInitialized())
    {
        VERIFY( m_pAdapter->RegisterDeviceNotification( GetSafeHwnd()));
    }

    ...
} 
Uninitalization code: 

Unregister device notification before destroying the window as follows.

BOOL CNotificationReceiverWnd::DestroyWindow() 
{
    if( m_pAdapter)
    {
        m_pAdapter->UnregisterDeviceNotification();
    }
}

Device notification message handler:

If the adapter is not initialized and the broadband device is plugged in, use the device plug-in handling code. If the adapter is initialized and the broadband device is removed, use the device removal handling code.

void CNotificationReceiverWnd::OnDeviceChange(WPARAM wChangeEvent, LPARAM lData)
{
    if (!m_pAdapter)
    {
        return;
    }
 
    if (   !m_pAdapter->IsInitialized()                // Device already detected?
        && m_pAdapter->IsDeviceArrival(wChangeEvent, lData) )
    {
        // Device plug-in handling code
                ...
    }
 
    if (   m_pAdapter->IsInitialized()
        && m_pAdapter->IsDeviceRemoveComplete(wChangeEvent, lData))
    {
        // Device removal handling code
                ...
    }
}



Copyright © 1998-2002 B2C2, Inc. Proprietary and Confidential