unicode wchar_t is 16 bit char variable in COM/OLE, is also known as wide character. Wide character array/string can give Multilanguage language support.
BSTR is a kind of array of wide chars. The difference is BSTR holds the length of the string in the header and the content follows the header. The first 16bit/2 byte holds the length of the string and rest of the bytes contains the wide chars. All the out-of-process COM/DCOM uses this BSTR because when the string has to be transported through RPC the length indicates how many bytes should follow after header of BSTR.
|BSTR memory layout|
|Header 16 bit(length)||Body (Length * sizeof(wchar_t)|
BSTR can be allocated using SysAllocString() API using header file oleauto.h. It takes an WIDE string or unicode string and returns the BSTR buffer. A failure case is handled by returning NULL to the caller.
BSTR allocated using SysAllocString() API should be deallocated using SysFreeString(). All the BSTR APIs are part of oleauto.h header file.
Note: Programmer should not use malloc or free calls for BTRs allocation and deallocation process.
Allocate & free BSTR example
Here is an example use of BSTR with webbrowser navigate method. We are allocation the URL BSTR string from an UNICODE string and using in IWebBrowser.Navigate(URL) call. Once the call is made we are deallocating it.
Function returning BSTR example
In the above case we are having an example of call by value. The caller is allocating the string and passing to an external method. Now another case is possible where the interface method is allocating the string and that is returned to the caller. This is known as call by reference and returned by reference. The caller is providing the reference BSTR address to the method and the method is allocating it and returning the same. In this case the parameter is known as [out,retval] parameter in DCOM terminology. The caller is responsible for the deallocation of this BSTR buffer. A memory leak might happen if the caller misses to call the SysFreeString.
About our authors: Team EQA
You have viewed 1 page out of 67. Your COM/DCOM learning is 0.00% complete. Login to check your learning progress.
Questions index C Questions C++ Questions Win32 MFC COM/DCOM DLL Questions
Compilers & Editors
Download Visual Studio Download XCode Download Visual Studio Code Android studio install sdk Eclipse installer Best C compilers IDEs
Development system setup
Windows media creation tool MSDN subscription Ubuntu virtualbox
New updated posts
Why learn C? Draw on printer Free alt softwares