C#(winform)调用VbScript脚本

解决方案

1.工程引用 Interop.MSScriptControl.dll

2.创建一个类,代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using MSScriptControl;

namespace myClass
{
/// <summary>
/// 脚本类型
/// </summary>
public enum ScriptLanguage
{
/// <summary>
/// JScript脚本语言
/// </summary>
JScript,

/// <summary>
/// VBscript脚本语言
/// </summary>
VBScript,

/// <summary>
/// JavaScript脚本语言
/// </summary>
JavaScript
}

/// <summary>
/// 脚本运行错误代理
/// </summary>
public delegate void RunErrorHandler();

/// <summary>
/// 脚本运行超时代理
/// </summary>
public delegate void RunTimeoutHandler();

/// <summary>
/// ScriptEngine类
/// </summary>
public class clsScriptEngine
{
private ScriptControl msc;
//定义脚本运行错误事件
public event RunErrorHandler RunError;
//定义脚本运行超时事件
public event RunTimeoutHandler RunTimeout;

/// <summary>
///构造函数
/// </summary>
public clsScriptEngine()
:
this(ScriptLanguage.VBScript)
{
}

/// <summary>
/// 构造函数
/// </summary>
/// <param name="language">脚本类型</param>
public clsScriptEngine(ScriptLanguage language)
{
this.msc =new ScriptControlClass();
//this.msc.UseSafeSubset = true; //使用安全的子集,如果等于true则无法连接数据库
this.msc.Language = language.ToString();
((DScriptControlSource_Event)
this.msc).Error += new DScriptControlSource_ErrorEventHandler(ScriptEngine_Error);
((DScriptControlSource_Event)
this.msc).Timeout += new DScriptControlSource_TimeoutEventHandler(ScriptEngine_Timeout);
}

/// <summary>
/// 运行Eval方法
/// </summary>
/// <param name="expression">表达式</param>
/// <param name="codeBody">函数体</param>
/// <returns>返回值object</returns>
public object Eval(string expression, string codeBody)
{
msc.AddCode(codeBody);
return msc.Eval(expression);
}

/// <summary>
/// 运行Eval方法
/// </summary>
/// <param name="language">脚本语言</param>
/// <param name="expression">表达式</param>
/// <param name="codeBody">函数体</param>
/// <returns>返回值object</returns>
public object Eval(ScriptLanguage language, string expression, string codeBody)
{
if (this.Language != language)
this.Language = language;
return Eval(expression, codeBody);
}

/// <summary>
/// 运行Run方法
/// </summary>
/// <param name="mainFunctionName">入口函数名称</param>
/// <param name="parameters">参数</param>
/// <param name="codeBody">函数体</param>
/// <returns>返回值object</returns>
public object Run(string mainFunctionName, object[] parameters, string codeBody)
{
this.msc.AddCode(codeBody);
return msc.Run(mainFunctionName, ref parameters);
}

/// <summary>
/// 运行Run方法
/// </summary>
/// <param name="language">脚本语言</param>
/// <param name="mainFunctionName">入口函数名称</param>
/// <param name="parameters">参数</param>
/// <param name="codeBody">函数体</param>
/// <returns>返回值object</returns>
public object Run(ScriptLanguage language, string mainFunctionName, object[] parameters, string codeBody)
{
if (this.Language != language)
this.Language = language;
return Run(mainFunctionName, parameters, codeBody);
}

/// <summary>
/// 放弃所有已经添加到 ScriptControl 中的 Script 代码和对象
/// </summary>
public void Reset()
{
this.msc.Reset();
}

/// <summary>
/// 获取或设置脚本语言
/// </summary>
public ScriptLanguage Language
{
get { return (ScriptLanguage)Enum.Parse(typeof(ScriptLanguage), this.msc.Language, false); }
set { this.msc.Language = value.ToString(); }
}

/// <summary>
/// 获取或设置脚本执行时间,单位为毫秒
/// </summary>


/// <summary>
/// 设置是否显示用户界面元素
/// </summary>
public bool AllowUI
{
get { return this.msc.AllowUI; }
set { this.msc.AllowUI = value; }
}

/// <summary>
/// 宿主应用程序是否有保密性要求
/// </summary>
public bool UseSafeSubset
{
get { return this.msc.UseSafeSubset; }
set { this.msc.UseSafeSubset = true; }
}

/// <summary>
/// RunError事件激发
/// </summary>
private void OnError()
{
if (RunError != null)
RunError();
}

/// <summary>
/// OnTimeout事件激发
/// </summary>
private void OnTimeout()
{
if (RunTimeout != null)
RunTimeout();
}

private void ScriptEngine_Error()
{
OnError();
}

private void ScriptEngine_Timeout()
{
OnTimeout();
}
}
}

3.创建一个名为vbs_Record.vbs 的VbScrpt,代码如下:

Function vbs_Record (strChId,strDate,strStartTime,strStopTime,strTimeLen,strTimeLenS,strCallType,strCallResult,strCallerId,strDtmf,strRecordFile,strSessionId)

msgbox strChId
& " , " & strDate & " , " & strStartTime & " , " & strStopTime & " , " & strTimeLen & " , " & strTimeLenS & " , " & strCallType & " , " & strCallResult & " , " & strCallerId & " , " & strDtmf & " , " & strRecordFile & " , " & strSessionId

Set cn
= CreateObject("ADODB.Connection")
strCn
= "provider=SQLOLEDB.1;" & _
"Persist Security Info=False;" & _
"User ID=sa;"& _
"password=*******;" & _
"Initial Catalog=DataBase;" & _
"data source=(local)"

cn.Open strCn

if strCallerId<>"" then
strPhoneNumber
=strCallerId
else
strPhoneNumber
=strDtmf
end
if

str
= "insert into cti_Record(SessionId,StartDate,StartTime,StopTime,TimeLen,PhoneNumber,CallType,RecordFile) values('"+strSessionId+"','"+strDate+"','"+strStartTime+"','"+strStopTime+"','"+strTimeLenS+"','"+strPhoneNumber+"','"+strCallType+"','"+strRecordFile+"')"

cn.Execute str

msgbox
"OK"


End function

4.C#调用此VB脚本,代码如下:

string strScript="";
System.IO.StreamReader myReader;
strScriptFile
="d:\\vbs_Record.vbs";
myReader=System.IO.File.OpenText(strScriptFile);

strScript
=myReader.ReadLine();
while(myReader.EndOfStream==false)
{
strScript
=strScript +@"
" + myReader.ReadLine();
}
clsScriptEngine scriptEngine
= new clsScriptEngine();
scriptEngine.Language
= (ScriptLanguage)Enum.Parse(typeof(ScriptLanguage), "VBScript");

string[] strParameter=this._strParameter.Split(',');

Object[] objParameter
=new Object[12];
objParameter.SetValue(strParameter[
0],0);
objParameter.SetValue(strParameter[
1],1);
objParameter.SetValue(strParameter[
2],2);
objParameter.SetValue(strParameter[
3],3);
objParameter.SetValue(strParameter[
4],4);
objParameter.SetValue(strParameter[
5],5);
objParameter.SetValue(strParameter[
6],6);
objParameter.SetValue(strParameter[
7],7);
objParameter.SetValue(strParameter[
8],8);
objParameter.SetValue(strParameter[
9],9);
objParameter.SetValue(strParameter[
10],10);
objParameter.SetValue(strParameter[
11],11);

scriptEngine.Reset();
scriptEngine.Run(
"vbs_Record", objParameter, strScript);
scriptEngine
= null;
posted @ 2011-02-15 14:10  海乐学习  阅读(9966)  评论(2编辑  收藏  举报