标签: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; }
标签:def devices otp null col imm article [] result
原文地址:https://www.cnblogs.com/happykoukou/p/9167479.html