Last active
June 27, 2021 21:56
-
-
Save kuba-orlik/999ca634dba613ba6a1c to your computer and use it in GitHub Desktop.
Convert BSTR to std::string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you