How to pass BSTR to printf?

Mr. Boy picture Mr. Boy · Sep 9, 2015 · Viewed 10.1k times · Source

I have a non-unicode (MBCS) C++ project building with VS2013.

Given a BSTR value, how should I pass this to printf safely?

Answer

MSalters picture MSalters · Sep 9, 2015

A BSTR really is a WCHAR* with preceding length information. You can ignore that length part for printing purposes. So:

BSTR str = foo();
printf("%S", str); // Capital S