从 MSScriptControl 获取数据时出现问题
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将必须使用 Eval 函数或类似的函数来将值返回给您。
我不知道上面哪一个能正确工作,但会有类似的东西。
您将变量传递给脚本,仅在脚本实例中,该变量用作引用,但是当 C# 在 sc.Run 方法中传递变量时,它仅将其作为值传递,而不是引用。
您无法检索脚本中 ByRef 的值。
另一种方法是传递整个对象。
制作类ComVisible,可以让你通过vbscript访问和改变属性。
You will have to use Eval function or something similar that will return you the value back to you.
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.
Making class ComVisible, can let you access and change the properties through vbscript.