Tuesday, February 11, 2014

Hosting MSScriptControl in .NET

I am working on a project where I need to be able to execute VBScript from within a .NET application. Additionally, the VBScript needs to be able to call methods and manipulate values on a .NET object. There are several useful articles about how to do this:

I studied up and created a sample application, but the VBScript code was not able to see my object. I kept getting InvalidCastException whenever I tried to add my custom object using ScriptControl.AddObject. Using information from the above articles, and some trial and error, I was able to figure out the following requirements:
  1. Your object (and its methods/properties) needs to be public
  2. Your process must be an x86 process (COM Interop doesn't work well in x64)
  3. The assembly containing your class must be registered for COM interop (which means it must be a class library ... you cannot define the class within an executable and have it work)
With these requirements correctly implemented, the ScriptControl works well and can interact with my .NET object.

No comments:

Post a Comment