Repository navigation
BSTRING
Home › All Categories › sys/win › sys/win/com › BSTRING
Wraps a COM BSTR string and frees it when the variable goes out of scope.
A BSTR is the string type of COM and Automation: UTF-16 text in a block allocated by
SysAllocString, with its length in bytes stored just before the first character. COM methods
take and return text this way, and the memory belongs to whoever holds the handle.
A BSTRING holds one such handle in its public field m_bstr and frees it in its destructor.
The model to keep in mind:
-
It owns its handle. The destructor calls
SysFreeStringonm_bstr. Anything you store inm_bstryourself becomes theBSTRING's to free. -
Text in, by encoding. A wide string (
wstr( "…" ), a USTRING, anotherBSTRING) is copied as it is, and so is text assigned with=(Let), narrow or wide. -
Text out. Assign to a
ustring(dim u as ustring = bs) or read Utf8. A function that reads aBSTRtakes theBSTRINGitself;*bsis the handle.
#import "sys/win"
#import "sys/win/com"
using win, comBuilds a string, edits it in place, passes it to a COM function that reads a BSTR and lets
one fill a BSTRING:
#import "sys/win"
#import "sys/win/com"
using win, com
dim bs as BSTRING = wstr( "Café" )
bs += " au lait"
print bs
print "length:"; len( bs )
bs[0] = asc( "c" )
dim u as ustring = bs
print u
' A function that reads a BSTR takes the BSTRING as it is
dim other as BSTRING = wstr( "CAFÉ AU LAIT" )
if VarBstrCmp( bs, other, LOCALE_USER_DEFAULT, NORM_IGNORECASE ) = VARCMP_EQ then print "same text"
' A function that writes a BSTR gets the address of the handle
dim num as BSTRING
VarBstrFromI4( 1234, LOCALE_USER_DEFAULT, 0, @num.m_bstr )
print num.Utf8; " has "; len( num ); " characters"It prints:
length:12
café au lait
same text
1234 has 4 characters
| Member | Does |
|---|---|
| Constructor | Creates an empty BSTRING, or one holding a copy of some text. |
| Destructor | Frees the BSTR. |
| Append | Adds text to the end. |
| Clear | Frees the BSTR and leaves the variable empty. |
| Utf8 | Reads the text as UTF-8, or replaces it with UTF-8 text. |
| wchar | Returns a copy of the text in memory from CoTaskMemAlloc. |
| Let | Replaces the text with a copy of a wide string. |
| Cast | Converts to the text or to the BSTR handle. |
| Operator [] (character index) | Reads or changes one UTF-16 code unit. |
| Operator @ (address) | Frees the text and returns the address of the handle, for an output parameter. |
constructor
constructor( byref bs as BSTRING )
constructor( byref ustr AS USTRING )| Parameter | Meaning |
|---|---|
bs |
The BSTRING to copy. |
ustr |
Wide text to convert. |
Example
dim b1 as BSTRING = "Hello"
DIM b2 AS BSTRING = "Серге́й"
print b1 & " " & b2Microsoft Learn: SysAllocString
destructorFrees m_bstr with SysFreeString when it is not NULL. It runs when the variable goes out
of scope.
Example
scope
dim bs as BSTRING = "temporary"
end scope ' the BSTR is freed hereMicrosoft Learn: SysFreeString
sub Append( byref ustr as USTRING )Adds a copy of ustr to the end, in a new BSTR that replaces the old one.
Example
dim bs as BSTRING = "Hello"
bs.Append( ", world" )
dim u as ustring = bs
print u ' Hello, worldMicrosoft Learn: SysAllocStringLen
sub ClearFrees the BSTR and sets m_bstr to NULL, as a new BSTRING is. Call it before handing
@bs.m_bstr to a function that writes a new BSTR there, or the old one leaks.
Example
dim bs as BSTRING = "old"
bs.Clear()
VarBstrFromI4( 42, LOCALE_USER_DEFAULT, 0, @bs.m_bstr )property Utf8() as string
property Utf8( byref utf8String as string )Reading Utf8 returns the text converted to UTF-8, or "" for an empty BSTRING.
Setting Utf8 frees the current text and stores utf8String converted from UTF-8.
Returns the text as UTF-8 (the getter).
Example
dim bs as BSTRING = wstr( "año" )
dim s as string = bs.Utf8
print len( s ) ' 4 bytesMicrosoft Learn: WideCharToMultiByte
function wchar() as wstring ptrReturns a copy of the characters in a block allocated with CoTaskMemAlloc, for a method that
takes a wide string and frees it itself. Free it with CoTaskMemFree when nothing else does.
The block is exactly as large as the characters, with no room for a terminating null, so the copy is only null-terminated when the text is empty; pass the length along or terminate it yourself.
Returns the copy, or NULL when the allocation fails.
Example
dim bs as BSTRING = "abc"
dim p as wstring ptr = bs.wchar()
CoTaskMemFree( p )Microsoft Learn: CoTaskMemAlloc
operator let( byref bs as BSTRING )
operator let( byref ustr as USTRING )Frees the current text and stores a copy of bs or ustr.
Example
dim b1 as BSTRING = "first"
dim b2 as BSTRING = "second"
b2 = b1 ' b2 now holds its own copy of "first"operator cast() byref as const wstring
operator cast() as any ptrThe first form gives the text as a wide string, which is how a BSTRING turns into a ustring
or a wstring. The second gives the BSTR handle, which is how a BSTRING is passed to a
parameter of type BSTR. Both read m_bstr directly; an empty BSTRING converts to an empty
ustring and passes a NULL handle.
Example
dim bs as BSTRING = "Hello"
dim u as ustring = bs
print u; " "; SysStringLen( bs ) ' Hello 5operator []( byval nIndex as UINT ) byref as ushortReturns a reference to the UTF-16 code unit at the zero-based position nIndex, so it can be
read or changed. The index is not checked.
Returns the code unit at nIndex.
Example
dim bs as BSTRING = "Hello"
bs[0] = asc( "J" )
print bs[1] ' 101, the code of "e"operator @() as BSTR ptrFrees the current text, stores a new empty BSTR and returns the address of m_bstr, so that a
function with an output BSTR parameter writes its result into the BSTRING. A function that
overwrites the parameter without freeing it leaves that empty string behind.
Returns the address of m_bstr.
Example
dim bs as BSTRING = "old"
VarBstrFromI4( 7, LOCALE_USER_DEFAULT, 0, @bs.m_bstr ) ' the form that builds today| Function | Does |
|---|---|
| Len | Returns the length in characters. |
| Operator * (handle) | Returns the BSTR handle. |
operator len( byref bs as BSTRING ) as UINTReturns the number of UTF-16 code units in the BSTR, read from its length prefix, or 0 for an
empty BSTRING.
Returns the length.
Example
dim bs as BSTRING = "Hello"
print len( bs ) ' 5Microsoft Learn: SysStringLen
operator *( byref bs as BSTRING ) as wstring ptrReturns the BSTR handle, m_bstr, typed as a wstring ptr. **bs is the text. Use it to
pass the string where a BSTR is wanted.
Returns m_bstr.
Example
dim bs as BSTRING = "Hello"
print SysStringByteLen( *bs ) ' 10- sys/win/com
-
DVARIANT, which converts to and from
BSTRING -
DSafeArray for arrays of
BSTR - COM procedures
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing