With The Case Of  Release The Power OF  Visual C++ !   HomeProducts | PurchaseSupport | Downloads  
Download Evaluation
Pricing & Purchase?
E-XD++Visual C++/ MFC Products
Features Tour 
Electronic Form Solution
Visualization & HMI Solution
Power system HMI Solution
CAD Drawing and Printing Solution

Bar code labeling Solution
Workflow Solution

Coal industry HMI Solution
Instrumentation Gauge Solution

Report Printing Solution
Graphical modeling Solution
GIS mapping solution

Visio graphics solution
Industrial control SCADA &HMI Solution
BPM business process Solution

Industrial monitoring Solution
Flowchart and diagramming Solution
Organization Diagram Solution

Graphic editor Source Code
UML drawing editor Source Code
Map Diagramming Solution

Architectural Graphic Drawing Solution
Request Evaluation
ActiveX COM Products
Technical Support
  General Q & A
Discussion Board
Contact Us


Get Ready to Unleash the Power of UCanCode .NET


UCanCode Software focuses on general application software development. We provide complete solution for developers. No matter you want to develop a simple database workflow application, or an large flow/diagram based system, our product will provide a complete solution for you. Our product had been used by hundreds of top companies around the world!

"100% source code provided! Free you from not daring to use components because of unable to master the key technology of components!"

VC++ MFC Example: Adding VBScript and JScript support in your C++ applications

Introduce to MSSCRIPT.OCX and calling JScript and VBScript in your VC++ Application

Download ScriptDemo Demo Project - 31 Kb



I am always amazed to see how the script control (msscript.ocx) is fun to use and at the same time how VC++ developers reacted when it's time to use. Maybe the extension (.ocx) make them feel, it's visual basic! In this article, I would like to remove those frontiers and give some new reasons for VC++ developers to use it.


To use either VBScript or JScript is fairly simple in a VB and VC++ Application thanks to Microsoft's development efforts to create Windows Scripting technology. A developer only needs to know how to use the Microsoft Scripting ActiveX control (msscript.ocx) and how to pass value to a script method. For this reason, the first wrapper class that I want to identify is the CScriptObject. This wrapper is very simple to use and it provides most of the functionality that you will want to use in your application. It has a function to load script (text data) from a file or resource, get a list of methods name, selecting script language and to the execute function and statement. This class has no dependencies on MFC and can also be used in a console application. 

First of all to call a script it is important to know that VBScript and JScript deal only with VARIANT parameters. This is the reason I created the CSafeArrayHelper class. The CSafeArray helper wrapper class allows you to create parameters that you will pass to your script function.

class CSafeArrayHelper

    bool Create(VARTYPE  vt, UINT  cDims, UINT lBound, UINT cCount);
    bool Destroy();
    UINT GetDimension();

    bool Attach(LPSAFEARRAY psa);
    bool AttachFromVariant(VARIANT* pVariant);
    LPSAFEARRAY Detach();
    LPSAFEARRAY GetArray();
    bool AccessData(void FAR* FAR* pvData);
    bool UnaccessData();
    bool Lock();
    bool Unlock();
    bool PutElement(long lIndices, void FAR* vData);
    bool GetElement(long lIndices, void FAR* vData);
    VARIANT GetAsVariant();


It provides the exact same features that you will want to use with SAFEARRAY object but its usage may be simpler for some of us (like me!). The function GetAsVariant may be useful in case when you want to view the type of data that was encapsulated in your SAFEARRAY. This function could not provide ways to read all data types since the SAFEARRAY Data type (fFeatures) didn't implement it. Nonetheless to say, this function do a guess on the data types.

How to use

First to use this control, I will recommend you to take a look at the documentation for VBScript and JScript to know all you can do within your script function.

Writing a Script function

Let's say we want to create a simple function to convert temperature from Fahrenheit to Celsius.

In VBScript write:

Function Celsius(fDegrees)
   Celsius = (fDegrees - 32) * 5 / 9
End Function
or in JScript write:
function Celsius(fDegres)
   return (fDegres-32)*5/9;
To call this function, one only needs to store each parameter into VARIANT. Since your function (method) can have more than one parameter, a SAFEARRAY is needed to encapsulated them. In that latter case, you may want to view the parameter count for the array passed to your function by checking the .length property for string function or by some other means.
function CountParam(aParam)
    var strPresent = "Parameter is : " + (aParam.length>0 ? "Present": "Not present");
    return strPresent;
The same technique may be used in VBScript. This allows you to detect variable length argument at run time. To call a function without argument, a SAFERRAY is created but without parameter.

Calling a Script function

Your code can be as easy as this:

void CScriptDemoDlg::OnBtnExecute() 
    CString strParam, strProc;
    m_ctlParameter.GetWindowText( strParam );
    m_ctlFunctions.GetWindowText( strProc );

    CSafeArrayHelper sfHelper;
        _variant_t var;
        if (strProc.IsEmpty())
            sfHelper.Create(VT_VARIANT, 1, 0, 0);    // (void) parameter
            sfHelper.Create(VT_VARIANT, 1, 0, 1);    // 1 parameter
            var = _bstr_t(strParam);
        sfHelper.PutElement(0, (void*)&var);    // parameter1 -> index 0
        LPSAFEARRAY sa =  sfHelper.GetArray();
        _variant_t varRet;
        if (m_ScriptObj.RunProcedure(strProc, &sa, &varRet))
            m_ctlResult.SetWindowText( (LPCTSTR)(_bstr_t(varRet)) );
            CString strError = m_ScriptObj.GetErrorString();
            m_ctlResult.SetWindowText( strError );
        CString strError = m_ScriptObj.GetErrorString();
        m_ctlResult.SetWindowText( strError );

Some Ideas

Some of the ideas that you may want to try.
  1. You may want to have your script acts like a plugin, one suggestion is to have a resource script into a DLL and loads it at runtime (you may also have it part of your application). In that case, you will want to have specific module-related function, like: InitModule, ReleaseModule, btnOK_Click, btnCancel_Click, LoadUserData(strUsername), SaveUserData(strUserData), etc... and each of your DLL will have to implement them.

  2. You may have your script to do a complete and you will load the script file based on the (the CScriptObject class can load a script file for you!).

    Example: This script starts the "Calculator" program.

    function StartCalc()
     var WshShell = new ActiveXObject("WScript.Shell");
     var oExec = WshShell.Exec("calc");
         WshShell = null;
  3. You may want to create ActiveX object that lives longer than for a function call:
    var XML_Obj;
    function StartModule()
     XML_Obj = new ActiveXObject("Msxml.DOMDocument");
     XML_Obj.async = false;
    function StopModule()
     XML_Obj = null;
    function LoadSettings(strFilename)
  4. There are cases that you may want to execute the script code directly, just add the code, do not create a function...try it for fun!


Microsoft Windows Script Control
VBScript Documentation
JScript Documentation




Copyright ?1998-2009 UCanCode.Net Software , all rights reserved.
Other product and company names herein may be the trademarks of their respective owners.

Please direct your questions or comments to