EnumDevice.cpp
上传用户:bxq2008bxq
上传日期:2022-07-18
资源大小:6138k
文件大小:2k
源码类别:

DirextX编程

开发平台:

Visual C++

  1. #include "StdAfx.h"
  2. #include "EnumDevice.h"
  3. CDeviceName::CDeviceName(void)
  4. :m_FriendName(_T(""))
  5. {
  6. }
  7. CDeviceName::~CDeviceName(void)
  8. {
  9. }
  10. CEnumDevice::CEnumDevice(void)
  11. {
  12. }
  13. CEnumDevice::~CEnumDevice(void)
  14. {
  15. }
  16. HRESULT  CEnumDevice::EnumDevice(void)
  17. {
  18. ICreateDevEnum *pDevEnum = NULL;
  19. IEnumMoniker *pEnum = NULL;
  20. IMoniker *pMoniker = NULL;
  21. // Create the System Device Enumerator.
  22. HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL,
  23. CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, 
  24. reinterpret_cast<void**>(&pDevEnum));
  25. if (SUCCEEDED(hr))
  26. {
  27. // Create an enumerator for the video capture category.
  28. hr = pDevEnum->CreateClassEnumerator(
  29. CLSID_VideoInputDeviceCategory,
  30. &pEnum, 0);
  31. }
  32. if(SUCCEEDED(hr) && hr==S_OK)
  33. {
  34. //HWND hList; // Handle to the list box.
  35. while (pEnum->Next(1, &pMoniker, NULL) == S_OK)
  36. {
  37. IBaseFilter *pCap = NULL;
  38. IPropertyBag *pPropBag;
  39. hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag, 
  40. (void**)(&pPropBag));
  41. if (FAILED(hr))
  42. {
  43. pMoniker->Release();
  44. continue;  // Skip this one, maybe the next one will work.
  45. // Find the description or friendly name.
  46. VARIANT varName;
  47. VariantInit(&varName);
  48. hr = pPropBag->Read(L"FriendlyName", &varName, 0);
  49. if (FAILED(hr))
  50. {
  51. hr = pPropBag->Read(L"Description", &varName, 0);
  52. }
  53. if (SUCCEEDED(hr))
  54. {
  55. // Add it 
  56. hr = pMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&pCap);
  57. if (SUCCEEDED(hr))
  58. {
  59. CDeviceName *DeviceName = new CDeviceName();
  60. USES_CONVERSION;
  61. CString str(OLE2T(varName.bstrVal));
  62. DeviceName->m_FriendName = str; 
  63. DeviceName->pSrc = pCap;
  64. outlist.Add(DeviceName);
  65. }
  66. VariantClear(&varName);   
  67. }
  68. pPropBag->Release();
  69. pMoniker->Release();
  70. }
  71. pEnum->Release();
  72. pDevEnum->Release(); 
  73. }
  74. return hr;
  75. }
  76. IBaseFilter* CEnumDevice::GetCaptureDevice(int index)
  77. {
  78. IBaseFilter * tempCap = NULL;
  79. INT_PTR count = outlist.GetCount();
  80.     if(index >= 0 && index <= count-1)
  81. {
  82. tempCap = outlist[index]->pSrc;
  83. for(int i=0;i<count;i++)
  84. {
  85.           if( i != index) 
  86.   {
  87.   outlist[i]->pSrc->Release();
  88.   }
  89.   delete outlist[i];
  90. }
  91. return tempCap;
  92. }
  93. else
  94. {
  95.          return  tempCap;
  96. }
  97. }