Site Archive (Complete)
Windows/.NET
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
February 27, 2006
.NET Scripting Hosts, Part Thirteen

Mark M. Baker
Last time we began outfitting out .NET Scripting Host with basic debugger support modeled after the steps we follow when creating a COM-based Scripting Host. Let's take a look at how the code for those steps is integrated into our existing host.

First, we need to update how we reference the Process Debug Manager component. As I mentioned in an earlier issue, using certain interfaces from either the PDM type library, or from a type library created from the ActivScp.idl file using MIDL is problematic -- the IActiveScriptSite.GetItemInfo method requires that a type library be returned if available (actually an ITypeInfo interface for the object being returned). Unfortunately, the GetItemInfo definition that comes with the PDM type library and the ActiveScp.idl file is misdefined when an Interop library is created by Visual Studio. The last parameter is typed as an "out System.Type" when it needs to be an "IntPtr" instead to contain the marshaled ITypeInfo interface.

So this puts us in a bit of a pickle -- we'd like to use these ready-to-go interfaces but can't because of this flaw. The PDM Interop library also is missing the IActiveScriptParse interface which we need also. Finally, since the IActiveScript interface requires a host-implemented IActiveScriptSite interface, we can't use the PDM defined IActiveScriptSite if we create out own IActiveScript interface. This forces use to define the IActiveScript, IActiveScriptParse and IActiveScriptSite interfaces while using the remainder from the PDM interop library.

Since we're going to be using multiple interfaces from the PDM interop and ones we've created ourselves that conflict with the ones in the PDM, we need to fully qualify the names of the interfaces from the PDM we want to use. Rather than use the long name of the interop library, we'll create an alias called PDM for it:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using stdole;
using PDM = ProcessDebugManagerLib;
 
 namespace dotnet_scriptinghost
{
 
The next step is to add support for the interfaces we need to support for debugger support -- IActiveScriptSiteDebug, IApplicationDebugger, IDebugSessionProvider -- to the ones on our script host:

public class ScriptHost : 
   IActiveScriptSite, 
   PDM.IActiveScriptSiteDebug, 
   PDM.IApplicationDebugger,
   PDM.IDebugSessionProvider, 
   IActiveScriptSiteWindow
   {
 
Notice that the 3 debugger interfaces are inherited from the definitions in the PDM interop, and the remainder are defined in our namespace.

Now we need to begin augmenting the code to show how we use these debugger interfaces and the ones on the scripting engine itself. We also need to track the PDM object we create and the interfaces we get for later use. So we'll add some member variables to our class to track these:

 
   private PDM.IDebugDocumentHelper debugDocHelper;
   private uint debugDocCookie;
   private PDM.IDebugApplication debugApp;
   private PDM.IProcessDebugManager procDebugMgr;
 
We'll track a IDebugDocumentHelper interface, a IDebugApplication interface, the PDM object and a "cookie." This should be straightforward if you review the steps I outlined in the previous issue. We're now ready to create the PDM object and begin tracking those interfaces. We do this at the very beginning of our host initialization and before we create the script engine we want to work with:

public void Run()
    {
   try
   {
       // create PDM
       PDM.ProcessDebugManagerClass pdmObject = 
           new PDM.ProcessDebugManagerClass();
       this.procDebugMgr = 
           pdmObject as PDM.IProcessDebugManager;
 
       // get our IApplicationDebugger interface
       this.procDebugMgr.CreateApplication( 
           out this.debugApp );
       this.debugApp.SetName( "MyScriptHost" );
 
       // create the debug document helper via the PDM
       PDM.CDebugDocumentHelper ddhObject;
       this.procDebugMgr.CreateDebugDocumentHelper( 
           null,out ddhObject );
       this.debugDocHelper = 
           ddhObject as PDM.IDebugDocumentHelper;
 
       // initialize the DDH
       ddhObject.Init( 
           this.debugApp,"MyScriptHost","MyScriptHost",0 );
       ddhObject.Attach( null );
 
       // create a script engine
       JScript engine = new JScript();
 

The striking thing about this code is how much it follows the set of steps we follow when creating a COM-based Scripting Host. The order of the calls, the items returned and what we do with them afterwards is essentially the same. The PDM Interop library makes this a great deal easier too by defining the needed interfaces -- properly, as it turns out.

.NET Cast -- an Internet radio show (podcast) for .NET developers

Whether you are moving to .NET from Win32 or have been using .NET since 1.0, I encourage you to listen to .NET Cast, an Internet radio show that I host and produce for CMP Media. Hear from the people behind .NET and key experts in the industry working with it.

Listen to the show and send feedback to me at dotnetcast@sunburstsoftware.com!

.NET Migration

You can listen to 20 different .NET Cast episodes on the issues surrounding migration to .NET from Visual Basic 6.0. Hear from the experts at Microsoft and in the industry on best practices, approaches and tools you'll need when considering migration. In addition, you can get more information and help on the challenges of migration here.

Do you have a passion for Scripting?

If you have questions about how to use Active Scripting, I'd encourage you to read through my Active Scripting FAQ site that covers a wide range of problems other developers have had and many useful answers from their experiences.

A rich source of material on Active Scripting, JScript and VBScript from a true Microsoft insider is Eric Lippert's blog.

Whether you're just getting started with .NET or you've been using it since it was in beta, check out my Windows/.NET Q&A column published by DDJ.

Please feel free to email me at scripting@sunburstsoftware.com and let me know what you think of it, ways to improve on content, and how this newsletter is helping you.

Until next month... Peace.

Mark

TOP 5 ARTICLES
No Top Articles.
DR. DOBB'S CAREER CENTER
Ready to take that job and shove it? open | close
Search jobs on Dr. Dobb's TechCareers
Function:

Keyword(s):

State:  
  • Post Your Resume
  • Employers Area
  • News & Features
  • Blogs & Forums
  • Career Resources

    Browse By:
    Location | Employer | City
  • Most Recent Posts:



    MICROSITES
    FEATURED TOPIC

    ADDITIONAL TOPICS

    INFO-LINK



     



    Related Sites: DotNetJunkies, SD Expo, SqlJunkies