Bstring / wstring

Started by Frank Brübach, March 10, 2024, 07:09:47 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Frank Brübach

Hey all Made this little example to compare  bstring with wstring


'#include "windows.bi" ' Include the Windows API declarations

uses corewin

'-- 1)
' Function to create a BSTR from a FreeBASIC string
Function CreateBSTR(ByVal strs As String) As bstring 'LPWSTR
    Dim As Integer length = Len(strs)
    Dim As bstr bstrs = SysAllocStringLen(ByVal StrPtr(strs), length)
    Return bstrs
End Function

' Subroutine to free a BSTR
Sub FreeBSTR(Byref bstrs As bstring)
    SysFreeString(bstrs)
    bstrs = Null
End Sub

' Main program
Sub Main()
    Dim As String myString = "Hello, my BSTR! "
    Dim As bstring bstrs = CreateBSTR(myString)
   
    ' Use the BSTR
    print bstrs " BSTR Example"
   
    ' Free the allocated memory
    FreeBSTR(bstrs)
End Sub

main() 'result: 33004116

'-- 2)
' Function to create a WSTRING from a FreeBASIC string
'
Function CreateBSTR2(ByVal wstrs As String) As wstring 'LPWSTR
    Dim As Integer length = Len(wstrs)
    Dim As wstring wstrs = SysAllocStringLen(ByVal StrPtr(wstrs), length)
    Return wstrs
End Function

' Subroutine to free a wstring
Sub FreeBSTR2(Byref wstrs As wstring)
    SysFreeString(wstrs)
    wstrs = Null
End Sub

' Main program
Sub Main2()
    Dim As String myString = "Hello, my WSTRING! "
    Dim As wstring wstrs = CreateBSTR2(myString)
   
    ' Use the BSTR
    print wstrs " WSTRING Example"
   
    ' Free the allocated memory
    FreeBSTR(wstrs)
End Sub

main2() '3470 - result for wstring is much shorter

  •  

Frank Brübach

And Second one :)

String ,/ zstring example

' simple example string / zstring oxygenbasic
' generatíng a string of n repeated characters
'
function generateRepeatedChar(character as string, n as Integer) as string
    dim result as string = ""
    dim i as integer

    for i = 1 to n
        result = result + character
    next

    return result
end function

' test example 1)
'
dim repeatedString as string
repeatedString = generateRepeatedChar("*#", 5)

print repeatedString ' Output: *#*#*#*#*#

' test example 2)
'
' Create a zString
dim zString as string = ""
print len(zString) ' Output: 0

'In this case, zString is an empty string,
'meaning it contains no characters.
'The length of zString is zero.

dim zString as string = "hey"
print len(zString) ' Output: 3

  •  

Charles Pegge

#2
OxygenBasic's built-in string types: ( ascii / unicode )

fixed length strings:

asciiz
zstring / wzstring
char  / wchar

ole/bstr strings:

bstring / wbstring

garbage collected, referenced ole/bstr strings

string / wstring