Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8170391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:07:17+00:00 2026-06-06T21:07:17+00:00

I’m new to COM and smartpointers, I’m trying to convert a project from raw

  • 0

I’m new to COM and smartpointers, I’m trying to convert a project from raw pointers to CComPtr to avoid the hassle with memory management. I’m looking for some advice on how to properly use CComPointers when it comes to functions and scope in general. A sample of my code.

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);
    }

Basiclly, the first DisplayDeviceInformation(pEnum, pMoniker); gives a p==0 error. If I however uncomment the pEnum->Next(1, &pMoniker,&cFetched); it works. With raw pointers I don’t have to do that since the code just skips to the next device. Any advice or help would make me outmost grateful, thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:07:18+00:00Added an answer on June 6, 2026 at 9:07 pm

    Where CComPtr gives you an assert failure, you are likely to have an issue with raw pointers as well. You just are not pre-warned and the problem comes up later, e.g. as a reference leak.

    1. You don’t seem to need IMoniker in global scope
    2. You need to clear IMoniker pointer before supplying it for the enumerator

    See below:

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.