从 MSScriptControl 获取数据时出现问题

发布于 2024-08-03 17:42:50 字数 633 浏览 7 评论 0原文

我在 C# 控制台应用程序中从 vbscript 获取数据时遇到问题。 我只是写下面的代码:

        int[] i = new int[3] { 1, 2, 3 };
        string msg = "";
        object[] myParam = { msg , i};
        MSScriptControl.ScriptControlClass sc = new MSScriptControl.ScriptControlClass();            
        sc.Language = "VBScript";
        sc.AddCode("Sub Test(ByRef msg, ByRef aryI)" + Environment.NewLine + 
                    "  msg = \"234\"" + Environment.NewLine +                        
                    "End Sub");
        sc.Run("Test", ref myParam);

我想在调用 Run 方法后获取 msg 修改后的字符串,但它不再工作(没有任何变化)

你能帮我吗?

提前致谢

I have problem with get data from vbscript in C# console application.
I just write below code:

        int[] i = new int[3] { 1, 2, 3 };
        string msg = "";
        object[] myParam = { msg , i};
        MSScriptControl.ScriptControlClass sc = new MSScriptControl.ScriptControlClass();            
        sc.Language = "VBScript";
        sc.AddCode("Sub Test(ByRef msg, ByRef aryI)" + Environment.NewLine + 
                    "  msg = \"234\"" + Environment.NewLine +                        
                    "End Sub");
        sc.Run("Test", ref myParam);

I want to get msg modified string after call Run method, but it does not work anymore(not any change)

Could you please help to me ?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山人契 2024-08-10 17:42:50

您将必须使用 Eval 函数或类似的函数来将值返回给您。

int[] i = new int[3] { 1, 2, 3 };        
string msg = "";        
object[] myParam = { msg , i};        
MSScriptControl.ScriptControlClass sc 
   = new MSScriptControl.ScriptControlClass();
sc.Language = "VBScript";
sc.AddCode("Function Test(ByRef msg, ByRef aryI) as String" + 
Environment.NewLine +  "  msg = \"234\"" + 
Environment.NewLine +  "  Test = msg" + // this Test=msg is a return statement
Environment.NewLine + "End Function");

msg = (string)sc.Run("Test", ref myParam);
or 
msg = (string)sc.Eval("Test",ref myParam);

我不知道上面哪一个能正确工作,但会有类似的东西。

您将变量传递给脚本,仅在脚本实例中,该变量用作引用,但是当 C# 在 sc.Run 方法中传递变量时,它仅将其作为值传递,而不是引用。

您无法检索脚本中 ByRef 的值。

另一种方法是传递整个对象。

[ComVisible(true)]
public class VBScriptObjects{
      public string Property1 {get;set;}
      public string Property2 {get;set;}
}


VBScriptObjects obj = new VBScriptObjects();

sc.AddObject( "myObj", obj , false);
sc.Run("myObj.Property1 = 'Testing'");

obj.Property1 <-- this should have a new value now..

制作类ComVisible,可以让你通过vbscript访问和改变属性。

You will have to use Eval function or something similar that will return you the value back to you.

int[] i = new int[3] { 1, 2, 3 };        
string msg = "";        
object[] myParam = { msg , i};        
MSScriptControl.ScriptControlClass sc 
   = new MSScriptControl.ScriptControlClass();
sc.Language = "VBScript";
sc.AddCode("Function Test(ByRef msg, ByRef aryI) as String" + 
Environment.NewLine +  "  msg = \"234\"" + 
Environment.NewLine +  "  Test = msg" + // this Test=msg is a return statement
Environment.NewLine + "End Function");

msg = (string)sc.Run("Test", ref myParam);
or 
msg = (string)sc.Eval("Test",ref myParam);

I dont know which one of above will work correcly but there will be something similar.

You are passing a variable to Script, in instance of Script only, the variable is used as Reference, but when C# passes the variable in sc.Run method it passes it as only value, not reference.

There is no way you can retrive value back which is ByRef in script.

Alternative way is to pass entire object.

[ComVisible(true)]
public class VBScriptObjects{
      public string Property1 {get;set;}
      public string Property2 {get;set;}
}


VBScriptObjects obj = new VBScriptObjects();

sc.AddObject( "myObj", obj , false);
sc.Run("myObj.Property1 = 'Testing'");

obj.Property1 <-- this should have a new value now..

Making class ComVisible, can let you access and change the properties through vbscript.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文