-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathds_av_devices.cpp
93 lines (77 loc) · 2.57 KB
/
ds_av_devices.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "ds_av_devices.h"
#if defined(Q_OS_WIN)
#pragma comment(lib, "Strmiids.lib")
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "oleaut32.lib")
int DSGetAVDevices(std::vector<DeviceCtx>& device_ctx_vec, REFGUID guid_value)
{
DeviceCtx name;
HRESULT hr;
// 初始化
device_ctx_vec.clear();
// 初始化COM
hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if (FAILED(hr))
{
return -1;
}
// 创建系统设备枚举器实例
ICreateDevEnum* sys_dev_enum = NULL;
hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (void**) &sys_dev_enum);
if (FAILED(hr))
{
CoUninitialize();
return -1;
}
// 获取设备类枚举器
int ret = -1;
do
{
IEnumMoniker* enum_cat = NULL;
hr = sys_dev_enum->CreateClassEnumerator(guid_value, &enum_cat, 0);
if (hr != S_OK)
{
break;
}
// 枚举设备名称
IMoniker* moniker = NULL;
ULONG fetched;
while (enum_cat->Next(1, &moniker, &fetched) == S_OK)
{
IPropertyBag* prop_bag;
hr = moniker->BindToStorage(NULL, NULL, IID_IPropertyBag, (void**) &prop_bag);
if (SUCCEEDED(hr))
{
// 获取设备友好名
VARIANT var_name;
VariantInit(&var_name);
hr = prop_bag->Read(L"FriendlyName", &var_name, NULL);
if (SUCCEEDED(hr))
{
StringCchCopy(name.FriendlyName, MAX_FRIENDLY_NAME_LENGTH, var_name.bstrVal);
// 获取设备Moniker名
LPOLESTR ole_display_name = reinterpret_cast<LPOLESTR>(CoTaskMemAlloc(MAX_MONIKER_NAME_LENGTH * 2));
if (ole_display_name != NULL)
{
hr = moniker->GetDisplayName(NULL, NULL, &ole_display_name);
if (SUCCEEDED(hr))
{
StringCchCopy(name.MonikerName, MAX_MONIKER_NAME_LENGTH, ole_display_name);
device_ctx_vec.push_back(name);
}
CoTaskMemFree(ole_display_name);
}
}
VariantClear(&var_name);
prop_bag->Release();
}
moniker->Release();
} // End for While
enum_cat->Release();
ret = 0;
} while (0);
sys_dev_enum->Release();
CoUninitialize();
return ret;
}
#endif