Reputation: 1804
I have a BSTR*. how to get the value from the BSTR* to std::string so that I can print it to console?
BSTR* ptr;
HRESULT result = objPtr->GetValue(ptr);
//need to print to console the value
Upvotes: 0
Views: 2393
Reputation: 1277
There are many ways to pull this off; here's a simple example making use of the _bstr_t Class to effect the conversion from BSTR*
to std::string
.
#include "stdafx.h"
#include <Windows.h>
#include <comutil.h>
#include <iostream>
#include <string>
#pragma comment(lib,"comsuppw.lib")
int _tmain(int argc, _TCHAR* argv[])
{
const BSTR* const pbstr = new BSTR(::SysAllocString(L"Hello World"));
if (pbstr)
{
const std::string stdstr(_bstr_t(*pbstr, true));
std::cout << stdstr << std::endl;
::SysFreeString(*pbstr);
delete pbstr;
}
return 0;
}
Upvotes: 1
Reputation: 4366
std::wcout << ptr;
should work because it is compatible to an wchar_t*.
You can also construct a std::wstring
of an BSTR that is no nullptr
.
If you wish to create a std::string
of it you can check outher questions like
this one but be aware of the ideas of encoding. BSTR is encoded as UTF-16.
Upvotes: 1