BSTR에서 std :: string (std :: wstring)으로 또는 그 반대로
C ++에서 COM으로 작업하는 동안 문자열은 일반적으로 BSTR데이터 유형입니다. 누군가는 또는 MS BSTR와 같은 래퍼 를 사용할 수 있습니다 . 그러나 MinGW 컴파일러에서 ATL 또는 MFC를 사용할 수 없기 때문에 변환 (또는 )하거나 그 반대로 변환 할 표준 코드 스 니펫이 있습니까?CComBSTRCStringBSTRstd::stringstd::wstring
BSTR와 유사한 비 MS 래퍼도 CComBSTR있습니까?
최신 정보
어떤 식 으로든 나를 도와 주신 모든 분들께 감사드립니다! BSTR와 사이의 변환에 대한 문제를 아무도 해결하지 않았기 때문에 std::string여기에 방법에 대한 몇 가지 단서를 제공하고 싶습니다.
아래는 내가 변환에 사용하는 기능입니다 BSTR에 std::string와 std::string에 BSTR각각 :
std::string ConvertBSTRToMBS(BSTR bstr)
{
int wslen = ::SysStringLen(bstr);
return ConvertWCSToMBS((wchar_t*)bstr, wslen);
}
std::string ConvertWCSToMBS(const wchar_t* pstr, long wslen)
{
int len = ::WideCharToMultiByte(CP_ACP, 0, pstr, wslen, NULL, 0, NULL, NULL);
std::string dblstr(len, '\0');
len = ::WideCharToMultiByte(CP_ACP, 0 /* no flags */,
pstr, wslen /* not necessary NULL-terminated */,
&dblstr[0], len,
NULL, NULL /* no default char */);
return dblstr;
}
BSTR ConvertMBSToBSTR(const std::string& str)
{
int wslen = ::MultiByteToWideChar(CP_ACP, 0 /* no flags */,
str.data(), str.length(),
NULL, 0);
BSTR wsdata = ::SysAllocStringLen(NULL, wslen);
::MultiByteToWideChar(CP_ACP, 0 /* no flags */,
str.data(), str.length(),
wsdata, wslen);
return wsdata;
}
BSTR받는 사람 std::wstring:
// given BSTR bs
assert(bs != nullptr);
std::wstring ws(bs, SysStringLen(bs));
std::wstring받는 사람 BSTR:
// given std::wstring ws
assert(!ws.empty());
BSTR bs = SysAllocStringLen(ws.data(), ws.size());
문서 심판 :
std::basic_string<typename CharT>::basic_string(const CharT*, size_type)std::basic_string<>::empty() conststd::basic_string<>::data() conststd::basic_string<>::size() constSysStringLen()SysAllocStringLen()
You could also do this
#include <comdef.h>
BSTR bs = SysAllocString("Hello");
std::wstring myString = _bstr_t(bs, false); // will take over ownership, so no need to free
or std::string if you prefer
Simply pass the BSTR directly to the wstring constructor, it is compatible with a wchar_t*:
BSTR btest = SysAllocString(L"Test");
assert(btest != NULL);
std::wstring wtest(btest);
assert(0 == wcscmp(wtest.c_str(), btest));
Converting BSTR to std::string requires a conversion to char* first. That's lossy since BSTR stores a utf-16 encoded Unicode string. Unless you want to encode in utf-8. You'll find helper methods to do this, as well as manipulate the resulting string, in the ICU library.
There is a c++ class called _bstr_t. It has useful methods and a collection of overloaded operators.
For example, you can easily assign from a const wchar_t * or a const char * just doing _bstr_t bstr = L"My string"; Then you can convert it back doing const wchar_t * s = bstr.operator const wchar_t *();. You can even convert it back to a regular char const char * c = bstr.operator char *(); You can then just use the const wchar_t * or the const char * to initialize a new std::wstring oe std::string.
참고URL : https://stackoverflow.com/questions/6284524/bstr-to-stdstring-stdwstring-and-vice-versa
'Programing' 카테고리의 다른 글
| NULL / 0을 dynamic_cast에 전달하는 것이 이식 가능합니까? (0) | 2020.11.28 |
|---|---|
| Objective-C in, out, inout, byref, byval, .. 등등. (0) | 2020.11.28 |
| git 저장소 이동 (0) | 2020.11.28 |
| 하나의 유닛이 .NET MVC 컨트롤러를 어떻게 테스트해야합니까? (0) | 2020.11.28 |
| 개체가 Mockito 모의인지 어떻게 알 수 있습니까? (0) | 2020.11.28 |