WIA 2.0,C ++:IWiaDevMgr2 :: EnumDeviceInfo没有检测到连接的摄像头

时间:2015-07-28 14:28:12

标签: c++ wia

我正在尝试编写一个从相机传输图像和视频的程序(供我个人使用,在Win 8.1上)。我使用Microsoft的示例代码作为起点(WIA Tutorial),而且我试图检测连接的相机设备。问题是没有错误,代码似乎工作,但它只是没有检测到任何连接的相机(我已尝试使用两个不同的相机),而操作系统可以清楚地检测到相机(显示在Windows资源管理器中。

我错过了什么吗? IWiaDevMgr2 :: EnumDeviceInfo不是检测连接设备的方法吗?以下是我使用的代码:

HRESULT WiaCreateDeviceManager(IWiaDevMgr2 **ppWiaDevMgr)
{
    if(NULL == ppWiaDevMgr) return E_INVALIDARG;
    *ppWiaDevMgr = NULL;

    // Create an instance of the device manager
    HRESULT hr = CoCreateInstance(CLSID_WiaDevMgr2, NULL, CLSCTX_LOCAL_SERVER, IID_IWiaDevMgr2, (void**)ppWiaDevMgr);
    return hr;
}

HRESULT WiaEnumerateDevices(IWiaDevMgr2 *pWiaDevMgr)
{
    if(NULL == pWiaDevMgr)
    {
        return E_INVALIDARG;
    }

    // Get a device enumerator interface
    IEnumWIA_DEV_INFO *pWiaEnumDevInfo = NULL;
    HRESULT hr = pWiaDevMgr->EnumDeviceInfo(WIA_DEVINFO_ENUM_LOCAL, &pWiaEnumDevInfo);
    assert(hr == S_OK);

    if(SUCCEEDED(hr))
    {
        ULONG count(911);
        HRESULT res = pWiaEnumDevInfo->GetCount(&count);
        if(res == S_OK) printf("EnumDeviceInfo: count = %lu\n", count); // count is always zero
        else printf("IEnumWIA_DEV_INFO::GetCount() failed!\n");

        // Loop until you get an error or pWiaEnumDevInfo->Next returns
        // S_FALSE to signal the end of the list.
        while(S_OK == hr)
        {
            // Get the next device's property storage interface pointer
            IWiaPropertyStorage *pWiaPropertyStorage = NULL;
            hr = pWiaEnumDevInfo->Next(1, &pWiaPropertyStorage, NULL);

            // pWiaEnumDevInfo->Next will return S_FALSE when the list is
            // exhausted, so check for S_OK before using the returned
            // value.
            if(hr == S_OK)
            {
                // Do something with the device's IWiaPropertyStorage*
                WiaReadProperties(pWiaPropertyStorage); // this line is never reached
                // Release the device's IWiaPropertyStorage*
                pWiaPropertyStorage->Release();
                pWiaPropertyStorage = NULL;
            }
        }

        // If the result of the enumeration is S_FALSE (which
        // is normal), change it to S_OK.
        if(S_FALSE == hr) hr = S_OK;

        // Release the enumerator
        pWiaEnumDevInfo->Release();
        pWiaEnumDevInfo = NULL;
    }

    return hr;
}

int main()
{
    ...
    IWiaDevMgr2 *wiamgr;
    WiaCreateDeviceManager(&wiamgr);
    HRESULT res = WiaEnumerateDevices(wiamgr); // res is always S_OK, but no device is detected
    ...
}

1 个答案:

答案 0 :(得分:1)

显然,WIA不支持Windows Vista及更高版本的相机设备。我只是在WIA文档中两次传递中暗示或提到过这一点,最后一次是this page。在我花了这么多时间研究WIA之后,我无法相信这种情况正在发生。显然,我应该使用WPD用于相机,而不是WIA。

编辑:话虽如此,我仍然不确定发生了什么。如果我不能在Win 8.1上以编程方式使用WIA,那么为什么这些PowerShell命令有效呢?

$WIAdialog = New-Object -ComObject "WIA.CommonDialog"
$Device    = $WIAdialog.ShowSelectDevice()
$i=$WIAdialog.ShowAcquireImage()
$i.SaveFile("$pwd\test.$($i.fileExtension)")

只有API不能用于相机,而脚本模型呢?