6
votes

I am working on a COM dll. I wish to convert a BSTR to a std::string to pass to a method that takes a const reference parameter.

It seems that using _com_util::ConvertBSTRToString() to get the char* equivalent of the BSTR is an appropriate way to do so. However, the API documentation is sparse, and the implementation is potentially buggy:

http://msdn.microsoft.com/en-us/library/ewezf1f6(v=vs.100).aspx http://www.codeproject.com/Articles/1969/BUG-in-_com_util-ConvertStringToBSTR-and-_com_util

Example:

#include <comutil.h>
#include <string>

void Example(const std::string& Str) {}

int main()
{
    BSTR BStr = SysAllocString("Test");
    char* CharStr = _com_util::ConvertBSTRToString(BStr);
    if(CharStr != NULL)
    {
        std::string StdStr(CharStr);
        Example(StdStr);
        delete[] CharStr;
    }
    SysFreeString(BStr);
}

What are the pros and cons of alternatives to using ConvertBSTRToString(), preferrably based on standard methods and classes?

2
Garbage in, garbage out. You can write your own converter with WideCharToMultiByte() and do something more reasonable when you get a one megabyte string. Like throwing an exception. - Hans Passant
To clarify, is this a question about a poorly documented MS comsupp-lib API? Or are you asking how to do this yourself? (or both?) - WhozCraig
@WhozCraig: I suppose both, so perhaps I should split them into two separate questions, and I'll edit this one to focus on my goal rather than my method. - user1472525
Note that, in that codeproject page, the author rants about a function allocating "twice as much memory as [it] needs," because they allocate wcslen(str)*2. There seems to be a misunderstanding as to just how wide wide-characters are. The article should be taken with some amount of salt, probably twice as much as usual. - ssube

2 Answers

12
votes

You can do this yourself. I prefer to convert into the target std::string if possible. If not, use a temp-value override.

// convert a BSTR to a std::string. 
std::string& BstrToStdString(const BSTR bstr, std::string& dst, int cp = CP_UTF8)
{
    if (!bstr)
    {
        // define NULL functionality. I just clear the target.
        dst.clear();
        return dst;
    }

    // request content length in single-chars through a terminating
    //  nullchar in the BSTR. note: BSTR's support imbedded nullchars,
    //  so this will only convert through the first nullchar.
    int res = WideCharToMultiByte(cp, 0, bstr, -1, NULL, 0, NULL, NULL);
    if (res > 0)
    {
        dst.resize(res);
        WideCharToMultiByte(cp, 0, bstr, -1, &dst[0], res, NULL, NULL);
    }
    else
    {    // no content. clear target
        dst.clear();
    }
    return dst;
}

// conversion with temp.
std::string BstrToStdString(BSTR bstr, int cp = CP_UTF8)
{
    std::string str;
    BstrToStdString(bstr, str, cp);
    return str;
}

Invoke as:

BSTR bstr = SysAllocString(L"Test Data String")
std::string str;

// convert directly into str-allocated buffer.
BstrToStdString(bstr, str);

// or by-temp-val conversion
std::string str2 = BstrToStdString(bstr);

// release BSTR when finished
SysFreeString(bstr);

Something like that, anyway.

-1
votes

Easy way

BSTR => CStringW => CW2A => std::string.