zoukankan      html  css  js  c++  java
  • windows7下检测耳机麦克拔插(转)

    原文转自 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;
    }
    
  • 相关阅读:
    go语言入门(3)运算符及流程控制
    go语言入门(2)数据类型
    go语言入门(1)
    ubuntu上软件下载慢,github下载慢
    密码基础知识(2)以RSA为例说明加密、解密、签名、验签
    让你减少焦虑的一首英文小诗
    使用脚本启动fabric时出错
    Hyperledger Fabric(5)ChainCode的编写步骤
    Hyperledger Fabric(4)链码ChainCode
    设计题专题总结
  • 原文地址:https://www.cnblogs.com/happykoukou/p/9167479.html
Copyright © 2011-2022 走看看