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

  • Home
  • SEARCH
  • 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 9122539
In Process

The Archive Base Latest Questions

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

BSTR is this weird Windows data type with a few specific uses, such as

  • 0

BSTR is this weird Windows data type with a few specific uses, such as COM functions. According to MSDN, it contains a WCHAR string and some other stuff, like a length descriptor. Windows is also nice enough to give us the _bstr_t class, which encapsulates BSTR; it takes care of the allocation and deallocation and gives you some extra functionality. It has four constructors, including one that takes in a char* and one that takes in a wchar_t*. MSDN’s description of the former: “Constructs a _bstr_t object by calling SysAllocString to create a new BSTR object and then encapsulates it. This constructor first performs a multibyte to Unicode conversion.”

It also has operators that can extract a pointer to the string as any of char*, const char*, and wchar_t*, and I’m pretty sure those are null-terminated, which is cool.

I’ve spent a while reading up on how to to convert between multibyte and Unicode, and I’ve seen a lot of talk about how to use mbstowcs and wcstomb, and how MultiByteToWideChar and WideCharToMultiByte are better because of encodings may differ, and blah blah blah. It all kind of seems like a headache, so I’m wondering whether I can just construct a _bstr_t and use the operations to access the strings, which would be… a lot fewer lines of code:

char* multi = "asdf";
_bstr_t bs = _bstr_t(mb);
wchar_t* wide = (wchar_t*)bs; // assume read-only

I guess my intuitive answer to this is that we don’t know what Windows is doing behind the scenes, so if I have a problem using mbstowcs/wcstomb (I guess I really mean mbstowcs_s/wcstomb_s) rather than MultiByteToWideChar/WideCharToMultiByte, I shouldn’t risk it because it’s possible that Windows uses those. (It’s almost certainly not using the latter, since I’m not specifying a “code page” here, whatever that is.) Honestly I’m not sure yet whether I consider the mbstowcs_s and wcstomb_s functions OK for my purposes, because I don’t really have a grasp on all of the different encodings and stuff, but that’s a whole different question and it seems to be addressed all over the Internet.

Sooooo, is there anything wrong with doing this, aside from that potential concern?

  • 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-17T06:07:41+00:00Added an answer on June 17, 2026 at 6:07 am

    Using _bstr_t::_bstr_t(const char*) is not exactly a good idea in production code:

    Constructs a _bstr_t object by calling SysAllocString to create a new BSTR object and encapsulate it. This constructor first performs a multibyte to Unicode conversion. If s2 is too large, you [sic] may generate a stack overflow error. In such a situation, convert your char* to a wchar_t with MultiByteToWideChar and then call the wchar_t * constructor.

    Besides that _bstr_t::operator wchar_t*() const throw() seems barely useful. It’s just for struct member extraction, so you’re constrained to a const:

    These operators can be used to extract raw pointers to the encapsulated Unicode or multibyte BSTR object. The operators return the pointer to the actual internal buffer, so the resulting string cannot be modified.

    So _bstr_t is just a helper object for encapsulating BSTRs, and a mediocre one at that. Conversion using MultiByteToWideChar and WideCharToMultiByte is a much better choice, for multiple reasons:

    • It’s much less prone to crash.
    • You don’t get a const buffer in return, because you provide your own.
    • The names of those functions are self-descriptive. Conversion through a constructor and casting operator of an unrelated type is not.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to interface a Linux app to a server that uses bstr data.
I have a structure defined in IDL. This structure has following members: { BSTR
I have an ATL COM component method which takes a BSTR as an in
I need to store a VARIANT of type bstr in a stl vector. I'm
Updated: please see this other thread instead, all this COM stuff is not part
I have an external function like this: extern C __declspec(dllexport) int __cdecl Identify(BSTR* bstrTemplates,
I have this code that uses the USE_CONVERSION macro in a C++ project... I
I'm working on some older code that uses ATL's CComBSTR type. I'm changing it
i had an global structure like this:- struct MachineList { BSTR AccountId; BSTR MachineId;
I am confused about Windows BSTR's and WCHAR's, etc. WCHAR is a 16-bit character

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.