启用通知

Windows Media 设备管理器 声明应用程序可在 COM 类中实现的四个接口来接收事件通知。 这些接口分为两组,如下表所示。

接口 说明
IWMDMNotification 设备或存储媒体连接或断开连接时通知应用程序。
IWMDMProgress
IWMDMProgress2
IWMDMProgress3
一个非常简单的通知系统,用于提醒应用程序任何事件的进度。 应用程序不需要执行任何操作来响应这些消息。

IWMDMNotification

当即插即用设备与计算机连接或断开连接时,以及即插即用存储媒体 ((如闪存卡) )插入或从设备中删除时,IWMDMNotification 会向应用程序发出警报。 这些通知可帮助应用程序更新其用户界面以反映更改。

为了接收这些通知,应用程序必须使用平台 SDK IConnectionPointContainerIConnectionPoint 接口注册以接收这些通知。 应用程序应注册以在启动时接收事件,并在应用程序关闭时取消注册。 按照以下步骤注册以接收这些通知。

  1. 查询在对 IConnectionPointContainer 的应用程序进行身份验证时收到的 main IWMDeviceManager 接口。
  2. 调用 IConnectionPointContainer::FindConnectionPoint 以检索 IWMDMNotification 接口的容器连接点。
  3. 通过调用 IConnectionPoint::Advise 注册以接收事件。 传入实现 IWMDMNotification 的 类,并检索 Cookie,这是标识连接点的唯一 ID。 它必须存储,稍后用于取消注册事件通知。

以下 C++ 代码演示如何注册以接收来自 IWMDMNotification 的通知。

HRESULT CWMDMController::RegisterForNotifications()
{
    HRESULT hr = S_OK;
    CComPtr<IConnectionPointContainer> pConxnPointCont;
    CComPtr<IConnectionPoint> pIConnPoint;

    // Get the IConnectionPointContainer interface from IWMDeviceManager.
    if (SUCCEEDED (hr = m_IWMDMDeviceMgr->QueryInterface(IID_IConnectionPointContainer, (void**) & pConxnPointCont)))
    {
        // Get a connection point from the container.
        if (SUCCEEDED (hr = pConxnPointCont->FindConnectionPoint(IID_IWMDMNotification, &pIConnPoint)))
        {
            // Add ourselves as a callback handler for the connection point.
            // If we succeeded, indicate that by storing the connection point ID.
            DWORD dwCookie;
            if (SUCCEEDED (hr = pIConnPoint->Advise((IUnknown*)((IWMDMNotification*)this), &dwCookie)))
            {
                m_dwNotificationCookie = dwCookie;
            }
        }
    }

    return hr;
}

应用程序关闭时,必须注销 IConnectionPoint ,以指示它不应再向你发送通知。 按照以下步骤取消注册通知:

  1. 查询 IConnectionPointContainer main IWMDeviceManager 接口。
  2. 获取 IWMDMNotification 接口的连接点。
  3. 通过调用 IConnectionPoint::Unadvise 注销应用程序以获取事件通知,并传入注册以接收事件时收到的唯一 ID。

以下 C++ 代码演示如何在应用程序关闭时取消注册 IWMDMNotification 事件。

HRESULT CWMDMController::UnregisterForNotifications()
{
    HRESULT hr = S_FALSE;

    // On class initialization, we initialized the handle to -1 as a flag 
    // to indicate we had not yet registered for notifications. If registration 
    // never happened, don't bother to unregister.
    if (-1 != m_dwNotificationCookie)
    {
        CComPtr<IConnectionPointContainer> pConxnPointCont;
        CComPtr<IConnectionPoint> pIConnPoint;

        // Get the connection point container from IWMDeviceManager. 
        if (SUCCEEDED (hr = 
           m_IWMDMDeviceMgr->QueryInterface(IID_IConnectionPointContainer,
           (void**) & pConxnPointCont)))
        {
            // Get a connection point from the container.
            if (SUCCEEDED (hr = pConxnPointCont->FindConnectionPoint(IID_IWMDMNotification, &pIConnPoint)))
            {
                // Remove ourselves as a callback from the connection point.
                // If successful, reset the ID to a flag value.
                if (SUCCEEDED (hr = 
                    pIConnPoint->Unadvise(m_dwNotificationCookie)))
                {
                    m_dwNotificationCookie = -1;
                    hr = S_OK;
                }
            }
        }
    }

    return hr;
}

使用 IWMDMProgress

Windows Media 设备管理器可以在发生特定操作(如内容传输、安全时钟获取和遇到 DRM 文件信息)时发送应用程序状态消息。 应用程序可以使用这些消息来监视事件的状态或取消事件。 若要使用此接口,请实现 IWMDMProgressIWMDMProgress2IWMDMProgress3,并将其作为参数传递给将接受进度消息的方法。 请注意, IWMDMProgress3 是高级接口,因为它提供标识 GUID 来指定要跟踪的操作。 以下应用程序方法接受进度接口 (相应的服务提供程序方法应能够将通知发送到提交的接口) :

IWMDMStorageControl::D elete

IWMDMStorageControl::Insert

IWMDMStorageControl::Move

IWMDMStorageControl::Read

IWMDMStorageControl::Rename

IWMDMStorageControl2::Insert2

IWMDMStorageControl3::Insert3

IWMDMStorageGlobals::Initialize

IWMDRMDeviceApp::AcquireDeviceData

这些方法的文档提供了将接口传递到方法的示例。 有关实现回调接口的示例,请参阅 IWMDMProgress、IWMDMProgress2IWMDMProgress3 方法的文档。

创建 Windows Media 设备管理器 应用程序