How to Convert BSTR to Byte Array in VC++ ???

Posted on 2005-03-01
Medium Priority
Last Modified: 2012-08-13
Hi All,
  Can anybody provide me a simple routine to convert BSTR to byte array in VC++ ?

Any help will be really appreciated.

Question by:Manjesh
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

novitiate earned 150 total points
ID: 13437503
You can simply do this, or use One of WideCharToMultiByte functions.

#include <comdef.h>
#include <atlbase.h>
#include <iostream>

typedef unsigned char byte;
//#include <oleauto.h>

int main(void) {
      BSTR str = SysAllocString(L"Hello World");
      byte data[256];

      int len = SysStringLen(str);

      for(int i = 0; i < len; ++i)
            data[i] = (byte)str[i];
      data[i] = '\0';
      std::cout << data;
      return 0;


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question