C# 使用 MSScriptControl AddObject 运行 VBScript,字符串失败 [英] C# run VBScript with MSScriptControl AddObject with string failed

查看:36
本文介绍了C# 使用 MSScriptControl AddObject 运行 VBScript,字符串失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 C# 程序:

This is my C# program:

class Program
{
    static void Main(string[] args)
    {
        CallVbsFunction(1); //Work
        CallVbsFunction(1.2); //Work
        CallVbsFunction('a'); //Work
        CallVbsFunction("a"); //!!Exception see bellow
    }

    private static void CallVbsFunction(object p)
    {
        var sc = new MSScriptControl.ScriptControl();
        sc.Language = "VBScript";
        sc.AllowUI = true;

        try
        {         
            sc.AddCode(System.IO.File.ReadAllText("script.vbs"));
            sc.AddObject("myguid", p, false);
            var parameters = new object[] { "a" };
            sc.Run("test", ref parameters);
        }
        catch (Exception e)
        {
            Console.Out.WriteLine(e.ToString());
        }
    }
}

我的 VBScript 文件内容:

My VBScript file contents:

Function Test(a)
    MsgBox myguid
End Function

最后,当我将 AddObject() 与字符串对象一起使用时,这是我的例外:

And Finally this is my exception when I use AddObject() with string object:

System.Runtime.InteropServices.COMException (0x800A0005):无效过程调用或参数在MSScriptControl.IScriptControl.Run(String ProcedureName, Object[]&参数)在 Srcipting.Program.CallVbsFunction(Object p) 中程序.cs

System.Runtime.InteropServices.COMException (0x800A0005): Invalid procedure call or argument at MSScriptControl.IScriptControl.Run(String ProcedureName, Object[]& Parameters) at Srcipting.Program.CallVbsFunction(Object p) in Program.cs

推荐答案

您需要使用 ComVisible 的包装对象:

You need to use a wrapper object that is ComVisible:

[ComVisible(true)]
public class StringWrapper
{
    private string wrappedString;

    public StringWrapper(string value)
    {
        wrappedString = value;
    }

    public override string ToString()
    {
        return wrappedString;
    }
}

CallVbsFunction(new StringWrapper("a"));

问题是 .net String 对象在第一次看时看起来像 MSScriptControl 的本机 vb 字符串,但在第二次看时却不是.

The problem is that the .net String object looks like a native vb string to the MSScriptControl on the first look but not on the second look.

只有在直接注册字符串或注册返回字符串的函数时才需要使用这个包装器.注册具有字符串类型属性的对象时没有问题.您传递给 Run() 的参数也没有问题,因为这些参数会被 .net 运行时正确编组为原生 vb 字符串.

You only need to use this wrapper when you register a string directly or register a function that returns a string. There is no problem when registering an object that has properties of type string. There is also no problem for the parameters you pass to Run() because these will be correctly marshaled to native vb strings by the .net runtime.

因此,也许最好的选择是不向脚本提供单个字符串,而是提供一个封装了您希望它使用的所有不同值的对象.

So the maybe best option is to not provide individual strings to your script but an object that encapsulates all the different values you want it to use.

定义这个类

[ComVisible(true)]
public class HostOptions
{
    public string OptionA { get; set; }
    public string OptionB { get; set; }
}

然后构造对象并设置所有属性并注册到脚本控件

Then construct the object and set all the properties and register it with the script control

var hostOptions = new HostOptions();
hostOptions.OptionA = "AAA";
hostOptions.OptionB = "BBB";

sc.AddObject("HostOptions", hostOptions, false);

然后您可以像这样在脚本中使用它:

You can then use it in your script like this:

Function Test(a)
    MsgBox HostOptions.OptionA
    MsgBox HostOptions.OptionB
End Function

这篇关于C# 使用 MSScriptControl AddObject 运行 VBScript,字符串失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆