Click here to Skip to main content
15,887,683 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.7K   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

 
QuestionCode License Pin
Member 1063802210-Mar-14 23:57
Member 1063802210-Mar-14 23:57 
GeneralAdding a new Class in the ScripTestLib to add functions in the Script Editor Pin
brestian15-Jan-07 3:50
brestian15-Jan-07 3:50 
GeneralRe: Adding a new Class in the ScripTestLib to add functions in the Script Editor Pin
vccai7-Feb-07 3:02
vccai7-Feb-07 3:02 
GeneralRe: Adding a new Class in the ScripTestLib to add functions in the Script Editor Pin
confideman200010-Feb-12 21:56
confideman200010-Feb-12 21:56 
GeneralMulti-threading Pin
jkhax0r8-Nov-06 13:30
jkhax0r8-Nov-06 13:30 
GeneralCOM projects Pin
daniel remedi29-Jul-06 4:06
daniel remedi29-Jul-06 4:06 
GeneralLNK2001 Error for CreateEngine Pin
rbuchana5-Jul-06 6:46
rbuchana5-Jul-06 6:46 
GeneralRe: LNK2001 Error for CreateEngine Pin
rbuchana7-Jul-06 11:37
rbuchana7-Jul-06 11:37 
GeneralRe: LNK2001 Error for CreateEngine Pin
rbuchana14-Jul-06 6:39
rbuchana14-Jul-06 6:39 
GeneralRe: LNK2001 Error for CreateEngine Pin
brestian12-Jan-07 4:51
brestian12-Jan-07 4:51 
GeneralRe: LNK2001 Error for CreateEngine Pin
totobby31-Jan-07 6:38
totobby31-Jan-07 6:38 
GeneralRe: LNK2001 Error for CreateEngine Pin
sunbelt578-Feb-14 15:33
sunbelt578-Feb-14 15:33 
GeneralRe: LNK2001 Error for CreateEngine Pin
sunbelt578-Feb-14 15:49
sunbelt578-Feb-14 15:49 
GeneralIt seems headers in demo and source do not match Pin
zhaozh18-Aug-05 12:53
zhaozh18-Aug-05 12:53 
GeneralRe: It seems headers in demo and source do not match Pin
Alex Hazanov19-Aug-05 12:02
Alex Hazanov19-Aug-05 12:02 
GeneralMemory leaks Pin
arielevt12330-Jul-05 5:58
arielevt12330-Jul-05 5:58 
When you creates a new Object (OCX) and exit the program you will get a memory leaks for that object.
I found that its due to having getting the Unknown interface and give its value to the passing pointer in the function but never rlease this pointer I have copied the function and mark the lines I have added to avoid the memory leaks.

STDMETHODIMP CScripter::XActiveScriptSite::GetItemInfo(
/* [in] */ LPCOLESTR pstrName,
/* [in] */ DWORD dwReturnMask,
/* [out] */LPUNKNOWN* ppiunkItem,
/* [out] */LPTYPEINFO* ppti)
{
HRESULT hr = S_OK;

METHOD_PROLOGUE(CScripter, ActiveScriptSite)
USES_CONVERSION;

if (dwReturnMask & SCRIPTINFO_ITYPEINFO)
{
if (!ppti)
return E_INVALIDARG;
*ppti = NULL;
}

if (dwReturnMask & SCRIPTINFO_IUNKNOWN)
{
if (!ppiunkItem)
return E_INVALIDARG;
*ppiunkItem = NULL;
}

IDispatch* pDisp;

if (!(pThis->mapNamedItems.Lookup(OLE2CT(pstrName), (void*&) pDisp)))
{
return TYPE_E_ELEMENTNOTFOUND;
}
LPUNKNOWN pUnk = pDisp;
if (dwReturnMask & SCRIPTINFO_ITYPEINFO)
{
// Use IProvideClassInfo to get ITypeInfo of coclass!
IProvideClassInfo *pci = NULL;
hr = pUnk->QueryInterface(IID_IProvideClassInfo, (void**)&pci);
if (SUCCEEDED(hr) && pci)
hr = pci->GetClassInfo(ppti);

///////////////////freen the IUnknown interface /////////////////////////
pUnk->Release() ;
/////////////////////////////////////////////

if (FAILED(hr))
{
hr = pDisp->GetTypeInfo(0,0,ppti);
if (FAILED(hr))
return E_FAIL;
}
}

if (dwReturnMask & SCRIPTINFO_IUNKNOWN)
{
*ppiunkItem = pUnk;

///////////////////freen the IUnknown interface /////////////////////////
pUnk->Release() ;
/////////////////////////////////////////////

(*ppiunkItem)->AddRef(); // because returning
}

return S_OK;
}

Also I have changes a bitthe function CColorizer::SetLineColors to support object name coloring and changed the color in the Colorize header file

#define KEYWORD_COLOR RGB(0,0,160)
#define COMMENT_COLOR RGB(0,120,0)
#define CONSTANT_COLOR RGB(128,128,0)
#define OBJECT_COLOR RGB(128,0,0)
#define FUNCTION_COLOR RGB(44,130,163)
#define ERROR_COLOR RGB(255,0,0)
#define NORMAL_COLOR RGB(0,0,0)


void CColorizer::SetLineColors(int lineNum, CString &csString, vector<colorref>& colors)
{
// nothing to do
if (csString.GetLength() == 0)
{
colors.clear();
return;
}

// one COLORREF per input char
colors.resize(csString.GetLength());

// look for keywords

CString word;
word = csString;
word.TrimLeft();
word.TrimRight();
word = word.Left(3);

//set the line to all black
int k = 0;
int j = csString.GetLength();
COLORREF color = NORMAL_COLOR;
for (int i = 0; i < j; i++)
{
if (csString[i] == '\'')
{
color = COMMENT_COLOR;
}
if (lineNum == pScripter->errLine)
{
color = ERROR_COLOR;
}
colors.at(i) = color;
}
BOOL bFoundWord = FALSE ;
COLORREF colorFoundWord = NORMAL_COLOR ;
// do keyword coloring and auto-case correction
while( k < j)
{
// find "words" (sequences of alpha-numeric characters)
word.Empty();
while ((k < j) && (!__iscsym(csString.GetAt(k))))
{
k++;
}

int s = k;
while ((k < j) && (__iscsym(csString.GetAt(k))))
{
word = word + csString.GetAt(k);
k++;
}

int e = k;

word.TrimLeft();
word.TrimRight();
bFoundWord = TRUE ;

do
{
if (IsKeyWord(word))
{
colorFoundWord = KEYWORD_COLOR ;
break ;
}
if (pScripter->IsLangKeyWord(word))
{
colorFoundWord = FUNCTION_COLOR ;
break ;
}
if (pScripter->IsObject(word))
{
colorFoundWord = OBJECT_COLOR ;
break ;
}
bFoundWord = FALSE ;
}while (0) ;

if (bFoundWord == TRUE)
{
int l = 0;
for(i = s; i < e; i++)
{
l++;
if (colors.at(i) == NORMAL_COLOR)
colors.at(i) = colorFoundWord;
}
if (pScripter->m_bCapitalizeScript)
{
csString.Delete(s, e - s);
csString.Insert(s, word);
}
}
}
}
GeneralRe: Memory leaks Pin
arielevt1235-Aug-05 8:02
arielevt1235-Aug-05 8:02 
GeneralCreate a new function Pin
arielevt12310-Jul-05 10:08
arielevt12310-Jul-05 10:08 
GeneralRe: Create a new function Pin
Alex Hazanov10-Jul-05 21:25
Alex Hazanov10-Jul-05 21:25 
QuestionAnd what about Lua ? Pin
Kochise18-Feb-05 5:10
Kochise18-Feb-05 5:10 
AnswerRe: And what about Lua ? Pin
Alex Hazanov19-Feb-05 6:47
Alex Hazanov19-Feb-05 6:47 
GeneralBUG&amp;#65311; Pin
cnmusic31-Jul-04 23:41
cnmusic31-Jul-04 23:41 
GeneralProblem w/ events Pin
De Nardis Andrea20-Jul-04 0:01
De Nardis Andrea20-Jul-04 0:01 
GeneralRe: Problem w/ events Pin
Alex Hazanov19-Feb-05 6:40
Alex Hazanov19-Feb-05 6:40 
GeneralRe: Problem w/ events Pin
De Nardis Andrea20-Feb-05 21:31
De Nardis Andrea20-Feb-05 21:31 

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.