码迷,mamicode.com
首页 > Windows程序 > 详细

windows7下检测耳机麦克拔插(转)

时间:2018-06-11 17:15:30      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:def   devices   otp   null   col   imm   article   []   result   

原文转自 https://blog.csdn.net/rankun1/article/details/50972990

 

#include "stdafx.h"


#define SAFE_RELEASE(punk)  if ((punk) != NULL)  { (punk)->Release(); (punk) = NULL; }

#include <mmdeviceapi.h>  
#include "iostream"  
using namespace std;


class CMMNotificationClient : public IMMNotificationClient
{
public:
    IMMDeviceEnumerator *m_pEnumerator;
    CMMNotificationClient() :
        _cRef(1),
        m_pEnumerator(NULL)
    {
        //初始化COM  
        ::CoInitialize(NULL);
        HRESULT hr = S_OK;

        //创建接口  
        hr = CoCreateInstance(
            __uuidof(MMDeviceEnumerator), NULL,
            CLSCTX_ALL, __uuidof(IMMDeviceEnumerator),
            (void**)&m_pEnumerator);

        if (hr == S_OK)
        {
            cout << "接口创建成功" << endl;
        }
        else
        {
            cout << "接口创建失败" << endl;
        }
        //注册事件  
        hr = m_pEnumerator->RegisterEndpointNotificationCallback((IMMNotificationClient*)this);
        if (hr == S_OK)
        {
            cout << "注册成功" << endl;
        }
        else
        {
            cout << "注册失败" << endl;
        }
    }

    ~CMMNotificationClient()
    {
        SAFE_RELEASE(m_pEnumerator)
            ::CoUninitialize();
    }


    // IUnknown methods -- AddRef, Release, and QueryInterface   
private:
    LONG _cRef;
    ULONG STDMETHODCALLTYPE AddRef()
    {
        return InterlockedIncrement(&_cRef);
    }

    ULONG STDMETHODCALLTYPE Release()
    {
        ULONG ulRef = InterlockedDecrement(&_cRef);
        if (0 == ulRef)
        {
            delete this;
        }
        return ulRef;
    }

    HRESULT STDMETHODCALLTYPE QueryInterface(
        REFIID riid, VOID **ppvInterface)
    {
        if (IID_IUnknown == riid)
        {
            AddRef();
            *ppvInterface = (IUnknown*)this;
        }
        else if (__uuidof(IMMNotificationClient) == riid)
        {
            AddRef();
            *ppvInterface = (IMMNotificationClient*)this;
        }
        else
        {
            *ppvInterface = NULL;
            return E_NOINTERFACE;
        }
        return S_OK;
    }


    HRESULT STDMETHODCALLTYPE OnDefaultDeviceChanged(
        EDataFlow flow, ERole role,
        LPCWSTR pwstrDeviceId)
    {
        //cout<<"OnDefaultDeviceChanged"<<endl;   
        return S_OK;
    }

    HRESULT STDMETHODCALLTYPE OnDeviceAdded(LPCWSTR pwstrDeviceId)
    {
        return S_OK;
    };

    HRESULT STDMETHODCALLTYPE OnDeviceRemoved(LPCWSTR pwstrDeviceId)
    {

        return S_OK;
    }

    HRESULT STDMETHODCALLTYPE OnDeviceStateChanged(
        LPCWSTR pwstrDeviceId,
        DWORD dwNewState)
    {
        switch (dwNewState)
        {
        case DEVICE_STATE_ACTIVE:
            cout << "监测到可用的麦克风。" << endl;
            break;

        case DEVICE_STATE_DISABLED:
            cout << "监测到麦克风不可用。" << endl;
            break;

        case DEVICE_STATE_NOTPRESENT:
            cout << "监测到麦克风未准备好。" << endl;
            break;

        case DEVICE_STATE_UNPLUGGED:
            cout << "监测到麦克风被拔出。" << endl;
            break;

        default:
            break;
        }

        return S_OK;
    }

    HRESULT STDMETHODCALLTYPE OnPropertyValueChanged(
        LPCWSTR pwstrDeviceId,
        const PROPERTYKEY key)
    {

        return S_OK;
    }
};


int main(int argc, TCHAR* argv[], TCHAR* envp[])
{
    CMMNotificationClient mmClient;

    system("pause");
    return 0;
}

 

windows7下检测耳机麦克拔插(转)

标签:def   devices   otp   null   col   imm   article   []   result   

原文地址:https://www.cnblogs.com/happykoukou/p/9167479.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!