Log in

View Full Version : Embed VB.NET or C# code in a thinBasic script



Joe Caverly
10-04-2025, 02:36
Using PSCom (https://www.thinbasic.com/community/showthread.php?13383-Execute-a-PowerShell-ps1-script-or-a-PowerShell-command(s)&p=96993&viewfull=1#post96993),
you can embed VB.NET or C# code in a thinBasic script, and execute it.


uses "console"

dim ps as iDispatch
dim result as string
Dim VBNETCode as string

ps = CreateObject("PSCom.PSScript")

if IsComObject(ps) Then

VBNETCode = "$vbnetCode = @" + CHR$(34) + vbCRLF
VBNETCode = VBNETCode + "Imports System" + vbCRLF
VBNETCode = VBNETCode + "Public Class HelloWorld" + vbCRLF
VBNETCode = VBNETCode + " Public Function GetMessage() As String" + vbCRLF
VBNETCode = VBNETCode + " Return " + chr$(34) + "Hello from VB.NET!" + chr$(34) + vbCRLF
VBNETCode = VBNETCode + " End Function" + vbCRLF
VBNETCode = VBNETCode + "End Class" + vbCRLF
VBNETCode = VBNETCode + Chr$(34) + "@" + vbCRLF
VBNETCode = VBNETCode + "Add-Type -TypeDefinition $vbnetCode -Language VisualBasic" + VBCRLF
VBNETCode = VBNETCode + "$helloWorld = New-Object HelloWorld" + VBCRLF
VBNETCode = VBNETCode + "$message = $helloWorld.GetMessage()" + VBCRLF
VBNETCode = VBNETCode + "Write-Output $message" + VBCRLF

result = ps.ExecuteCommand(VBNETCode)

printl result
EndIf

ps = Nothing

Joe

Petr Schreiber
10-04-2025, 19:08
Thanks again for sharing an interesting solution.

Little tip to take advantage of thinBASIC string specification over multiple lines.

Instead of this:


VBNETCode = "$vbnetCode = @" + CHR$(34) + vbCRLF
VBNETCode = VBNETCode + "Imports System" + vbCRLF
VBNETCode = VBNETCode + "Public Class HelloWorld" + vbCRLF
VBNETCode = VBNETCode + " Public Function GetMessage() As String" + vbCRLF
VBNETCode = VBNETCode + " Return " + chr$(34) + "Hello from VB.NET!" + chr$(34) + vbCRLF
VBNETCode = VBNETCode + " End Function" + vbCRLF
VBNETCode = VBNETCode + "End Class" + vbCRLF
VBNETCode = VBNETCode + Chr$(34) + "@" + vbCRLF
VBNETCode = VBNETCode + "Add-Type -TypeDefinition $vbnetCode -Language VisualBasic" + VBCRLF
VBNETCode = VBNETCode + "$helloWorld = New-Object HelloWorld" + VBCRLF
VBNETCode = VBNETCode + "$message = $helloWorld.GetMessage()" + VBCRLF
VBNETCode = VBNETCode + "Write-Output $message" + VBCRLF


...you can do this (difference is in adding + sign at the end of the line):


VBNETCode = "$vbnetCode = @" + CHR$(34) + vbCRLF +
"Imports System" + vbCRLF +
"Public Class HelloWorld" + vbCRLF +
" Public Function GetMessage() As String" + vbCRLF +
" Return " + chr$(34) + "Hello from VB.NET!" + chr$(34) + vbCRLF +
" End Function" + vbCRLF +
"End Class" + vbCRLF +
Chr$(34) + "@" + vbCRLF +
"Add-Type -TypeDefinition $vbnetCode -Language VisualBasic" + VBCRLF +
"$helloWorld = New-Object HelloWorld" + VBCRLF +
"$message = $helloWorld.GetMessage()" + VBCRLF +
"Write-Output $message" + VBCRLF



Petr