ConvertBSTRToString

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at ConvertBSTRToString.

Microsoft Specific**

Converts a BSTR value to a char *.

  
      char* __stdcall ConvertBSTRToString(  
   BSTR pSrc  
);  

Parameters

pSrc
A BSTR variable.

ConvertBSTRToString allocates a string you must delete.

// ConvertBSTRToString.cpp  
#include <comutil.h>  
#include <stdio.h>  
  
#pragma comment(lib, "comsuppw.lib")  
  
int main() {  
   BSTR bstrText = ::SysAllocString(L"Test");  
   wprintf_s(L"BSTR text: %s\n", bstrText);  
  
   char* lpszText2 = _com_util::ConvertBSTRToString(bstrText);  
   printf_s("char * text: %s\n", lpszText2);  
  
   SysFreeString(bstrText);  
   delete[] lpszText2;  
}  

BSTR text: Test  
char * text: Test  

Header: comutil.h.

Lib: comsuppw.lib or comsuppwd.lib (see /Zc:wchar_t (wchar_t Is Native Type) for more information)

Compiler COM Global Functions

Show: