ScriptBasic COM Integration
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.
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.
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.
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:
Its tested and works fine, but slightly bulky. Next experiment is going to be a statement parser that automates these steps.