Skip to content

Instantly share code, notes, and snippets.

@kuba-orlik
Last active June 27, 2021 21:56
Show Gist options
  • Save kuba-orlik/999ca634dba613ba6a1c to your computer and use it in GitHub Desktop.
Save kuba-orlik/999ca634dba613ba6a1c to your computer and use it in GitHub Desktop.
Convert BSTR to std::string
#include "stdafx.h"
#include <wtypes.h>
#include <comutil.h>
#pragma comment(lib,"comsuppw.lib")
#include <string>
#include <string.h>
#include <stdio.h>
using namespace std;
string bstr_to_str(BSTR source){
//source = L"lol2inside";
_bstr_t wrapped_bstr = _bstr_t(source);
int length = wrapped_bstr.length();
char* char_array = new char[length];
strcpy_s(char_array, length+1, wrapped_bstr);
return char_array;
}
int _tmain(int argc, _TCHAR* argv[]){
BSTR bstr_var = SysAllocString(L"I am bstr");
string str = bstr_to_str(bstr_var);
printf("result: %s\n", str.c_str());//result: I am bstr
getchar();
return 0;
}
@PrithiviRathinam
Copy link

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment