如何转换到BSTR为std :: string在Visual Studio C ++ 2010? [英] How to convert BSTR to std::string in Visual Studio C++ 2010?

查看:198
本文介绍了如何转换到BSTR为std :: string在Visual Studio C ++ 2010?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个COM DLL。我想一个BSTR转换为一个std ::字符串传递给需要一个const引用参数的方法。

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.

看来,使用_com_util :: ConvertBSTRToString()来获得的char *相当于BST​​R的是一个适当的方式这样做。然而,API文档稀疏,而且执行是潜在的越野车:

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.$c$cproject.com/Articles/1969/BUG-in-_com_util-ConvertStringToBSTR-and-_com_util

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

例如:

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

有什么优点和替代品的利弊使用ConvertBSTRToString(),preferrably基于标准的方法和类的?

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

推荐答案

您可以自己做。我preFER转换成目标的std ::字符串如果可能的话。如果没有,用一个临时值覆盖。

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

调用为:

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.

这篇关于如何转换到BSTR为std :: string在Visual Studio C ++ 2010?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆