所以我们有一个简单的C win32控制台应用程序.我们想要的只是打印可用的网络摄像机和其他视频捕获设备列表.我们想尽可能多地使用
windows apis – 毕竟没有外部库 – 我们想要的就是打印一个列表 – 不要飞到月球上!)如何做到这一点?
我自己的研究:
我发现这个official msdn sample但是我仍然没有得到如何将设备列表输出到屏幕=((对不起 – 我是C的新手)
……还有一些研究……
在一篇关于主题的最简单的ms样本中找到了这个
- HRESULT OnInitDialog(HWND hwnd,ChooseDeviceParam *pParam)
- {
- HRESULT hr = S_OK;
- HWND hList = GetDlgItem(hwnd,IDC_DEVICE_LIST);
- // Display a list of the devices.
- for (DWORD i = 0; i < pParam->count; i++)
- {
- WCHAR *szFriendlyName = NULL;
- hr = pParam->ppDevices[i]->GetAllocatedString(
- MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME,&szFriendlyName,NULL
- );
- if (Failed(hr))
- {
- break;
- }
- int index = ListBox_AddString(hList,szFriendlyName);
- ListBox_SetItemData(hList,index,i);
- CoTaskMemFree(szFriendlyName);
- }
看起来它应该做的工作,但我不知道如何将其包含在简单的命令行应用程序中,以便输出数据…
也来自这个系列:
> How to get a list of video capture devices on linux?和special details on getting cameras NAMES正确,经过测试的答案
> How to get a list of video capture devices on Mac OS?正确,尚未通过我的答案测试
> How to get a list of video capture devices on windows?正确,经过测试的答案
> How to get a list video capture devices NAMES using Qt (crossplatform)?
从显示的示例中,将以下代码复制到dev.c.然后打开包含所有SDK变量集的命令行.在命令行链接到ole32.lib和oleaut32.lib.然后它会显示所有设备.
cl dev.c ole32.lib oleaut32.lib
dev.exe将在命令行中列出该列表.
- #include <windows.h>
- #include <dshow.h>
- #pragma comment(lib,"strmiids")
- HRESULT EnumerateDevices(REFGUID category,IEnumMoniker **ppEnum)
- {
- // Create the System Device Enumerator.
- ICreateDevEnum *pDevEnum;
- HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum,NULL,CLSCTX_INPROC_SERVER,IID_PPV_ARGS(&pDevEnum));
- if (SUCCEEDED(hr))
- {
- // Create an enumerator for the category.
- hr = pDevEnum->CreateClassEnumerator(category,ppEnum,0);
- if (hr == S_FALSE)
- {
- hr = VFW_E_NOT_FOUND; // The category is empty. Treat as an error.
- }
- pDevEnum->Release();
- }
- return hr;
- }
- void DisplayDeviceInformation(IEnumMoniker *pEnum)
- {
- IMoniker *pMoniker = NULL;
- while (pEnum->Next(1,&pMoniker,NULL) == S_OK)
- {
- IPropertyBag *pPropBag;
- HRESULT hr = pMoniker->BindToStorage(0,IID_PPV_ARGS(&pPropBag));
- if (Failed(hr))
- {
- pMoniker->Release();
- continue;
- }
- VARIANT var;
- VariantInit(&var);
- // Get description or friendly name.
- hr = pPropBag->Read(L"Description",&var,0);
- if (Failed(hr))
- {
- hr = pPropBag->Read(L"FriendlyName",0);
- }
- if (SUCCEEDED(hr))
- {
- printf("%S\n",var.bstrVal);
- VariantClear(&var);
- }
- hr = pPropBag->Write(L"FriendlyName",&var);
- // WaveInID applies only to audio capture devices.
- hr = pPropBag->Read(L"WaveInID",0);
- if (SUCCEEDED(hr))
- {
- printf("WaveIn ID: %d\n",var.lVal);
- VariantClear(&var);
- }
- hr = pPropBag->Read(L"DevicePath",0);
- if (SUCCEEDED(hr))
- {
- // The device path is not intended for display.
- printf("Device path: %S\n",var.bstrVal);
- VariantClear(&var);
- }
- pPropBag->Release();
- pMoniker->Release();
- }
- }
- void main()
- {
- HRESULT hr = CoInitializeEx(NULL,COINIT_MULTITHREADED);
- if (SUCCEEDED(hr))
- {
- IEnumMoniker *pEnum;
- hr = EnumerateDevices(CLSID_VideoInputDeviceCategory,&pEnum);
- if (SUCCEEDED(hr))
- {
- DisplayDeviceInformation(pEnum);
- pEnum->Release();
- }
- hr = EnumerateDevices(CLSID_AudioInputDeviceCategory,&pEnum);
- if (SUCCEEDED(hr))
- {
- DisplayDeviceInformation(pEnum);
- pEnum->Release();
- }
- CoUninitialize();
- }
- }