ScriptBasic COM Integration


Author: Dave
Date: 06.14.14 - 10:35am



This is part 3 of the ScriptBasic series.

You can download a copy to play with here: Source & Binaries

So i have been tinkering with it for the last 3 weekends. All of the major hurtles have been cleared.

  • using scivb/scintilla for syntax text editor
  • intellisense + tool tips
  • integrated debugger with breakpoints, single step, step out, run to cursor
  • run time variable inspection
  • run time call stack inspection
  • api configurable default paths for include and modules directories
  • COM extension for creating ActiveX objects and calling methods/properties

Debug UI showing active debugging


The rest of this post is mostly on the COM integration. This adds very powerful capabilities without much code. The design i went for uses CreateObject and CallByName which are both familiar to vb6 developers.

declare sub CreateObject alias "CreateObject" lib "sb_engine.dll"
declare sub CallByName alias "CallByName" lib "sb_engine.dll"
declare sub ReleaseObject alias "ReleaseObject" lib "sb_engine.dll"

const VbLet = 4
const VbMethod = 1

'you can load objects either by ProgID or CLSID
obj = CreateObject("SAPI.SpVoice") 

if obj = 0 then 
	print "CreateObject failed!
"
else
	CallByName(obj, "rate", VbLet, 2)
	CallByName(obj, "volume", VbLet, 60)
	CallByName(obj, "speak", VbMethod, "This is my test")
	ReleaseObject(obj)
	print "Release called obj reference is now: ", obj, "
"
end if 


Next came the problem of how to give scripts access to the host applications objects. ScriptBasic does not have any easy implementation for this. I spent hours looking through the source trying to see how to add symbols to the GlobalVariables table.

The easy answer however was not to have the host set global variables at runtime, but instead to let the script query the host application for the values it needs. The resolver idea I had before worked perfectly for this and I cooked up a new extension method.

declare sub GetHostObject alias "GetHostObject" lib "sb_engine.dll"
declare sub GetHostString alias "GetHostString" lib "sb_engine.dll"

obj = GetHostObject("Form1")

if obj = 0 then 
	print "GetHostObject failed! Make sure host called AddObject
"
else
	CallByName(obj, "caption", VbLet, "this is my form caption!")
	CallByName(obj, "Alert", VbMethod, "This is my test")
end if 

print "GetHostString(test)=", GetHostString("test")


The VB6 host then implements a method AddObject, AddString that allow the extension to look up these live object pointers at runtime through a callback to a resolver.

Once you have the objptr (which is just a long value), CallByName can operate freely and access the objects arbitrarily. (because all VB6 form and class objects are COM objects under the hood which already support IDispatch scripting interfaces.

In just a couple lines of code, integration with the host app just became seamless and now requires no extra work marshalling data between the native script host and the embedded intrepreter. Pretty slick!

The next question that comes up, is how do you deal with complex statements that access multiple objects when using CallByName. Lets look at an Excel example:

'vbscript: 
Set ExcelWorkbook = ExcelApp.Workbooks.Add

'translates to
oWorkBook = CallByName(oExcelApp, "Workbooks", vbGet)
oExcelWorkbook = CallByName(oWorkBook, "Add")

'vbscript: 
ExcelSheet.Cells(i, j).Value = "test"

'translates to
oCell = CallByName(oExcelSheet, "Cells", vbGet, i, j)
CallByName(oCell, "Value", vbLet, "test")


Its tested and works fine, but slightly bulky. Next experiment is going to be a statement parser that automates these steps.




Comments: (0)

 
Leave Comment:
Name:
Email: (not shown)
Message: (Required)
Math Question: 69 + 79 = ? followed by the letter: Q 



About Me
More Blogs
Main Site
Posts: (year)
2024 (3)
     VB6 JSON
     ffmpeg voodoo
     RegJump Vb
2023 (9)
     VB6 Virtual Files
     File handles across dlls
     python abort / script timeout
     py4vb
     VB6 Python embed w/debugger
     python embedding
     VB6 IDE Enhancements
     No Sleep
     A2W no ATL
2022 (4)
     More VB6 - C data passing
     Vb6 Asm listing
     Byte Array C to VB6
     Planet Source Code DVDs
2021 (2)
     Obscure VB
     VB6 IDE SP6
2020 (4)
     NTFileSize
     BSTR from C Dll to VB
     Cpp Memory Manipulation
     ActiveX Binary Compatability
2019 (5)
     Console tricks
     FireFox temp dir
     OCX License
     Extract substring
     VB6 Console Apps
2018 (6)
     VB6 UDTs
     VB6 Debugger View As Hex tooltips
     VB6 - C Share registry data
     VB6 Addin Missing Menus
     VB6 Class Init Params
     VB6 isIn function
2017 (6)
     Python and VB6
     Python pros and cons
     download web Dir
     vc rand in python
     VB6 Language Enhancement
     Register .NET as COM
2016 (22)
     VB6 CDECL
     UDT Tricks pt2
     Remote Data Extraction
     Collection Extender
     VB6 FindResource
     CDO.Message
     DirList Single Click
     Reset CheckPoint VPN Policy
     VB6 BSTR Oddities Explained
     SafeArrays in C
     BSTR and Variant in C++
     Property let optional args
     Misc Libs
     Enum Named Pipes
     Vb6 Collection in C++
     VB6 Overloaded Methods
     EXPORT FUNCDNAME Warning
     VB6 Syncronous Socket
     Simple IPC
     VB6 Auto Resize Form Elements
     Mach3 Automation
     Exit For in While
2015 (15)
     C# self register ocx
     VB6 Class Method Pointers
     Duktape Debug Protocol
     QtScript 4 VB
     Vb6 Named Args
     vb6 Addin Part 2
     VB6 Addin vrs Toolbars
     OpenFile Dialog MultiSelect
     Duktape Example
     DukTape JS
     VB6 Unsigned
     .Net version
     TitleBar Height
     .NET again
     VB6 Self Register OCXs
2014 (25)
     Query Last 12 Mos
     Progid from Interface ID
     VB6 to C Array Examples
     Human Readable Variant Type
     ScriptBasic COM Integration
     CodeView Addin
     ScriptBasic - Part 2
     Script Env
     MSCOMCTL Win7 Error
     printf override
     History Combo
     Disable IE
     API Hooking in VB6
     Addin Hook Events
     FastBuild Addin
     VB6 MemoryWindow
     Link C Obj Files into VB6
     Vb6 Standard Dlls
     CStr for Pascal
     Lazarus Review
     asprintf for VS
     VB6 GlobalMultiUse
     Scintilla in VB6
     Dynamic Highlight
     WinVerifyTrust, CryptMsgGetParam VB6
2013 (4)
     MS GLEE Graphing
     printf for VB6
     C# App Config
     Tero DES C# Test
2012 (10)
     VC 2008 Bit Fields
     Speed trap
     C# Db Class Generator
     VB6 vrs .NET (again)
     FireFox Whois Extension
     git and vb6
     Code Additions
     Compiled date to string
     C# ListView Sorter
     VB6 Wish List
2011 (7)
     C# Process Injection
     CAPTCHA Bots
     C# PE Offset Calculator
     VB6 Async Download
     Show Desktop
     coding philosophy
     Code release
2010 (11)
     Dll Not Found in IDE
     Advanced MSScript Control
     random tip
     Clipart / Vector Art
     VB6 Callback from C#
     Binary data from VB6 to C#
     CSharp and MsScriptControl
     HexDumper functions
     Js Beautify From VB6 or C#
     vb6 FormPos
     Inline Asm w VB6
2009 (3)
     The .NET Fiasco
     One rub on computers
     Universal extractor