views:

31

answers:

2

I'm developping an ASP.NET application, and I need to use an old VBScript file that would be really hard to translate into pure VB in a VB function.

This VBScript is supposed to return an array that I'd like to use it in the VB function that called it.

How could I call the VBScript function in my VB application and use the value it returned?

I've search how to do so through many forum and FAQ, but it seems that these results where mainly focused on old version of the .NET framework.

If someone could explain it to me through a very basic example, I'll be very grateful.


I tried to use the following code for calling the VBS:

Dim WShell As Object 
WShell = CreateObject("WScript.Shell") 
WShell.Run("F:/SERVER/IIS/MyScript.vbs", 1, 0) 
A: 

I tried to use the following code for calling the VBS:

Dim WShell As Object  
WShell = CreateObject("WScript.Shell")  
WShell.Run("F:/SERVER/IIS/MyScript.vbs", 1, 0) 
Tom
@Ben McCormack: Yep, that was a responce to Sane, but it seems that I can't edit or comment my question, so I choose the only choice left: reply. I guess it's because I'm an unregistred user? Anyway, thanks for your edit!
Tom
@Tom: You've accidentally created two user accounts (http://stackoverflow.com/users/428242/tom and http://stackoverflow.com/users/428356/tom). E-mail [email protected] and ask the team to merge them.
Helen
+1  A: 

VBScript is a few steps removed from VB.NET, so it's going to be difficult to intermingle both languages without a good bit of refactoring to get the code to play well in VB.NET. I see three ways you might approach this solution:

  1. Refactor the code to work in VB.NET
    • As you mentioned, this might require a lot of work, but you have to decide whether or not the additional work required to refactor the code is outweighed by the complexity of introducing other dependencies into your solution. You should also gain a performance benefit by having your code run exclusively in a managed environment instead of crossing over HTTP or COM boundaries.
  2. Embed the script into a classic ASP page and use HTTP messages to communicate between your VB.NET application and the ASP page
    • VBScript is a natural complement to classic ASP so your script should "just work" in an ASP page. You can craft your ASP page to accept input via a query string and output the data as text that your VB.NET application can understand. You didn't mention whether or not you are making an ASP.NET web site or a different type of VB.NET application, but if you're building an ASP.NET web site, this might be something to look at since you'll already be building a web site.
  3. Use the VB6/VBA Script Control in a COM component and use COM interop to communicate with the component in VB.NET
    • This is where it starts to get really complicated. Before the release of Visual Basic 6 (VB6), Microsoft released Script control that could be embedded within VB applications and run custom user scripts. In theory, you could create a new VB6 component and reference Microsoft Script Control 1.0, use a new Script object that calls your script, and use COM interop to call the VB6 component from within your VB6 application. That's an awful lot of work and several layers of dependencies to run your script, and I would only recommend this option if your script is literally thousands of lines of code. You're going to pay a performance penalty each time you cross the COM/.NET boundary, so you'd need to be sure you craft your solution quite carefully.

Personally, I would look at refactoring the code to VB.NET code as my first choice. While you do have to do more work up front, you ensure that you are taking advantage of the performance benefits of running exclusively in managed code and you remove the need for additional dependencies in your application. If the script is truly so monstrous that it would be a great deal of work to convert to VB.NET, then you might look at the other tool options.

Ben McCormack
+1 Converting the code to VB.Net is probably the best bet. I suppose option 4 could be converting the VBScript into VB6 in an ActiveX DLL and then calling that through COM Interop. VBScript is 99% identical to VB6 so that should be easy: the code might work unchanged. I still think converting VBScript to VB.Net would be easier. What happens if you run the VB6 upgrade wizard against the VBScript as if it were VB6?
MarkJ