简体   繁体   中英

convert BSTR to LPCWSTR

Here is my need

BSTR l_strArgs;
LPCWSTR  sth;
//----
//---
OutputDebugStringW(sth);

How to convert BSTR to LPCWSTR ?

Is there any header only library that coverts any string type(microsoft) to LPCWSTR type ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM