Microsoft KB Archive/232394

From BetaArchive Wiki
Knowledge Base


How to catch run-time errors in an ActiveX Script Host

Article ID: 232394

Article Last Modified on 1/9/2006



APPLIES TO

  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition
  • Microsoft Visual C++ .NET 2002 Standard Edition
  • Microsoft Visual C++ 6.0 Professional Edition
  • Microsoft Visual C++ 6.0 Enterprise Edition
  • Microsoft Visual C++ 6.0 Standard Edition
  • Microsoft Visual C++ 5.0 Professional Edition
  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual Basic, Scripting Edition 5.0
  • Microsoft Visual Basic, Scripting Edition 4.0
  • Microsoft Visual Basic, Scripting Edition 3.0



This article was previously published under Q232394

Note Microsoft Visual C++ 2005, Microsoft Visual C++ .NET 2003, and Microsoft Visual C++ .NET 2002 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code.

SUMMARY

Like all programs, scripts running in an ActiveX Script Host can throw two kinds of errors, compile-time and run-time. In earlier implementations, the ActiveX Scripting Engines provided by Microsoft (VBScript and JScript), made no distinction between the two types of errors. Both were handled in IActiveScriptSite::OnScriptError(). With more recent versions of the script engines, a distinction was made between run-time and compile-time errors. Compile-time errors, such as syntax errors, are still reported to the ActiveX Script Host using the IActiveScriptSite::OnScriptError() method. However, run-time errors, such as passing invalid arguments, are not directly reported to OnScriptError(). Instead, they are reported to a different method, IActiveScriptSiteDebug::OnScriptErrorDebug().

This article describes how to modify an existing ActiveX Script Host to catch run-time errors thrown from scripts.

MORE INFORMATION

This article assumes that you've already written an ActiveX Script Host. For more information on doing this, please see the articles noted in the "References" section of this article.

In the newest releases of the script engines, IActiveScriptSiteDebug::OnScriptErrorDebug is called when a run-time error occurs. The IActiveScriptSiteDebug interface gives the Script Host a chance to participate in debugging before the debugger is involved. In order for the Script Host to be notified when a run-time error occurs, a minimal implementation of IActiveScriptSiteDebug is required.

When the IActiveScript::SetScriptSite method is called, the script engine will QueryInterface the Host's IActiveScriptSite pointer for the IActiveScriptSiteDebug interface. If this fails, the script engine will attempt to contact the script debugger on its own. However, if the QueryInterface is successful, the script engine will then call IActiveScriptSiteDebug::GetApplication() to establish the debugging facilities for the scripting session. If IActiveScriptSiteDebug::GetApplication() fails, the script engine will conclude that debugging is not available on the machine, and revert to IActiveScriptSite::OnScriptError() for all error handling.

This is the IActiveScriptSiteDebug interface, with a minimal implementation:

   STDMETHOD(GetDocumentContextFromPosition)(
      DWORD dwSourceContext, 
      ULONG uCharacterOffset, 
      ULONG uNumChars, 
      IDebugDocumentContext **ppsc) {return E_NOTIMPL;}

   STDMETHOD GetApplication( IDebugApplication **ppda ) {return E_NOTIMPL;}

   STDMETHOD(GetRootApplicationNode)( IDebugApplicationNode **ppdanRoot) {return E_NOTIMPL;}
   
   STDMETHOD(OnScriptErrorDebug)( 
      IActiveScriptErrorDebug *pErrorDebug, 
      BOOL *pfEnterDebugger,
      BOOL *pfCallOnScriptErrorWhenContinuing) {return E_NOTIMPL;}
                

Note In Visual C++ 2005, you must add the common language runtime support compiler option (/clr:oldSyntax) to successfully compile the previous code sample. To add the common language runtime support compiler option, follow these steps:

  1. Click Project, and then click ProjectName Properties.


Note ProjectName is a placeholder for the name of the project.

  1. Expand Configuration Properties, and then click General.
  2. In the right pane, click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project settings.
  3. Click Apply, and then click OK.

For more information about common language runtime support compiler options, visit the following Microsoft Developer Network (MSDN) Web site:

Alternately, a Script Host may choose to have a more complete implementation of the IActiveScriptSiteDebug interface. For documentation about the IActiveScriptSiteDebug interface, visit the following MSDN Web site:

REFERENCES

For more information about building ActiveX Script, click the following article number to view the article in the Microsoft Knowledge Base:

223139 How to add support for hosting VBScript to an ATL application


183698 Axsh.exe demonstrates how to implement an active scripting host


168214 MFCAxs.exe implements an Active Script host using MFC


223389 Scripting.exe file contains the headers and libraries that are necessary to create ActiveX Script hosts and engines


Keywords: kbhowto kbactivexscript KB232394