This project has moved and is read-only. For the latest updates, please go here.

Problem Calling VBScript Function

Jan 3, 2014 at 3:22 PM
I have a very simple app at this time. I am developing in VB.NET (CLR 4.5) and using the VBScript Engine. My application is to be used for industrial process simulations so my .Net application will call the same script method (Sub Main) roughly every second. The code in VB.Net looks like this.
In VBScript
Public Sub Main()
  Dim x
  x = 5
End Sub
Right now I am just using a button to test this. The first time I click the button, I Execute the entire script
On subsequent button pushes I call Main
And I get the following error

"Exception has been thrown by the target of an invocation."

I have found that the following works fine however I read a post here that recommends against invoking script this way for performance reasons. Because my application will likely call many different Sub Main() scripts every second, performance will be very important.
I have stripped this down as much as possible and still can't get it to work as recommended - any suggestions?
Jan 3, 2014 at 6:18 PM

We're having trouble reproducing your issue. We just ran this test program without any problems:
Module ConsoleTest
    Sub Main()
        Using engine As New Microsoft.ClearScript.Windows.VBScriptEngine
            engine.Execute("Public Sub Main() : Dim x : x = 5 : End Sub")
        End Using
    End Sub
End Module
Can you think of anything you're doing that might be significantly different?

Also, the exception text you posted indicates TargetInvocationException, which should always have a non-null InnerException property. Can you examine that and tell us what you find?

Jan 5, 2014 at 6:09 AM
I copied your code into my app and executed on a Windows form Button_Click and I get the same error as before. The InnerException says.
Public member 'Main' on type 'VBScriptTypeInfo' not found.
I am using Clearscript Version that I installed as a NuGet package.

I then created a new console app in, installed ClearScript NuGet package and copied your code directly into my Sub Main. When I run the app, it generates the same error message when trying to execute engine.Script.Main(). In this case, because I created a bare bones console app with nothing more than the code you provided, I can't think of anything I am doing that may be different in any way.

Does ClearScript have other dependencies that may not be correct on my machine? Are you using ClearScript
Jan 5, 2014 at 1:17 PM
Hi again!

We've reproduced this issue, and you nailed it - in our previous tests we used a newer ClearScript version (not yet released) where this issue is already fixed. We'll get the fix out ASAP.

This bug only affects parameterless functions. You can work around it by adding a dummy parameter:
Using engine As New Microsoft.ClearScript.Windows.VBScriptEngine
    engine.Execute("Public Sub Main(dummy) : Dim x : x = 5 : End Sub")
End Using
Thanks again, and good luck!
Jan 6, 2014 at 3:14 AM
That's great news. I was beginning to think there may be a problem with my system or I was going crazy.

Any time
frame on getting the fix sent out?
Jan 6, 2014 at 1:27 PM
We're currently on track to post the ClearScript 5.3.11 source code later this week.
Jan 16, 2014 at 4:33 AM
Any news on v 5.3.11?
Jan 16, 2014 at 2:17 PM
The source code for Version 5.3.11 is now up. Cheers!