stdafx.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // 这段 MFC 示例源代码演示如何使用 MFC Microsoft Office Fluent 用户界面
  2. // ("Fluent UI"),该示例仅作为参考资料提供,
  3. // 用以补充《Microsoft 基础类参考》和
  4. // MFC C++ 库软件随附的相关电子文档。
  5. // 复制、使用或分发 Fluent UI 的许可条款是单独提供的。
  6. // 若要了解有关 Fluent UI 许可计划的详细信息,请访问
  7. // http://msdn.microsoft.com/officeui。
  8. //
  9. // 版权所有 (C) Microsoft Corporation
  10. // 保留所有权利。
  11. // stdafx.cpp : 只包括标准包含文件的源文件
  12. // FieldTestTool.pch 将作为预编译头
  13. // stdafx.obj 将包含预编译类型信息
  14. #include "stdafx.h"
  15. int EnumDevices(std::vector<TString> &vtDevices)
  16. {
  17. ICreateDevEnum *pDevEnum = NULL;
  18. IEnumMoniker *pEnum = NULL;
  19. HRESULT hr = NULL;
  20. CoInitialize(NULL);
  21. hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, reinterpret_cast<void**>(&pDevEnum));
  22. if (SUCCEEDED(hr))
  23. {
  24. hr = pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory,&pEnum,0);
  25. if (hr == S_OK)
  26. {
  27. //枚举捕获设备
  28. IMoniker *pMoniker = NULL;
  29. ULONG cFetched;
  30. while (pEnum->Next(1, &pMoniker, &cFetched) == S_OK)
  31. {
  32. IPropertyBag* pPropBag;
  33. hr = pMoniker->BindToStorage(0,0,IID_IPropertyBag,reinterpret_cast<void**>(&pPropBag));
  34. if (SUCCEEDED(hr))
  35. {
  36. //获取 vid 和 pid
  37. BSTR devicePath = NULL;
  38. hr = pMoniker->GetDisplayName(NULL, NULL, &devicePath);
  39. if (!SUCCEEDED(hr))
  40. {
  41. pMoniker->Release();
  42. continue;
  43. }
  44. wchar_t* lpszDevicePath = _bstr_t(devicePath);
  45. //获取设备名称
  46. VARIANT varName;
  47. varName.vt = VT_BSTR;
  48. VariantInit(&varName);
  49. hr = pPropBag->Read(L"FriendlyName", &varName, 0);
  50. if (SUCCEEDED(hr))
  51. {
  52. TCHAR str[2048];
  53. WideCharToMultiByte(CP_ACP,0,varName.bstrVal, -1, str, 2048, NULL, NULL);
  54. OutputDebugString(_T("新增设备:"));
  55. OutputDebugString(str);
  56. OutputDebugString(_T("\n"));
  57. vtDevices.push_back(str);
  58. SysFreeString(varName.bstrVal);
  59. }
  60. pPropBag->Release();
  61. }
  62. pMoniker->Release();
  63. }
  64. }
  65. }
  66. CoUninitialize();
  67. return 0;
  68. }