Results 1 to 4 of 4

Thread: Variant/String for VB6/thinBasic

  1. #1
    Member
    Join Date
    Feb 2023
    Location
    London, Ontario, Canada
    Posts
    37
    Rep Power
    5

    Variant/String for VB6/thinBasic

    I would like to use the StringBuilder module with VB6.

    I note that I have to use .ToString to convert StringBuilder contents.

    Looking at the mod_thinbasic.bas file, I see no string type;

    Public Const VarSubType_Byte = 1
    Public Const VarSubType_Integer = 2
    Public Const VarSubType_Word = 3
    Public Const VarSubType_DWord = 4
    Public Const VarSubType_Long = 5
    Public Const VarSubType_Quad = 6
    Public Const VarSubType_Single = 7
    Public Const VarSubType_Double = 8
    Public Const VarSubType_Currency = 9
    Public Const VarSubType_Ext = 10
    Public Const VarSubType_Variant = 50
    
    for use with the thinBasic_AddVariable_VB function.

    Thus, I see that I will have to declare the variable as Variant from VB6,
    and do converstion to string,
    for use with StringBuilder,
    then back to Variant for VB6.

    So, I started my quest by creating a test script;
    uses "Console"
    Dim vStuff as Variant
    Dim SStuff as String
    
    vStuff = "Test"                              'Variant
    PrintL "TypeOf(vStuff) : " + TypeOf(vStuff)  'Variant.Variant
    SStuff = vStuff                              'String equals Variant
    PrintL "TypeOf(sStuff) : " + TypeOf(sStuff)  'String.String
    PrintL "String : " + sStuff                  'Test
    PrintL "Variant: " + vStuff                  'Test
    
    ...which produces...

    TypeOf(vStuff) : Variant.Variant
    TypeOf(sStuff) : String.String
    String : Test
    Variant: Test
    
    So far, so good.

    Next, get this working with VB6;
    Attribute VB_Name = "mod_Main"
    Option Explicit
    Sub Main()
        Dim hScript As Long    'handle to thinBasic Script
        Dim sScript As String  'thinBasic Script
        Dim hRun As Long       'thinBasic_Run
        
        Dim sStuff As String   'Declare String Variable for thinBasic Script
        Dim vStuff As Variant  'Declare Variant Variable for thinBasic Script
        
        Dim lRet As Long       'thinBasic_AddVariable_VB
        Dim sResult As String  'thinBasic Script Output
        
        Const thinBasic_BufferType_IsFile = 0
        Const thinBasic_BufferType_IsScript = 1
    
        On Error GoTo CatchError
      
        'Begin thinBasic Script
        sScript = vbNullString
        sScript = "DIM sStuff as String" + vbCrLf
        sScript = sScript + "DIM vStuff as Variant" + vbCrLf
        
        sScript = sScript + "sStuff = " + Chr$(34) + "Test" + Chr$(34) + vbCrLf
        sScript = sScript + "vStuff = sStuff"
        'End   thinBasic Script
    
        If Len(sScript) Then
          hScript = thinBasic_Init(0, App.hInstance, "thinbasic")
          If hScript = 0 Then
                
            lRet = thinBasic_AddVariable_VB("vStuff", "", 0, VarSubType_Variant, VarPtr(vStuff))
                
            hRun = thinBasic_Run(hScript, sScript, thinBasic_BufferType_IsScript, 1 Or 2, False, False, False, 1, False)
            
            sResult = sResult + Time$ + " : String: " + sStuff + " : Variant: " + vStuff
               
            Debug.Print sScript
            Debug.Print sResult
            Debug.Print
                
            thinBasic_Release (hScript)
          End If
        Else
          Debug.Print "Where's the code for the thinBasic Script?"
        End If
        
        Exit Sub
    
    CatchError:
        MsgBox "Error occurred: " + Err.Description
        Resume Next
    Return
    
    End Sub
    
    ...which produces...

    DIM sStuff as String
    DIM vStuff as Variant
    sStuff = "Test"
    vStuff = sStuff
    11:04:11 : String:  : Variant:
    
    Nothing is returned from thinBasic to VB6.

    As there is no Public Const VarSubType_String,
    how do I send a variant from VB6 to thinBasic,
    convert it to a string,
    do something with the string,
    convert the string to a variant,
    and send it back to VB6?

    Thanks from Joe

  2. #2
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    55
    Posts
    1,554
    Rep Power
    174
    When using Variants be aware of the %VT_-variant-vartypes - a complete list is in the thinbasic help for keywords VariantVT[#|$]
    The enumeration is original Integer (Int16) for VB6
    I think there are missing some Forum-sections as beta-testing and support

  3. #3
    Member
    Join Date
    Feb 2023
    Location
    London, Ontario, Canada
    Posts
    37
    Rep Power
    5
    Thanks for that.

    Option Explicit
    Sub Main()
        Dim hScript As Long    'handle to thinBasic Script
        Dim sScript As String  'thinBasic Script
        Dim hRun As Long       'thinBasic_Run
        
        Dim sStuff As String   'Declare String Variable for thinBasic Script
        Dim vStuff As Variant  'Declare Variant Variable for thinBasic Script
        
        Dim lRet As Long       'thinBasic_AddVariable_VB
        Dim sResult As String  'thinBasic Script Output
        Dim rc As Long
        
        Const thinBasic_BufferType_IsFile = 0
        Const thinBasic_BufferType_IsScript = 1
    
        On Error GoTo CatchError
      
        sStuff = ""
        vStuff = ""
    
        rc = 0
        
        'Begin thinBasic Script
        sScript = vbNullString
        sScript = "DIM sStuff as String" + vbCrLf
        sScript = sScript + "DIM vStuff as Variant" + vbCrLf
        sScript = sScript + "sStuff = " + Chr$(34) + "Test" + Chr$(34) + vbCrLf
        sScript = sScript + "vStuff = sStuff" + vbCrLf
        sScript = sScript + "rc = VariantVT(vStuff)"
        'End   thinBasic Script
    
        If Len(sScript) Then
          hScript = thinBasic_Init(0, App.hInstance, "thinbasic")
          If hScript = 0 Then
                
            lRet = thinBasic_AddVariable_VB("vStuff", "", 0, VarSubType_Variant, VarPtr(vStuff))
            lRet = thinBasic_AddVariable_VB("rc", "", 0, VarSubType_Long, VarPtr(rc))
                
            hRun = thinBasic_Run(hScript, sScript, thinBasic_BufferType_IsScript, 1 Or 2, False, False, False, 1, False)
            
            sResult = sResult + Time$ + " : String: " + sStuff + " : Variant: " + vStuff
               
            Debug.Print sScript
            Debug.Print sResult
            Debug.Print "VariantVT(vStuff) is: " + Str$(rc)
            Debug.Print
                
            thinBasic_Release (hScript)
          End If
        Else
          Debug.Print "Where's the code for the thinBasic Script?"
        End If
        
        Exit Sub
    
    CatchError:
        MsgBox "Error occurred: " + Err.Description
        Resume Next
    Return
    
    End Sub
    
    Output;

    DIM sStuff as String
    DIM vStuff as Variant
    sStuff = "Test"
    vStuff = sStuff
    rc = VariantVT(vStuff)
    10:50:04 : String:  : Variant: 
    VariantVT(vStuff) is:  8
    
    Thus, vStuff is a %VT_BSTR, which is a Dynamic String.

    Still don't know why I cannot get thinBasic to return vStuff or sStuff to VB6.

    Joe

  4. #4
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    55
    Posts
    1,554
    Rep Power
    174
    1. Vb has undocumented functions STRPTR() and VARPTR()
    2. VB uses internally what tinbasic knows as z-terminated Widestring but for DLL vb auto- converts mostly to ANSI
    so you would not pass any string to thinbasic but a Strptr
    Then run a

    Word x
    while peek(word, vbstrptr +x)
    x +=2
    wend
    String s =widecharToUtf8$( Memory_get(vbStrptr, x-2))

    To peek the UTF-16 LE string as thinbasic-native dynamic string
    I think there are missing some Forum-sections as beta-testing and support

Similar Threads

  1. Fast string handling in thinBasic 1.10.5
    By ErosOlmi in forum Do you know ...
    Replies: 0
    Last Post: 15-10-2018, 22:45
  2. Pointer to Variant
    By mike lobanovsky in forum Tips and Tricks
    Replies: 5
    Last Post: 17-12-2013, 11:35
  3. typo VARIANT # & $
    By Michael Clease in forum Fixed or cleared errors in help material
    Replies: 1
    Last Post: 06-11-2007, 16:17
  4. variant parameter
    By kryton9 in forum Power Basic
    Replies: 2
    Last Post: 24-09-2007, 18:52

Members who have read this thread: 2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •