October 1999
Code for this article: Oct99VP.exe (230KB)
George Shepherd is a Senior Software Engineer at Stingray Software (www.stingsoft.com), an MFC/Java class library company, where he develops Visual CASE. George co-wrote MFC Internals (Addison-Wesley, 1996).
|
Q I've heard about a COM-based technology called
Active Scripting. What is it, and how can I use it in my
own applications?
Many readers
A Scripting has been a part of COM since its inception. You can go back and read Inside OLE, by Kraig Brockschmidt (or any of the older COM literature), for extensive descriptions rationalizing the IDispatch interface. In the early days of COM, the story usually went something like: "Each application has its own scripting language interface. Rather than having multiple applications each expose their own macro language, let's separate the language you use to script your application from the application itself. It would be better for applications to provide a single interface that some script-based environment (like Visual Basic® or Visual Basic for Applications) can understand so that high-level application users can easily script their applications."
Thus was born Automation, which is based on an interface named IDispatch. If your application (or objects within your app) implements the IDispatch interface, then it is said to be scriptable. That is, you can write some scripting code (like Visual Basic) and have the script code access methods and properties on COM objects. Of course, scripting your application is useful in itself. However, these days Visual Basic has gone from being a simple interpreted language to being a full-blown development tool. But scripting is still a valuable tool, and it's used all over the place, especially in Web pages, as scriptlets buried inside some HTML code. In addition to providing the ability for someone to script your application or object using IDispatch, sometimes you want to incorporate the scripting engine as part of your application. For example, your users may not have access to a scripting environment. In that case, you would want to include a script parser with your application. Also, there are other scripting languages out there (including JScript®, VBScript, and more esoteric languages like Perl and Lisp) that you might want to use for writing scripts. That's where Active Scripting comes in. The idea behind Active Scripting is to abstract the mechanics of scripting behind some COM interfaces so that it's easy to create the client side of a scripting environment. In the early days of COM, your applications and objects became the objects used within a script. Active Scripting adds a new dimension by allowing you to include the script interpreter with your application. This month, I'll look at what it takes to implement an Active Scripting host. Active Script Engines
The Active Script Site
|
Figure 4: The Active Script Host Composite Control |
The Active Script Host
The Active Script host is where all the meat is. The job of the Active Script host is to:
The CActiveScriptSite class includes a function named InitScriptEngine that creates the VBScript engine component and retrieves the interface pointers necessary to drive the scripting engine. Figure 6 shows the InitScriptEngine function. Named Items and Type Information
The Active Scripting Callbacks
Conclusion
Have a question about programming in Visual Basic, Visual FoxPro, Microsoft Access, Office, or stuff like that? Send your questions via email to George Shepherd at 70023.1000@compuserve.com.
|
From the October 1999 issue of Microsoft Systems Journal.
|