PDA

View Full Version : Variant/String for VB6/thinBasic



Joe Caverly
24-03-2024, 17:24
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

ReneMiner
28-03-2024, 16:20
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

Joe Caverly
28-03-2024, 16:54
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

ReneMiner
28-03-2024, 17:18
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