windows7下检测耳机麦克拔插

来源:互联网 发布:windows xp cd vl 编辑:程序博客网 时间:2024/06/09 19:06
#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) { cout<<"OnDeviceStateChanged"<<endl; 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; } 
参考微软文档https://msdn.microsoft.com/en-us/library/windows/desktop/dd370810(v=vs.85).aspx
0 0
原创粉丝点击