65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 VBScript 脚本化 .NET 应用程序

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.78/5 (9投票s)

2006 年 9 月 28 日

GPL3

3分钟阅读

viewsIcon

93740

downloadIcon

2980

允许在应用程序中使用用户提供的或其他外部 VBScript。

引言

mScript 是一个 .NET 类,它允许您使用 VBScript 动态地脚本化代码的某些部分。您可以将多个变量传递给脚本,执行任何必要的处理,并从脚本返回多个变量。

使用 mScriptable

在添加对 mScriptable.dll 程序集的引用后,您可以使用/导入命名空间

using mScriptable;

然后,您可以开始创建一个位于 mScriptable 命名空间中的 mScript 类的实例。

mScript script = new mScript()

接下来,您需要提供脚本需要访问的变量。

  script.addScriptInput("foo", "Hello");
  script.addScriptInput("bar", "World");
  script.addScriptInput("x", 100);
  script.addScriptInput("y", 13);

并为 script 对象分配您的脚本代码。

  string myScriptCode = "...";
  script.setScript(myScriptCode);

您的脚本代码必须是有效的 VBScript。脚本中的任何错误都将被 Windows Scripting Host 捕获,而不是控件。目前,Windows Scripting Host 进程的返回值未被监控以确定脚本是否成功完成,因此捕获自己的错误非常重要。您的 VBScript 可以使用提供的 inpVal(varName) 函数或缩写包装函数 iv(varName) 来检索提供给它的值。可以使用提供的 return varName, varVal 子例程将值返回给 .NET 调用者。示例脚本可能如下所示:

  foo = iv("foo")
  bar = iv("bar")
  helloWorld = foo & " " & bar & "!"
  return "hwString", helloWorld

  x = iv("x")
  y = iv("y")

  calc1 = x * y
  return "calc1", calc1

  if y = 0 then
    MsgBox "Zero divide attempted!"
    calc2 = 0
  else 
    calc2 = x / y
  end if

  return "calc2", calc2

您可以使用 mScript 对象的 runScript() 方法执行您的脚本代码。此方法将返回一个 Hashtable,其中包含您脚本的所有返回值。

Hashtable rValues = script.runScript();

脚本完成后,runScript() 方法将返回一个包含脚本返回值的 Hashtable。在我们的示例中,您的 Hashtable 将包含以下内容:

Hash 键
(变量名)
Hash 值
(变量值)
hwString 你好世界!
calc1 1300
calc2 7.692307...

在您的 .NET 应用程序中,您可以使用 Hashtable 检索这些值。

  string returned = "";
  foreach (string rVar in rValues.Keys) {
    returned += rVar + " = " + rValues[rVar] + "\r\n";
  }
  MessageBox.Show(returned);

基本上就是这样了。提供的演示项目显示了该类使用的有效示例,允许您提供输入、修改 VBScript、执行,然后查看输出。

工作原理

mScriptable 依赖于 Windows Scripting Host 来实现其 VBScript 功能。因此,每次调用 runScript() 时,都会产生启动 Windows Scripting Host 进程的开销。 .NET 组件和脚本主机之间的通信相当粗糙,但可行。每次运行都会创建一个新文件(称为 [timestamp].vbs)。该文件包含您提供的代码以及一些提供基本变量值检索和值返回功能以及文件 I/O 的头代码。您的脚本对 return 子例程的每次调用都会将制表符分隔的名称/值对倾印到另一个名为 [timestamp].txt 的文件中。脚本退出时,.NET 模块从该文件中读取值,并将其提供给 Hashtable。脚本完成后,并且检索到值后,将删除 .vbs.txt 文件。

更新!(2007/4/5)

应用户要求,mScriptable 已进行修改,现在可以运行用户的独立脚本。如果您想直接在 WSH 中使用 VBScript,并通过 mScriptable 的 .NET 应用程序使用,现在可以了。您需要对脚本进行的更改如下:

  1. 作为脚本输入的任何内容都必须从名为 inpVal()iv() 的函数中获取其值。
  2. 您的脚本返回的任何输出都必须使用名为 return() 的子例程返回。
  3. 您必须为 inpVal() 和/或 iv() 以及 return() 子例程创建虚拟函数。

为了防止 VBScript 代码中重复声明函数/子例程,mScriptable 将删除任何名为 inpVal()iv()return() 的用户定义函数/子例程。

一个可以独立运行,也可以通过 mScriptable 运行的示例 VBScript 可能如下所示:

  ' My function to allow script to run via WSH directly
  Function iv(myVariable) 
    Dim retVal
    
    Select Case myVariable
      Case "foo"
        retVal = "John"
      Case "bar"
        retVal = "Doe"
      Case "x"
        retVal = 9
      Case "y"
        retVal = 3
      Case Else
        retVal = ""
    End Select
    
    iv = retVal
  End Function
  
  Sub return(varName, varVal)
    ' this is just a dummy function
    MsgBox varName & " = " & varVal
  End Sub
  
  foo = iv("foo")
  bar = iv("bar")
  helloWorld = foo & " " & bar & "!"
  return "hwString", helloWorld

  x = iv("x")
  y = iv("y")

  calc1 = x * y
  return "calc1", calc1

  if y = 0 then
    MsgBox "Zero divide attempted!"
    calc2 = 0
  else 
    calc2 = x / y
  end if

  return "calc2", calc2

当通过 WSH 直接执行时,此脚本将按预期运行。当通过 mScriptable 加载并运行时,iv() 函数和 return() 子例程将被删除,并替换为 mScriptable 自己的代码,该代码提供脚本输入。

© . All rights reserved.