1

我是 COM 和智能指针的新手,我正在尝试将项目从原始指针转换为 CComPtr 以避免内存管理的麻烦。我正在寻找有关如何在一般功能和范围方面正确使用 CComPointers 的一些建议。我的代码示例。

int  DisplayDeviceInformation(IEnumMoniker * pEnum, IMoniker * pMoniker)
{
    CComPtr<IPropertyBag> pPropBag = NULL;

    while (pEnum->Next(1, &pMoniker, NULL) == S_OK)
    {

        HRESULT hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag));
        if (FAILED(hr))
        {

        } 

        VARIANT var;
        VariantInit(&var);

        // Get description or friendly name.
        hr = pPropBag->Read(L"Description", &var, 0);
        if (FAILED(hr))
        {
            hr = pPropBag->Read(L"FriendlyName", &var, 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", &var, 0);
        if (SUCCEEDED(hr))
        {
            printf("WaveIn ID: %d\n", var.lVal);
            VariantClear(&var); 
        }

        hr = pPropBag->Read(L"DevicePath", &var, 0);
        if (SUCCEEDED(hr))
        {
            // The device path is not intended for display.
            printf("Device path: %S\n", var.bstrVal);
            VariantClear(&var); 
        }

    }
    return 0;
}

CComPtr<IMoniker> pMoniker = NULL;  
CComPtr<IMoniker> pMoniker2 = NULL;
    CComPtr<IEnumMoniker> pEnum = NULL;

    hr = EnumerateDevices(CLSID_VideoInputDeviceCategory, &pEnum);
//pEnum->Next(1, &pMoniker,&cFetched);

    if (SUCCEEDED(hr))
    {
        DisplayDeviceInformation(pEnum, pMoniker);     
    }
    pEnum = NULL;
    hr = EnumerateDevices(CLSID_AudioInputDeviceCategory, &pEnum);
    //pEnum->Next(1, &pMoniker2,&cFetched);
    if (SUCCEEDED(hr))
    {
        DisplayDeviceInformation(pEnum, pMoniker);
    }

基本上,第一个DisplayDeviceInformation(pEnum, pMoniker);给出 ap==0 错误。但是,如果我取消注释pEnum->Next(1, &pMoniker,&cFetched);它会起作用。使用原始指针我不必这样做,因为代码只是跳到下一个设备。任何建议或帮助都会让我非常感激,在此先感谢!

4

1 回答 1

1

WhereCComPtr给你一个断言失败,你也可能遇到原始指针的问题。您只是没有得到预先警告,问题稍后会出现,例如作为参考泄漏。

  1. 您似乎不需要IMoniker在全球范围内
  2. 您需要IMoniker在为枚举器提供指针之前清除指针

见下文:

int DisplayDeviceInformation(IEnumMoniker* pEnum, IMoniker** ppSelectedMoniker)
{
    CComPtr<IMoniker> pMoniker;
    while (pEnum->Next(1, &pMoniker, NULL) == S_OK)
    {
        CComPtr<IPropertyBag> pPropBag; // You need it clear to start from for every moniker
        HRESULT hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag));
        // ...
        if(we should stop enumeration and we are good with current moniker)
        {
            //ATLASSERT(ppSelectedMoniker != NULL);
            *ppSelectedMoniker = pMoniker.Detach();
            return ...
        }
        // ...
        pMoniker.Release(); // You have to do this, so that next Next would accept empty CComPtr as an argument
    }
    return 0;
}

CComPtr<IEnumMoniker> pEnumVideoMoniker;
CComPtr<IMoniker> pSelectedVideoMoniker;
hr = EnumerateDevices(CLSID_VideoInputDeviceCategory, &pEnumVideoMoniker);
if (SUCCEEDED(hr))
    DisplayDeviceInformation(pEnumVideoMoniker, &pSelectedVideoMoniker);     
CComPtr<IEnumMoniker> pEnumAudioMoniker;
CComPtr<IMoniker> pSelectedAudioMoniker;
hr = EnumerateDevices(CLSID_AudioInputDeviceCategory, &pEnumAudioMoniker);
if (SUCCEEDED(hr))
    DisplayDeviceInformation(pEnumAudioMoniker, &pSelectedAudioMoniker);
于 2012-07-02T13:31:52.780 回答