开发者

How to use the return value of a VBScript function into a VB application?

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.

Thi开发者_开发知识库s 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) 


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.


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) 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜