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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:07:12+00:00 2026-06-05T23:07:12+00:00

I have an ATL COM component method which takes a BSTR as an in

  • 0

I have an ATL COM component method which takes a BSTR as an in parameter. I need to add each call to this method in an array. I can’t use a SAFEARRAY as that is fixed size so I was thinking that std::vector would be the easiest choice. Of course I will need to call SysAllocString for each addition to the vector. This means that SysFreeString needs to be called for each entry before the vector is destroyed.

I was looking for an easier/cleaner solution and thought of declaring the vector as vector<_bstr_t> which would include automatic cleanup. However, something in the back of my mind is raising the alarm at holding what is effectively a smart pointer in a standard container. Are my worries justified or can I safely do this? If not are there any other nicer solutions?

  • 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-05T23:07:13+00:00Added an answer on June 5, 2026 at 11:07 pm

    [regarding vector< _bstr_t >] Are my worries justified or can I safely do this?

    Yes. While you can safely use _bstr_t bear in mind that not only is it a smart pointer, but it is a reference counted smart pointer. Which means additional cost.

    If not are there any other nicer solutions?

    I would typically use a CComBSTR instead of BSTR or _bstr_t. If you need reference counting then you have to fall back on _bstr_t. E.g: If you’re also using ATL, you will probably only ever want CComBSTRs.

    The CComBSTR class is a wrapper for BSTRs, which are length-prefixed strings. The length is stored as an integer at the memory location preceding the data in the string.

    A BSTR is null-terminated after the last counted character but may also contain null characters embedded within the string. The string length is determined by the character count, not the first null character.

    So, before making a choice do consider the following:

    1. _bstr_t is a reference counted smart-pointer wrapper over BSTR whereas CComBSTR does not provide any reference counting.
    2. _bstr_t does not redefine the address-of operator and can thus be safely stored in a STL container. With CComBSTR you will need to use CAdapt.

    About the CAdapt object:

    CAdapt is a simple template used to wrap classes that redefine the address-of operator (operator &) to return something other than the address of the object. Examples of such classes include ATL’s CComBSTR, CComPtr, and CComQIPtr classes, and the compiler COM support class, _com_ptr_t. These classes all redefine the address-of operator to return the address of one of their data members (a BSTR in the case of CComBSTR, and an interface pointer in the case of the other classes).

    And then you can use:

    typedef std::vector< CAdapt< CComBSTR > > MyString;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ATL COM component method which has an [out] BSTR* parameter. I
We have a COM object implemented with C++/ATL that includes a method which will
I have a ATL COM project that was compiling fine in VC6 which gives
Have created a ATL COM project through which I am inserting Menu Items to
I have a ATL COM service exe (MyService.exe), which compiles and runs fine. If
I have an ATL COM object which needs to expose a collection of other
I have a COM component, implemented in C++ with ATL, that uses overlapped socket
I have a COM outproc server written in ATL that registers itself using _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER,
We have an ATL Service written in Visual Studio 6 which is now requiring
I have a simple ATL Service in which I have included the following statement.

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.