3

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 ?

asit_dhal
  • 1,239
  • 19
  • 35
  • possible duplicate of [ATL how to Convert BSTR\* str to registry key.SetValue(LPCTSTR str type](http://stackoverflow.com/questions/7654998/atl-how-to-convert-bstr-str-to-registry-key-setvaluelpctstr-str-type) – Roman R. May 17 '13 at 14:35

3 Answers3

6

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));
Roman R.
  • 68,205
  • 6
  • 94
  • 158
2

I just found this one

BSTR l_strArgs;
LPCWSTR  sth;
CString cs(_com_util::ConvertBSTRToString(l_strArg));
sth = cs;
OutputDebugStringW(sth);
asit_dhal
  • 1,239
  • 19
  • 35
1

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

Rich
  • 4,572
  • 3
  • 25
  • 31