Click here to Skip to main content
15,887,214 members
Articles / Desktop Programming / MFC
Article

Embeddable script editor for MFC applications

Rate me:
Please Sign up or sign in to vote.
4.90/5 (60 votes)
15 Jul 20032 min read 335.6K   6.4K   130   124
A library that allows you to embed scripting functionality to your C++ MFC application.

Image 1

Objects Events Listing

Sample screenshot

Pop-Up Object properties and methods list

Sample screenshot

IntelliSense - like function parameters tooltips

Introduction

I've been searching the net for a suitable and affordable solution that will allow me to embed scripting functionality in my C++ application. What I found was either not sufficient or cost a lot of money.

So I've decided to make one of my own. After about a month of work and testing I've come up with this embeddable scripter.

I made it a separate DLL , which is really easy to use.

Usage

  1. Download the source code, and compile it. Make sure to compile the correct version (Debug\Release) or both.
  2. Include ScriptEditor.h in your project and add the library ScriptEditor.lib to your additional libraries. Make sure to reference the correct version! Debug version for debugging and release version for release!
  3. Copy the DLL to the same folder as your executable.
  4. In your code create an instance of CScripter and initialize it.
  5. Add your objects to the script and optionally the script text.

Example

In the header of your main application window add:

#include "..\ScriptEditor\Scripter.h"

Add member variable:

CScripter scripter;

Then when initializing add:

scripter.CreateEngine("VBScript");

After that add your objects to the script:

scripter.AddObject("MPlayer",(IDispatch *)m_mediaPlayer.GetControlUnknown());
scripter.AddObject("PlayButton", 
      (IDispatch *)m_commandButton.GetControlUnknown());
scripter.AddObject("TabStrip",(IDispatch *)m_tabStrip.GetControlUnknown());
scripter.AddObject("SimpleObject",m_simpleObject.GetIDispatch(TRUE));

And optionally set the script text:

scripter.scriptText = "MsgBox \"Test message\"";

After that the script is ready to run, you now may do one of the following:

// Run the script
scripter.StartScript();

// Stop the script execution

scripter.StopScript();

// Open the script editor window

scripter.LaunchEditor();

// Reset the script (All added objects are removed and engine is recreated)

scripter.Reset();

History

This is the first release so it's probably not bug free. I've done my best to track any bugs but there are always surprises. So I will post here any additions, patches and fixes that will follow.

  • 19/07/03 - The DLL source was updated to compile in VS.NET (read the messages below)

Credits

  • Gil Eizenburg for debugging and testing.
  • Smaller Animals Software, Inc. for ColorEditWnd class.
  • Daniel Madden for CTokenEx class.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) RDV Systems
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: I get "Permission Denied" error Pin
Jason Troitsky (was Hattingh)24-Dec-03 0:11
Jason Troitsky (was Hattingh)24-Dec-03 0:11 
GeneralRe: I get "Permission Denied" error (solution found) Pin
Jason Troitsky (was Hattingh)24-Dec-03 0:48
Jason Troitsky (was Hattingh)24-Dec-03 0:48 
GeneralRe: I get "Permission Denied" error (solution found) Pin
Alex Hazanov24-Dec-03 5:49
Alex Hazanov24-Dec-03 5:49 
GeneralRe: I get "Permission Denied" error (solution found) Pin
Jason Troitsky24-Dec-03 5:53
Jason Troitsky24-Dec-03 5:53 
GeneralRe: I get "Permission Denied" error (solution found) Pin
Jason Troitsky24-Dec-03 5:59
Jason Troitsky24-Dec-03 5:59 
GeneralRe: I get "Permission Denied" error (solution found) Pin
Alex Hazanov24-Dec-03 10:59
Alex Hazanov24-Dec-03 10:59 
GeneralRe: I get "Permission Denied" error (solution found) Pin
Jason Troitsky24-Dec-03 14:09
Jason Troitsky24-Dec-03 14:09 
GeneralRe: I get "Permission Denied" error (could this be the solution?) Pin
Jason Troitsky7-Jan-04 9:29
Jason Troitsky7-Jan-04 9:29 
see:

DDJ Article[^]

Does this help??


For possibly easier/quicker info, the text in there says:


(1) why does he get the "Permission Denied" message box from the scripting engine when a MsgBox function is called within his VBScript script; and

(2) how does he programmatically deny access to UI behaviors within his script, as the Microsoft Scripting Control does with its AllowUI property.

These are interesting questions, and are particularly important when building a host that will run on a server or other environment that doesn't permit UI.

Here is my response to Shyam:

The reason you can't see MsgBox's in your application is that you didn't implement QueryInterface. There is no way for the scripting engines to know that you derived your host from the interfaces you want to support. It will call QI on your host and ask it for a particular interface. If it needs to know whether to display UI to the user (such as a MsgBox) it will QI for the IActiveScriptSiteWindow interface. If it gets the interface back, it assumes UI is allowed. If not, it assumes it isn't.

This also should lead you to how to implement a "AllowUI" feature. You simply test for the AllowUI setting in the QI implementation. If AllowUI is True, then you'd return the IActiveScriptSiteWindow interface and S_OK. If not, you'd return E_NOINTERFACE. This would properly tell the engine whether you want to allow UI or not.

The technique of relying on whether an interface exists in an object to decide whether it allows for a behavior is a normal COM practice found in many COM objects. However, the concept of supporting the interface based on a Boolean property is more suspect. In fact, this approach violates one of the three rules governing how interfaces are supposed to be managed by COM objects. Here is that rule, directly from the Microsoft Developer Network Library:

The Set of Interfaces on an Object Instance Must Be Static

The set of interfaces accessible on an object via QueryInterface must be static, not dynamic. Specifically, if QueryInterface returns S_OK for a given IID once, it must never return E_NOINTERFACE on subsequent calls on the same object; and if QueryInterface returns E_NOINTERFACE for a given IID, subsequent calls for the same IID on the same object must never return S_OK.

Clearly, returning an interface (or not) based on an internal property of the object where the property is configurable by the programmer is an improper way to allow for this behavior. My guess is that this behavior was added quickly by an engineer at Microsoft to allow scripting to be safely used on a server. A better technique would have been to derive a new interface from IActiveScriptSiteWindow called IActiveScriptSiteWindow2 and add a method named AllowUI. This would have made solving Shyam's problem straightforward and would have respected proper COM etiquette.

However, there's the proper way and then there's getting the job done. I hope this technique will be useful to the reader and possibly open up new avenues for exploration in Active Scripting to see what other undocumented or semi-undocumented behavior may exist in the engines.



regards,

Jason Hattingh
http://www.greystonefx.com
GeneralReference Counting Problem Pin
Danver Hartop5-Dec-03 10:39
Danver Hartop5-Dec-03 10:39 
GeneralRe: Reference Counting Problem Pin
Alex Hazanov6-Dec-03 21:53
Alex Hazanov6-Dec-03 21:53 
GeneralRe: Reference Counting Problem Pin
Danver Hartop8-Dec-03 8:34
Danver Hartop8-Dec-03 8:34 
GeneralRe: Reference Counting Problem Pin
Alex Hazanov9-Dec-03 9:36
Alex Hazanov9-Dec-03 9:36 
QuestionHow can I add my own objects ? Pin
Tony D.27-Nov-03 9:22
Tony D.27-Nov-03 9:22 
GeneralFeatures request 2 Pin
M.Khadem13-Nov-03 16:22
M.Khadem13-Nov-03 16:22 
GeneralRe: Features request 2 Pin
Anonymous16-Nov-03 20:21
Anonymous16-Nov-03 20:21 
GeneralRe: Features request 2 Pin
Alex Hazanov16-Nov-03 20:24
Alex Hazanov16-Nov-03 20:24 
GeneralRe: Features request 2 Pin
M.Khadem19-Nov-03 0:25
M.Khadem19-Nov-03 0:25 
GeneralRe: Features request 2 Pin
Alex Hazanov22-Nov-03 0:46
Alex Hazanov22-Nov-03 0:46 
GeneralAdding TypeLib Support Pin
Danver Hartop25-Oct-03 20:47
Danver Hartop25-Oct-03 20:47 
GeneralRe: Adding TypeLib Support Pin
Alex Hazanov16-Nov-03 20:31
Alex Hazanov16-Nov-03 20:31 
GeneralIncremental re-parseing is needed. Pin
Ed Deighton25-Oct-03 6:05
Ed Deighton25-Oct-03 6:05 
GeneralRe: Incremental re-parseing is needed. Pin
Ed Deighton26-Oct-03 15:39
Ed Deighton26-Oct-03 15:39 
QuestionOnly English?? Pin
mutter2124-Oct-03 16:51
mutter2124-Oct-03 16:51 
AnswerRe: Only English?? Pin
Alex Hazanov25-Oct-03 13:20
Alex Hazanov25-Oct-03 13:20 
GeneralRe: Only English?? Pin
mutter2126-Oct-03 3:46
mutter2126-Oct-03 3:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.