tags:

views:

146

answers:

2

Is it possible to execute a specific sub in a vbs file from a c# application?

I have looked at creating a Process and then launching it but can not find a way to specify which particular sub in the script file should be executed. Is there a way to specify this or is there a better way of doing it?

A vbs could look something like the sample below. What I want is from the C# code to launch either test1 or test2.

Public Sub test1
    msgbox "Hey1"
End Sub

Public Sub test2
    msgbox "Hey2"
End Sub
+1  A: 

When using Visual Basic (without the .Net) I used the Script Control for this.

This page contains some documentation how to call it from good old VB.

Syntax looks something like this:

  Result = ScriptControl.Run("Name", arg1, arg2, ... argn)

Edit (after reading comment): If you do not need the VBScript to run in your context (if you do not share objects), you could create a small application that runs the script out-of-process. Your main app is 64 bit, the helper app is 32-bit. You can still pass simple parameters.

GvS
Thanks to GvS and Christian for the tip about the scriptcontrol, however this does unfortunately not seem to work in 64bit Windows 7. If I set the target as x86 the application runs fine and the script is executed, but as soon as I switch it over to Any CPU or x64 it can not import the COM object.Googeling for it brings up a number of posts about the MSScriptControl not working in a 64bit environment.Setting the application to be 32bit is not an option, the application should run in x64 mode on a 64bit os so I'm afraid that using the ScriptControl doesn't work for me.Any other ideas?
Mikael
+1  A: 

You use the Windows Script Control COM component and call its Run method. Here is a useful link with code examples.

Christian Hayter