Ver código fonte

Win10自带DirectX,VS2008需要手动配置头文件和库文件路径。listDevices函数实现枚举出所有USB摄像头或录像设备。

sat23 5 anos atrás
pai
commit
c2671ee651
1 arquivos alterados com 79 adições e 0 exclusões
  1. 79 0
      GameAssist/GameAssist/GameAssist.cpp

+ 79 - 0
GameAssist/GameAssist/GameAssist.cpp

@@ -6,10 +6,81 @@
 #include "GameAssist.h"
 #include "GameAssistDlg.h"
 
+// directx
+#include <strmif.h>
+#pragma comment(lib, "strmiids.lib")
+#define VI_MAX_CAMERAS 20
+DEFINE_GUID(CLSID_SystemDeviceEnum, 0x62be5d10, 0x60eb, 0x11d0, 0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86);
+DEFINE_GUID(CLSID_VideoInputDeviceCategory, 0x860bb310, 0x5d01, 0x11d0, 0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86);
+DEFINE_GUID(IID_ICreateDevEnum, 0x29840822, 0x5b84, 0x11d0, 0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86);
+
 #ifdef _DEBUG
 #define new DEBUG_NEW
 #endif
 
+//列出硬件设备
+int listDevices(std::vector<std::string>& list)
+{
+	ICreateDevEnum *pDevEnum = NULL;
+	IEnumMoniker *pEnum = NULL;
+	int deviceCounter = 0;
+	CoInitialize(NULL);
+
+	//HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum,NULL,CLSCTX_INPROC_SERVER,IID_ICreateDevEnum,reinterpret_cast<void**>(&pDevEnum));
+	HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum,NULL,CLSCTX_INPROC_SERVER,IID_ICreateDevEnum,(void**)(&pDevEnum));
+	if (SUCCEEDED(hr))
+	{
+		hr = pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory,&pEnum, 0);
+		if (hr == S_OK){
+
+			IMoniker *pMoniker = NULL;
+			while (pEnum->Next(1, &pMoniker, NULL) == S_OK)
+			{
+				IPropertyBag *pPropBag;
+				hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag,(void**)(&pPropBag));
+
+				if (FAILED(hr)) {
+					pMoniker->Release();
+					continue; // Skip this one, maybe the next one will work.
+				}
+
+				VARIANT varName;
+				VariantInit(&varName);
+				hr = pPropBag->Read(L"Description", &varName, 0);
+				if (FAILED(hr))
+				{
+					hr = pPropBag->Read(L"FriendlyName", &varName, 0);
+				}
+
+				if (SUCCEEDED(hr))
+				{
+					hr = pPropBag->Read(L"FriendlyName", &varName, 0);
+					int count = 0;
+					char tmp[255] = { 0 };
+					while (varName.bstrVal[count] != 0x00 && count < 255)
+					{
+						tmp[count] = (char)varName.bstrVal[count];
+						count++;
+					}
+					list.push_back(tmp);
+				}
+
+				pPropBag->Release();
+				pPropBag = NULL;
+				pMoniker->Release();
+				pMoniker = NULL;
+
+				deviceCounter++;
+			}
+
+			pDevEnum->Release();
+			pDevEnum = NULL;
+			pEnum->Release();
+			pEnum = NULL;
+		}
+	}
+	return deviceCounter;
+}
 
 // CGameAssistApp
 
@@ -50,6 +121,14 @@ BOOL CGameAssistApp::InitInstance()
 
 	AfxEnableControlContainer();
 
+#if 1
+	std::vector<std::string> vtDevices;
+	listDevices(vtDevices);
+	// 枚举USB摄像头设备;
+	ICreateDevEnum *pCreateDevEnum=0;
+	HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (void**)&pCreateDevEnum);
+#endif
+
 	// 标准初始化
 	// 如果未使用这些功能并希望减小
 	// 最终可执行文件的大小,则应移除下列