以下是我PROPVARIANT
使用 WASAPI API 相关函数获取结构的方法:
//Pointer for stored audio stream
IAudioClient *iac = NULL;
//Endpoint device selection
IMMDeviceEnumerator *pEnumerator = NULL;
IMMDevice *pDevice;
IMMDeviceCollection *pCollection = NULL;
CoInitialize(NULL);
hr = CoCreateInstance(
CLSID_MMDeviceEnumerator, NULL,
CLSCTX_ALL, IID_IMMDeviceEnumerator,
(void**)&pEnumerator);
hr = pEnumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &pCollection);
//Create vector of IMMDevices
UINT endpointCount = NULL;
(*pCollection).GetCount(&endpointCount);
std::vector<IMMDevice**> IMMDevicePP; //IMMDevice seems to contain all endpoint devices, so why have a collection here?
for (UINT i = 0; i < (endpointCount); i++)
{
IMMDevice* pp = NULL;
(*pCollection).Item(i, &pp);
IMMDevicePP.assign(1, &pp);
}
UINT IMMDeviceCount = IMMDevicePP.size();
//Enumerate Properties of IMMDevices
std::vector<IPropertyStore*> IMMDeviceProperties;
for (int k = 0; k < IMMDeviceCount; k++) {
IPropertyStore* prop = NULL;
(**IMMDevicePP[k]).OpenPropertyStore(STGM_READ, &prop);
IMMDeviceProperties.assign(1, prop);
}
UINT PropertyStoreCount = IMMDeviceProperties.size();
//Find name property of device
std::vector<PROPVARIANT*> properties;
for (int i = 0; i < PropertyStoreCount; i++) {
DWORD propCount = 1;
HRESULT countResult = (*IMMDeviceProperties[i]).GetCount(&propCount);
if (countResult == S_OK) { }
else {
int x = 5;
}
for (int p = 0; p < propCount; p++) {
PROPERTYKEY key;
HRESULT keyResult = (*IMMDeviceProperties[i]).GetAt(p, &key);
HRESULT getAT;
PROPVARIANT propVari;
HRESULT propVariResult = (*IMMDeviceProperties[i]).GetValue(key, &propVari);
propVari.vt = VT_LPWSTR;
LPWSTR test = propVari.pwszVal;
//char pwszValTest;
//strcpy(&pwszValTest, propVari.pwszVal);
//WCHAR friendlyName = *propVari.pwszVal;
properties.assign(1, &propVari);
}
}
全部HRESULT
返回 S_OK。
PROPVARIANT
乍一看,生成的结构正确呈现。但是,当使用 VS 的属性 watch 进一步检查时,所有字符串类型属性都会返回此问题标题中反映的错误。因此,当我尝试检索包含我的结构pwszVal
属性的音频端点设备的名称时,如下所示:PROPVARIANT
LPWSTR test = propVari.pwszVal;
我无法检索所需的数据。我尝试使用各种转换器方法复制字符串无济于事。我知道这个错误有很多问题,但我似乎无法破解这个错误。
这是文档PROPVARIANT
及其相应的属性:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa380072(v=vs.85).aspx
在本文档中,它声明“PROPVARIANT
成员vt
设置为VT_LPWSTR
” VT_LPWSTR
是一种枚举类型,对应于值 31。而 VT_BLOB 对应于值 65。我的vt
成员被设置为VT_BLOB
or 65 而不是 31 or VT_LPWSTR
。为什么会这样?这与本文档中所述的值相矛盾:
http://msdn.microsoft.com/en-us/library/windows/desktop/dd370812(v=vs.85).aspx
手动设置vt
成员也不会更改/修复字符串读取错误:
propVari.vt = VT_LPWSTR;
这PKEY_Device_FriendlyName
就是我所追求的。非常感谢任何帮助/提示。