从 Windows 获取完整的音频设备名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1429143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the full audio device name from Windows
提问by Jared
Is there a way to get the fullaudio device name in Windows XP and later?
有没有办法在 Windows XP 及更高版本中获取完整的音频设备名称?
I can use MIXERCAPS but the szPname member will limit to 32 characters (including NULL). For an audio device name of "Microphone (High Definition Audio Device)", I only get back "Microphone (High Definition Aud". This is due to MAXPNAMELEN being defined to 32. I have tried redefining it to a larger number to no effect.
我可以使用 MIXERCAPS 但 szPname 成员将限制为 32 个字符(包括 NULL)。对于“麦克风(高清晰度音频设备)”的音频设备名称,我只得到“麦克风(高清晰度音频”)。这是由于 MAXPNAMELEN 被定义为 32。我尝试将其重新定义为更大的数字,但没有效果.
Here is the code I am using:
这是我正在使用的代码:
MIXERCAPS mc;
ZeroMemory( &mc, sizeof(MIXERCAPS) );
mm = mixerGetDevCaps( reinterpret_cast<UINT_PTR>(m_hMixer), &mc, sizeof(MIXERCAPS) );
I saw this question, but it references Vista and later.
我看到了这个问题,但它引用了 Vista 和更高版本。
采纳答案by nielsm
If you use the classic Windows Multimedia interface you probably can't get around the MAXPNAMELEN limitation, since that's compiled into Windows itself.
如果您使用经典的 Windows 多媒体界面,您可能无法绕过 MAXPNAMELEN 限制,因为它已编译到 Windows 本身中。
However you might be able to get the full device name if you use DirectSound instead. The following code is untested but I think it should work.
但是,如果您改用 DirectSound,则可能可以获得完整的设备名称。以下代码未经测试,但我认为它应该可以工作。
BOOL CALLBACK EnumCallback(LPGUID guid, LPCSTR descr, LPCSTR modname, LPVOID ctx)
{
std::vector<std::string> *names = (std::vector<std::string>*)ctx;
names->push_back(std::string(descr));
return TRUE;
}
int main()
{
std::vector<std::string> names;
if (!FAILED(DirectSoundEnumerate(&EnumCallback, &names)))
{
// do stuff
}
}
回答by Blight
Below is my (Delphi) code:
以下是我的(Delphi)代码:
This is using DirectShow/ActiveX, It enumurates DirectSound devices, which include wrapped WaveOut devices as well.
这是使用 DirectShow/ActiveX,它枚举 DirectSound 设备,其中也包括包装的 WaveOut 设备。
procedure EnumAudioDevices;
var
dsCreateDevEnum : ICreateDevEnum;
EnumDevice : IEnumMoniker;
DeviceMoniker : IMoniker;
Data : Integer;
DevicePropBag : IPropertyBag;
DeviceName : OLEVariant;
I : Integer;
begin
// CLSID_CQzFilterClassManager = Entire DirectShow Filter List
If CoCreateInstance(CLSID_SystemDeviceEnum,nil,CLSCTX_INPROC_SERVER,IID_ICreateDevEnum,dsCreateDevEnum) = S_OK then
Begin
If dsCreateDevEnum.CreateClassEnumerator(CLSID_AudioRendererCategory,EnumDevice,0) = S_OK then
Begin
I := 0;
EnumDevice.Reset;
While EnumDevice.Next(1,DeviceMoniker,@Data) = S_OK do
Begin
If DeviceMoniker.BindToStorage(nil,nil,IID_IPropertyBag,DevicePropBag) = NOERROR then
Begin
If DevicePropBag.Read('FriendlyName',DeviceName,nil) = NOERROR then
Begin
// Success
ShowMessage(DeviceName);
Inc(I);
End;
DevicePropBag := nil;
End;
DeviceMoniker := nil;
End;
EnumDevice := nil;
End;
dsCreateDevEnum := nil;
End;
End;