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
7
votes
2 answers

Memory leak for CComBSTR

I have read that the following code causes memory leak. But did not understand why. CComBSTR str; pFoo->get_Bar(&str); pFoo->get_Baf(&str); How does it cause a leak when we are not allocating anything?
Julian
  • 159
  • 1
  • 7
6
votes
5 answers

How do you efficiently copy BSTR to wchar_t[]?

I have a BSTR object that I would like to convert to copy to a wchar__t object. The tricky thing is the length of the BSTR object could be anywhere from a few kilobytes to a few hundred kilobytes. Is there an efficient way of copying the data…
Obediah Stane
6
votes
4 answers

How to use BSTR*

I have an out value as BSTR* for an interface in a C++ COM dll. And I am returning this to a C# .Net client. In my C++ function I have to assign different values according to a diff condition. For example: If my function is fun(BSTR* outval) { //…
Sana
  • 79
  • 1
  • 1
  • 3
6
votes
3 answers

C++: Convert wchar_t* to BSTR?

I'm trying to convert a wchar_t * to BSTR. #include #include using namespace std; int main() { wchar_t* pwsz = L"foo"; BSTR bstr(pwsz); cout << SysStringLen(bstr) << endl; getchar(); } This prints 0,…
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
6
votes
2 answers

Is there a way to write a BSTR literal?

When calling a function that expects a BSTR it'd be nice to be able to write something like: iFoo->function( bs"HELLO" ); However the only workaround I'm aware of is to use a wrapper that calls SysAllocString etc., e.g.: iFoo->function(…
M.M
  • 138,810
  • 21
  • 208
  • 365
6
votes
2 answers

C++ leak with VARIANT / bstrVal code

A leak checker tells me that I have a memory leak on memory that is allocated in the following code: // Get the value from the object as a variant. VARIANT vVal; VariantInit ( &vVal ); hres = clsObj->Get ( fieldName.c_str(), 0, &vVal, 0, 0 ); if (…
user1964027
  • 61
  • 1
  • 3
5
votes
2 answers

Freeing a BSTR using ::SysFreeString(). More Platform Dependant?

I am writing a COM Server which have a plenty of Interfaces and methods. And most of the methods have the BSTR as the parameters and as local parameters used for the return. A snippet looks like Update 5: The real code. This fetches from bunch of…
Sathish Guru V
  • 1,417
  • 2
  • 15
  • 39
5
votes
3 answers

Where is using null BSTR documented?

It's at least common practice to treat null BSTR (null WCHAR* pointer) as an empty string and design all the code manipulating BSTRs accordingly. Answers to this question say the same. Where is this practice documented? Is there any official…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
5
votes
1 answer

Why is my BSTR to std::wstring conversion so slow? Is my tester bad?

I often need to convert BSTR strings to std::wstring. A NULL BSTR counts as an empty BSTR. I used to do it like this: #define CHECKNULLSTR(str) ((str) ? (str) : L"") std::wstring wstr(CHECKNULLSTR(bstr)); It doesn't handle internal '\0' chars, but…
Felix Dombek
  • 13,664
  • 17
  • 79
  • 131
5
votes
2 answers

Why does the compiler use a temporary variable?

Minimal code reproducing the problem: #include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]) { CComBSTR ccbTest( L"foo" ); const wchar_t * pTest = ccbTest ? ccbTest : L"null string"; return 0; } The compiler uses a temporary…
manuell
  • 7,528
  • 5
  • 31
  • 58
5
votes
2 answers

What is the terminology for this use of a constructor?

A co-worker wrote the following code, which I am convinced is wrong. I want to explain the problems to him, but don't know the proper term, so I cannot find references to support my position: His code: BSTR someString = _bstr_t(L"Hello World"); Why…
abelenky
  • 63,815
  • 23
  • 109
  • 159
5
votes
2 answers

Shall we treat BSTR type in COM as value or reference?

From book ATL Internals, I knew BSTR is different from OLECHAR*, and there are CComBSTR and CString for BSTR. According MSDN Allocating and Releasing Memory for a BSTR, I knew memory management responsibility for caller/callee. Take this line from…
Raymond
  • 885
  • 10
  • 28
5
votes
1 answer

_bstr_t to char*, amazing result

first: LPCTSTR asdfsdf = (LPCTSTR)(_bstr_t)v; printf("%s\n", asdfsdf); second: printf("%s\n", (LPCTSTR)(_bstr_t)v); they are the same, but the first condition causes unreadable code why?
Janli Lee
  • 63
  • 1
  • 5
4
votes
4 answers

Which is better code for converting BSTR parameters to ANSI in C/C++?

So far I've discovered I can convert incoming BSTRs to ANSI in two (of many?) ways, and I'm curious to know whether one is "better" than the other with respect to speed / efficiency etc. The way I've been using for a while is use the USES_CONVERSION…
bugmagnet
  • 7,631
  • 8
  • 69
  • 131
4
votes
2 answers

Are BSTR UTF-16 Encoded?

I'm in the process of trying to learn Unicode? For me the most difficult part is the Encoding. Can BSTRs (Basic String) content code points U+10000 or higher? If no, then what's the encoding for BSTRs?
Mike
  • 1,760
  • 2
  • 18
  • 33
1
2
3
11 12