Windows系统获取音频设备属性信息
2022-09-02
638
0
#include <iostream>
#include <iostream>
#include <windows.h>
#include <Audioclient.h>
#include <MMSystem.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <Functiondiscoverykeys_devpkey.h>//PKEY_Device_FriendlyName
//-----------------------------------------------------------
// Record an audio stream from the default audio capture
// device. The RecordAudioStream function allocates a shared
// buffer big enough to hold one second of PCM audio data.
// The function uses this buffer to stream data from the
// capture device. The main loop runs every 1/2 second.
//-----------------------------------------------------------
// REFERENCE_TIME time units per second and per millisecond
#define REFTIMES_PER_SEC 10000000
#define REFTIMES_PER_MILLISEC 10000 //100ns
//以下代码来自MSDN
#define EXIT_ON_ERROR(hres) \
if (FAILED(hres)) { goto Exit; }
#define SAFE_RELEASE(punk) \
if ((punk) != NULL) \
{ (punk)->Release(); (punk) = NULL; }
const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
void PrintEndpointNames()
{
HRESULT hr = S_OK;
IMMDeviceEnumerator* pEnumerator = NULL;
IMMDeviceCollection* pCollection = NULL;
IMMDevice* pEndpoint = NULL;
IPropertyStore* pProps = NULL;
LPWSTR pwszID = NULL;
hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL,CLSCTX_ALL, IID_IMMDeviceEnumerator,(void**)&pEnumerator);
if (FAILED(hr))
{
goto Exit;
}
hr = pEnumerator->EnumAudioEndpoints( eAll, DEVICE_STATE_ACTIVE,&pCollection);
if (FAILED(hr))
{
goto Exit;
}
UINT count;
hr = pCollection->GetCount(&count);
if (FAILED(hr))
{
goto Exit;
}
if (count == 0)
{
printf("No endpoints found.\n");
}
for (ULONG i = 0; i < count; i++)
{
hr = pCollection->Item(i, &pEndpoint);
if (FAILED(hr))
{
goto Exit;
}
hr = pEndpoint->GetId(&pwszID);
if (FAILED(hr))
{
goto Exit;
}
hr = pEndpoint->OpenPropertyStore(STGM_READ, &pProps);
if (FAILED(hr))
{
goto Exit;
}
PROPVARIANT varName;
PropVariantInit(&varName);
hr = pProps->GetValue(PKEY_Device_FriendlyName, &varName);
if (FAILED(hr))
{
goto Exit;
}
printf("Endpoint %d: \"%S\" (%S)\n", i, varName.pwszVal, pwszID);
CoTaskMemFree(pwszID);
pwszID = NULL;
PropVariantClear(&varName);
if (pProps != NULL)
{
pProps->Release();
pProps = NULL;
}
if (pEndpoint != NULL)
{
pEndpoint->Release();
pEndpoint = NULL;
}
}
if (pEnumerator != NULL)
{
pEnumerator->Release();
pEnumerator = NULL;
}
if (pCollection != NULL)
{
pCollection->Release();
pCollection = NULL;
}
return;
Exit:
printf("Error!\n");
CoTaskMemFree(pwszID);
SAFE_RELEASE(pEnumerator);
SAFE_RELEASE(pCollection);
SAFE_RELEASE(pEndpoint);
SAFE_RELEASE(pProps);
}
int main()
{
CoInitialize(NULL);
PrintEndpointNames();
CoUninitialize();
return getchar();
}
可获取的设备属性信息包括:
头文件定义路径为:
C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um\functiondiscoverykeys_devpkey.h
内容为:
PKEY_Device_DeviceDesc,
PKEY_Device_HardwareIds,
PKEY_Device_CompatibleIds,
PKEY_Device_Service,
PKEY_Device_Class,
PKEY_Device_ClassGuid,
PKEY_Device_Driver,
PKEY_Device_ConfigFlags,
PKEY_Device_Manufacturer,
PKEY_Device_FriendlyName,
PKEY_Device_LocationInfo,
PKEY_Device_PDOName,
PKEY_Device_Capabilities,
PKEY_Device_UINumber,
PKEY_Device_UpperFilters,
PKEY_Device_LowerFilters,
PKEY_Device_BusTypeGuid,
PKEY_Device_LegacyBusType,
PKEY_Device_BusNumber,
PKEY_Device_EnumeratorName,
PKEY_Device_Security,
PKEY_Device_SecuritySDS,
PKEY_Device_DevType,
PKEY_Device_Exclusive,
PKEY_Device_Characteristics,
PKEY_Device_Address,
PKEY_Device_UINumberDescFormat,
PKEY_Device_PowerData,
PKEY_Device_RemovalPolicy,
PKEY_Device_RemovalPolicyDefault,
PKEY_Device_RemovalPolicyOverride,
PKEY_Device_InstallState,
PKEY_Device_LocationPaths,
PKEY_Device_BaseContainerId,
PKEY_Device_DevNodeStatus,
PKEY_Device_ProblemCode,
PKEY_Device_EjectionRelations,
PKEY_Device_RemovalRelations,
PKEY_Device_PowerRelations,
PKEY_Device_BusRelations,
PKEY_Device_Parent,
PKEY_Device_Children,
PKEY_Device_Siblings,
PKEY_Device_TransportRelations,
PKEY_Device_Reported,
PKEY_Device_Legacy,
PKEY_Device_InstanceId,
PKEY_Device_ContainerId,
PKEY_Device_ModelId,
PKEY_Device_FriendlyNameAttributes,
PKEY_Device_ManufacturerAttributes,
PKEY_Device_PresenceNotForDevice,
PKEY_Device_SignalStrength,
PKEY_Device_IsAssociateableByUserAc
PKEY_Numa_Proximity_Domain,
PKEY_Device_DHP_Rebalance_Policy,
PKEY_Device_Numa_Node,
PKEY_Device_BusReportedDeviceDesc,
PKEY_Device_InstallInProgress,
PKEY_Device_DriverDate,
PKEY_Device_DriverVersion,
PKEY_Device_DriverDesc,
PKEY_Device_DriverInfPath,
PKEY_Device_DriverInfSection,
PKEY_Device_DriverInfSectionExt,
PKEY_Device_MatchingDeviceId,
PKEY_Device_DriverProvider,
PKEY_Device_DriverPropPageProvider,
PKEY_Device_DriverCoInstallers,
PKEY_Device_ResourcePickerTags,
PKEY_Device_ResourcePickerException
PKEY_Device_DriverRank,
PKEY_Device_DriverLogoLevel,
PKEY_Device_NoConnectSound,
PKEY_Device_GenericDriverInstalled,
PKEY_Device_AdditionalSoftwareReque
PKEY_Device_SafeRemovalRequired,
PKEY_Device_SafeRemovalRequiredOver
PKEY_DrvPkg_Model,
PKEY_DrvPkg_VendorWebSite,
PKEY_DrvPkg_DetailedDescription,
PKEY_DrvPkg_DocumentationLink,
PKEY_DrvPkg_Icon,
PKEY_DrvPkg_BrandingIcon,
PKEY_DeviceClass_UpperFilters,
PKEY_DeviceClass_LowerFilters,
PKEY_DeviceClass_Security,
PKEY_DeviceClass_SecuritySDS,
PKEY_DeviceClass_DevType,
PKEY_DeviceClass_Exclusive,
PKEY_DeviceClass_Characteristics,
PKEY_DeviceClass_Name,
PKEY_DeviceClass_ClassName,
PKEY_DeviceClass_Icon,
PKEY_DeviceClass_ClassInstaller,
PKEY_DeviceClass_PropPageProvider,
PKEY_DeviceClass_NoInstallClass,
PKEY_DeviceClass_NoDisplayClass,
PKEY_DeviceClass_SilentInstall,
PKEY_DeviceClass_NoUseClass,
PKEY_DeviceClass_DefaultService,
PKEY_DeviceClass_IconPath,
PKEY_DeviceClass_ClassCoInstallers,
PKEY_DeviceInterface_FriendlyName,
PKEY_DeviceInterface_Enabled,
PKEY_DeviceInterface_ClassGuid,
HID人机交互QQ群:564808376
UAC音频QQ群:218581009
UVC相机QQ群:331552032
BOT&UASP大容量存储QQ群:258159197
STC-USB单片机QQ群:315457461
USB技术交流QQ群2:580684376
USB技术交流QQ群:952873936