关于Windows上声卡插拔检测
使用到什么:
mmdeviceapi.h windows.h
1注册设备状态检测函数
RegisterEndpointNotificationCallback((IMMNotificationclient))
来源:https://blog.csdn.net/std4453/article/details/72902954
#define SAFE_RELEASE(punk) \ if ((punk) != NULL) \ { (punk)->Release(); (punk) = NULL; } #include <windows.h> #include <setupapi.h> #include <initguid.h> #include <mmdeviceapi.h> #include <Functiondiscoverykeys_devpkey.h> #include "iostream" #include <stdio.h> 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; // Private function to print device-friendly name HRESULT _PrintDeviceName(LPCWSTR pwstrId); 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) { cout<<"OnDeviceAdded"<<endl; return S_OK; }; HRESULT STDMETHODCALLTYPE OnDeviceRemoved(LPCWSTR pwstrDeviceId) { cout<<"OnDeviceRemoved"<<endl; return S_OK; } HRESULT STDMETHODCALLTYPE OnDeviceStateChanged( LPCWSTR pwstrDeviceId, DWORD dwNewState) { cout<<"OnDeviceStateChanged"<<endl; return S_OK; } HRESULT STDMETHODCALLTYPE OnPropertyValueChanged( LPCWSTR pwstrDeviceId, const PROPERTYKEY key) { cout<<"OnPropertyValueChanged"<<endl; _PrintDeviceName(pwstrDeviceId); return S_OK; } }; // Given an endpoint ID string, print the friendly device name. HRESULT CMMNotificationClient::_PrintDeviceName(LPCWSTR pwstrId) { HRESULT hr = S_OK; IMMDevice *pDevice = NULL; IPropertyStore *pProps = NULL; PROPVARIANT varString; CoInitialize(NULL); PropVariantInit(&varString); if (m_pEnumerator == NULL) { // Get enumerator for audio endpoint devices. hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (void**)&m_pEnumerator); } if (hr == S_OK) { hr = m_pEnumerator->GetDevice(pwstrId, &pDevice); } if (hr == S_OK) { hr = pDevice->OpenPropertyStore(STGM_READ, &pProps); } if (hr == S_OK) { // Get the endpoint device's friendly-name property. hr = pProps->GetValue(PKEY_Device_FriendlyName, &varString); } printf("----------------------\nDevice name: \"%S\"\n" " Endpoint ID string: \"%S\"\n", (hr == S_OK) ? varString.pwszVal : L"null device", (pwstrId != NULL) ? pwstrId : L"null ID"); PropVariantClear(&varString); SAFE_RELEASE(pProps) SAFE_RELEASE(pDevice) return hr; } int main(int argc, TCHAR* argv[], TCHAR* envp[]) { CMMNotificationClient mmClient; cin.get(); return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
2021-05-07 linux类似系统中编译依赖库出现error trying to exec cc1plus