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 6375291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:35:47+00:00 2026-05-25T01:35:47+00:00

I use on my COM server simple object constructor: CComPtr<IObj> obj; //member hr =

  • 0

I use on my COM server simple object constructor:

CComPtr<IObj>      obj; //member
 hr = SUCCEDDED(hr) ? this->obj.CoCreateInstance(__uuidof(CObj)) : hr;

STDMETHODIMP CConfig::get_Obj(IObj ** pVal)
{
    if (pVal == nullptr)
    {
        return E_POINTER;
    }

    CComPtr<IObj> result(this->obj);
    *pVal = result.Detach();
    return S_OK;
}

What I want to accomplish is for the get_Obj() method to return same interface object at succesive calls. Looking over the code CComPtr seems to increment reference count. I also detach afterwards from CComPtr.
Is this the right way to implement what I want ? I don’t want the COM client to be forced to “dispose” the IObj interface object every time it calls method get_Obj().

  • 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-05-25T01:35:48+00:00Added an answer on May 25, 2026 at 1:35 am

    Yes, you implement it in one of the clearest ways. You’d have to store the last returned value somewhere – for example in CConfig as a member variable. Other than that you can’t provide clean onwership semantics – it’s normal that the client will expect the returned pointer to be AddRef()ed (as in your code) and it’s normal that the client will release the object ownership. If you want to return the same object you have to store an extra pointer somewhere and that pointer must also own the object (call AddRef() at least once).

    So your code does it right – it copies a stored pointer and does AddRef(). You could just as well go without CComPtr:

    if (pVal == nullptr) {
        return E_POINTER;
    }
    
    *pVal = this->obj;
    if( this->obj != 0 ) {
       this->obj->AddRef();
    }
    return S_OK;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If you type nslookup -type=SRV _xmpp-server._tcp.gmail.com (or use the dig command in OSX) you
Can I know how to deallocate COM server object forcefully from C# .NET if
I'm trying to use a super simple API from is.gd: http://is.gd/api.php?longurl=http://www.example.com Which returns a
I have been working 2 days on creating a simple exe COM server, I
I want to build a DLL Class Library use COM Interop, with C#, target
I'm working on an asp.net web site. We have to use com interop to
From some old c++ code im trying to use a com dll, it works
I often use the website www.cplusplus.com as a reference when writing C code. I
Is it better to use $.get(http://www.example.com/mydirectory, function(data) { $(.someclass).html(data); }); or $('.tripPlannerBottom').load(http://www.example.com/mydirectory); any speed
Is there some way I can use URLs like: http://www.blog.com/team-spirit/ instead of http://www.blog.com/?p=122 in

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.