Figure 1 The IScriptControl Interface


interface IScriptControl : IDispatch {
 [propget]
 HRESULT Language([out, retval] BSTR* pbstrLanguage);
 [propput]
 HRESULT Language([in] BSTR pbstrLanguage);
 [propget]
 HRESULT State([out, retval] ScriptControlStates* pssState);
 [propput]
 HRESULT State([in] ScriptControlStates pssState);
 [propput]
 HRESULT SitehWnd([in] long phwnd);
 [propget]
 HRESULT SitehWnd([out, retval] long* phwnd);
 [propget]
 HRESULT Timeout([out, retval] long* plMilleseconds);
 [propput]
 HRESULT Timeout([in] long plMilleseconds);
 [propget]
 HRESULT AllowUI([out, retval] VARIANT_BOOL* pfAllowUI);
 [propput]
 HRESULT AllowUI([in] VARIANT_BOOL pfAllowUI);
 [propget]
 HRESULT UseSafeSubset([out, retval] VARIANT_BOOL* pfUseSafeSubset);
 [propput]
 HRESULT UseSafeSubset([in] VARIANT_BOOL pfUseSafeSubset);
 HRESULT Modules([out, retval] IScriptModuleCollection** ppmods);
 HRESULT Error([out, retval] IScriptError** ppse);
 HRESULT CodeObject([out, retval] IDispatch** ppdispObject);
 HRESULT Procedures([out, retval] 
         IScriptProcedureCollection** pdispProcedures);
 HRESULT _AboutBox();
 HRESULT AddObject([in] BSTR Name, 
         [in] IDispatch* Object, 
         [in, optional, defaultvalue(0)] VARIANT_BOOL AddMembers);
 HRESULT Reset();
 HRESULT AddCode([in] BSTR Code);
 HRESULT Eval([in] BSTR Expression,     
              [out, retval] VARIANT* pvarResult);
 HRESULT ExecuteStatement([in] BSTR Statement);
 HRESULT Run([in] BSTR ProcedureName, 
             [in] SAFEARRAY(VARIANT)* Parameters, 
             [out, retval] VARIANT* pvarResult);
};
Figure 2 IScriptControl Properties and Methods
Properties
Description
AllowUI
Specifies whether or not the script or the ScriptControl can access the UI.
CodeObject
Returns an IDispatch pointer for calling public members of a module.
Error
Returns an Error object containing detailed information about the last error that occurred.
Language
Sets or returns the name of the scripting language being used.
Modules
Returns the collection of modules for the ScriptControl object.
Procedures
Returns the collection of the procedures defined in a specified module.
SitehWnd
Sets or returns the hWnd of the window used by executing scripting code to display dialog boxes and other user interface elements.
State
Sets or returns the mode of the ScriptControl object.
Timeout
Sets or returns the time in milliseconds, after which a user is presented with the option to discontinue scripting code execution or allow it to continue.
UseSafeSubset
Sets or returns a Boolean value indicating whether the host application is concerned about safety. The objects and procedures that can't be used when UseSafeSubset is True are identical to those restricted by the browser's highest security setting.
Methods
Description
AddCode
Adds code to a module.
AddObject
Adds a named object to the namespace.
Eval
Evaluates an expression.
ExecuteStatement
Executes a single statement.
Reset
Discards all scripting code and objects that have been added to the ScriptControl.
Run
Runs a specified procedure.
Figure 4 The IScriptModule Interface

interface IScriptModule : IDispatch {
 [propget] 
 HRESULT Name([out, retval] BSTR* pbstrName);
 [propget] 
 HRESULT CodeObject([out, retval] IDispatch** ppdispObject);
 [propget] 
 HRESULT Procedures([out, retval]
         IScriptProcedureCollection** ppdispProcedures);
 HRESULT AddCode([in] BSTR Code);
 HRESULT Eval([in] BSTR Expression, [out, retval] VARIANT* pvarResult);
 HRESULT ExecuteStatement([in] BSTR Statement);
 HRESULT Run([in] BSTR ProcedureName, 
             [in] SAFEARRAY(VARIANT)* Parameters, 
             [out, retval] VARIANT* pvarResult);
};
Figure 5 The IScriptError Interface

interface IScriptError : IDispatch {
 [propget]
 HRESULT Number([out, retval] long* plNumber);
 [propget]
 HRESULT Source([out, retval] BSTR* pbstrSource);
 [propget]
 HRESULT Description([out, retval] BSTR* pbstrDescription);
 [propget]
 HRESULT HelpFile([out, retval] BSTR* pbstrHelpFile);
 [propget]
 HRESULT HelpContext([out, retval] long* plHelpContext);
 [propget]
 HRESULT Text([out, retval] BSTR* pbstrText);
 [propget] 
 HRESULT Line([out, retval] long* plLine);
 [propget]
 HRESULT Column([out, retval] long* plColumn);
 HRESULT Clear();
};
Figure 6 IScriptError Information
Properties and Methods
Description
Column
Returns a column location indicating the approximate place in the scripting code where an error occurred
HelpContext
Returns the context ID number for a topic in a Microsoft Windows Help file
HelpFile
Returns a string expression that is the fully qualified path to a Microsoft Windows Help file
Line
Returns a line location indicating the place in scripting code where an error occurred
Source
Returns a string expression specifying the type of error that occurred
Text
Returns a string containing a snippet of the scripting code surrounding the location where an error occurred in code
Number
Returns a numeric value specifying a runtime error
Clear
Clear the error object
Figure 7 Utilizing the ScriptControl
Figure 7 Utilizing the ScriptControl
Figure 8 Adding to the ScriptControl's Namespace

HRESULT CUsemsscriptctlDlg::CreateCOMObject(CString strProgID, LPDISPATCH* ppDisp)
{
   USES_CONVERSION;

   CLSID clsid;

   CLSIDFromProgID(T2COLE(strProgID), &clsid);

   return CoCreateInstance(clsid, NULL, 
                           CLSCTX_ALL, 
                           IID_IDispatch, 
                           (void**)ppDisp);
}

void CUsemsscriptctlDlg::OnAddobject() 
{
   UpdateData(TRUE);    

   IDispatch* pDisp = 0;

   if(SUCCEEDED(CreateCOMObject(m_strProgID, &pDisp)))
   {    
      m_scriptCtl.AddObject(m_strObjectName, pDisp, TRUE);
   }
}
Figure 9 Global Module Procedures

void CUsemsscriptctlDlg::ShowProcedures()
{
   m_lbProcedures.ResetContent();
   CScriptProcedureCollection procedureCollection;
    
   procedureCollection = m_scriptCtl.GetProcedures();

   long l = procedureCollection.GetCount();

   for(long x = 1; x < l + 1; x++)
   {
      COleVariant v;
      v = x;

      CScriptProcedure procedure;

      TRY {
         procedure = procedureCollection.GetItem(v);
         this->m_lbProcedures.AddString(procedure.GetName());
      }
      CATCH_ALL(e)
      {
      }
      END_CATCH_ALL
   }
}
Figure 10 Obtaining Error Information

void CUsemsscriptctlDlg::OnErrorScriptcontrol1() 
{
   CScriptError scriptError;
    
   scriptError = m_scriptCtl.GetError();

   long lNumber = scriptError.GetNumber();
   CString strDescription = scriptError.GetDescription();
   CString strText = scriptError.GetText();
   long lLine = scriptError.GetLine();
   long lColumn = scriptError.GetColumn();
   scriptError.Clear();

   CString strErrorMsg;
   strErrorMsg.Format("Error #%ld, %s, %s, Line: %ld, Column: %ld",
                      lNumber, strDescription, 
                      strText, lLine, lColumn);
   AfxMessageBox(strErrorMsg);
}
Figure 11 Displaying Error Information
Figure 11 Displaying Error Information