Questions tagged [bstr]

`BSTR` stands for "Basic String". It is a size-prefixed, fixed-length, null-terminated, UTF-16 encoded character array used heavily in Microsoft's COM and OLE technologies for marshalling strings, especially between languages.

In many (but not all) cases BSTRs can be used in place of standard wide character arrays, but in almost all cases the reverse is not true.

For a complete guide to BSTR semantics, see Eric's Complete Guide to BSTR Semantics.

179 questions
23
votes
5 answers

How to convert char * to BSTR?

How can I pass a char * from C dll to VB Here is sample code: void Cfunc(char *buffer,int len) { BSTR buf_bstr = SysAllocString((BSTR)buffer); VBptr.VBfunc(buf_bstr,len); } This function is not working, In actual some other values are sent to…
Joseph_blr
21
votes
5 answers

Why are C#/.NET strings length-prefixed and null terminated?

After reading What's the rationale for null terminated strings? and some similar questions I have found that in C#/.NET strings are, internally, both length-prefixed and null terminated like in BSTR Data Type. What is the reason strings are both…
prostynick
  • 6,129
  • 4
  • 37
  • 61
14
votes
1 answer

Convert BSTR to char*

Anyone know how to convert BSTR to char* ? Update: I tried to do this, but don't know if it is right or wrong. char *p= _com_util::ConvertBSTRToString(URL->bstrVal); strcpy(testDest,p );
barlyee
  • 419
  • 4
  • 10
  • 20
14
votes
5 answers

How to convert _bstr_t to CString

I have a _bstr_t variable bstrErr and I am having a CString variable csError. How do I set the value which come in bstrErr to csError?
subbu
  • 3,229
  • 13
  • 49
  • 70
13
votes
4 answers

Static code analysis for detecting passing a wchar_t* to BSTR

Since a BSTR is only a typedef for wchar_t* our code base has several (many?) places where string literals are passed to a method expecting a BSTR this can mess up with marshallers or anyone who tries to use any BSTR specific method (e.g.…
Motti
  • 110,860
  • 49
  • 189
  • 262
11
votes
6 answers

Using _bstr_t to pass parameter of type BSTR* in function

What is the correct way of doing this: _bstr_t description; errorInfo->GetDescription( &description.GetBSTR() ); or: _bstr_t description; errorInfo->GetDescription( description.GetAddress() ); Where IError:GetDescription is defined as: HRESULT…
Asim
  • 869
  • 1
  • 10
  • 17
11
votes
4 answers

How would you convert a std::string to BSTR*?

How would you convert a std::string to BSTR*? STDMETHODIMP CMyRESTApp::rest(BSTR data, BSTR* restr) { RESTClient restclient; RESTClient::response resp = restclient.get(data); Log("Response Status code: %s", resp.code); …
xkm
  • 271
  • 2
  • 4
  • 11
10
votes
3 answers

How to best convert CString to BSTR to pass it as an "in" parameter into a COM method?

I need to convert a CString instance into a properly allocated BSTR and pass that BSTR into a COM method. To have code that compiles and works indentically for both ANSI and Unicode I use CString::AllocSysString() to convert whatever format CString…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
10
votes
2 answers

Should there be a difference between an empty BSTR and a NULL BSTR?

When maintaining a COM interface should an empty BSTR be treated the same way as NULL? In other words should these two function calls produce the same result? // Empty BSTR CComBSTR empty(L""); // Or SysAllocString(L"") someObj->Foo(empty); //…
Motti
  • 110,860
  • 49
  • 189
  • 262
9
votes
3 answers

_bstr_t to UTF-8 possible?

I have a _bstr_t string which contains Japanese text. I want to convert this string to a UTF-8 string which is defined as a char *. Can I convert the _bstr_t string to char * (UTF-8) string without losing the Japanese characters?
Manav Sharma
  • 1,053
  • 1
  • 13
  • 21
9
votes
5 answers

Can I free memory passed to SysAllocString?

When allocating a new BSTR with SysAllocString via a wchar_t* on the heap, should I then free the original wchar_t* on the heap? So is this the right way? wchar_t *hs = new wchar_t[20]; // load some wchar's into hs... BSTR bs =…
noctonura
  • 12,763
  • 10
  • 52
  • 85
9
votes
1 answer

COM, VARIANT containing BSTR. Who allocates?

OK, so I couldn't really think of an apropos title that summarizes this. The IPrintPipelinePropertyBag interface has the method AddProperty which aptly enough "adds a property to a property…
moogs
  • 8,122
  • 8
  • 44
  • 60
8
votes
3 answers

How to display values from a VARIANT with a SAFEARRAY of BSTRs

I am working on a COM Object library with function that returns a VARIANT with a SAFEARRAY of BSTRs. How can I display the values from this VARIANT instance and save it inside a TStringList? I tried searching the net with no clear answer. I tried…
Heb
  • 111
  • 2
  • 6
7
votes
2 answers

Who owns returned BSTR?

Suppose a method from a COM interface returns BSTR value. Am I right in my opinion that I must free it? The code example at http://msdn.microsoft.com/en-us/library/aa365382(VS.85).aspx does not do that. Who's wrong?
Sergey Skoblikov
  • 5,811
  • 6
  • 40
  • 49
7
votes
2 answers

How to pass BSTR to printf?

I have a non-unicode (MBCS) C++ project building with VS2013. Given a BSTR value, how should I pass this to printf safely?
Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
1
2 3
11 12