convert BSTR to LPCWSTR

允我心安 提交于 2019-12-05 18:21:31

Just cover NULL scenario and you're good to go

BSTR l_strArgs;
LPCWSTR sth = strArgs ? strArgs : L"";

As you mentioned ATL in the tag, here is ATL-style one-liner:

OutputDebugString(CString(l_strArgs));

or, to make sure you are staying in Unicode domain:

OutputDebugStringW(CStringW(l_strArgs));

I just found this one

BSTR l_strArgs;
LPCWSTR  sth;
CString cs(_com_util::ConvertBSTRToString(l_strArg));
sth = cs;
OutputDebugStringW(sth);

BSTRs become easier to handle when you use a wrapper like _bstr_t instead. Here's the microsoft documentation on them http://msdn.microsoft.com/en-us/library/zthfhkd6%28v=VS.100%29.aspx

As you would expect, one of the _bstr_t constructors takes a BSTR parameter. There is also an operator to return a const wchar_t* which you should be able to cast to LPCWSTR.

Hope this helps

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!