从C#回调VBScript

时间:2021-08-05 15:50:51

I'd liked to know whether it is possible to call a function in VBScript from C#. Let me try to clarify. I'm using an application (Quick Test Professional or "QTP") that automates another application, mostly using VBScript. There is the ability, via an add in model, to extend the test functionality by writing add-ins to the testing application (QTP) that are .NET assemblies. The basic workflow is that the VBScript tests automate the test application, and can call methods on a class in the extensibility add in assembly to do more complicated things. This part works fine.

我想知道是否可以从C#调用VBScript中的函数。让我试着澄清一下。我正在使用一个应用程序(Quick Test Professional或“QTP”)来自动化另一个应用程序,主要使用VBScript。通过添加模型,可以通过将加载项写入.NET程序集的测试应用程序(QTP)来扩展测试功能。基本工作流程是VBScript测试自动化测试应用程序,并且可以调用扩展程序中的类上的方法添加程序集以执行更复杂的操作。这部分工作正常。

What I'd like to know is whether it is possible for my C# code (in the extensibility add in assembly) to call back to a function in the VBScript. I don't think the test application framework (QTP) explicitly supports this, so I'm wondering if there is any way to do this using standard interop techniques. I was half way thinking of using GetRef() to get a reference to the VBScript function of interest, passing this as a parameter to a method I call in the extensibility addin (I suspect I would run into marshaling issues even at this point?) and then within the C# code of my extensibility add in, somehow call a method on this object; this is where I'm completely lost (since I don't know how to do this without the necessary type information normally used in reflection).

我想知道的是我的C#代码(在扩展程序中添加程序集)是否可以回调到VBScript中的函数。我不认为测试应用程序框架(QTP)明确支持这一点,所以我想知道是否有任何方法可以使用标准的互操作技术。我有一半考虑使用GetRef()获取对感兴趣的VBScript函数的引用,将其作为参数传递给我在可扩展性插件中调用的方法(我怀疑即使在此时我也会遇到编组问题?)然后在我的可扩展性的C#代码中添加,以某种方式调用此对象的方法;这是我完全丢失的地方(因为我不知道如何在没有通常用于反射的必要类型信息的情况下这样做)。

I'm thinking this may not be possible, but would like confirmation if that's the case.

我认为这可能是不可能的,但如果是这样的话,我希望得到确认。

Thank you!

3 个解决方案

#1


In the end not so hard but finding it out was harder In vbscript set a=getref In c# declare the ref as an object https://community.saas.hpe.com/t5/Unified-Functional-Testing/C-compile-on-the-fly-thru-dotnetfactory/m-p/1611299#M22811

最后不是那么难但发现它更难在vbscript中设置a = getref在c#中将ref声明为对象https://community.saas.hpe.com/t5/Unified-Functional-Testing/C-compile-在即时-通dotnetfactory / MP / 1611299#M22811

private object _UFTCallBackFunction = null;

 public int callMeBack2()
 {
 string[] retParts = {"Yep this is value 1"};
 _UFTCallBackFunction.GetType().InvokeMember("", 
  System.Reflection.BindingFlags.InvokeMethod, null, 
_UFTCallBackFunction, retParts);
 return 0;
 }

 public void InitUFTCallBack(object UFTCallBackFunction)
 {
 _UFTCallBackFunction = UFTCallBackFunction;
 }

And then in vbs

然后在vbs中

Set oCallMe = GetRef("CallMeBackWithAParameter")
oTestCom.InitUFTCallBack(oCallMe)
oTestCom.callMeBack2()
Function callMeBackWithAParameter(P1)
 print "I wass called back from C# having value " & P1
End Function

#2


How does the VBScript call the C# code? I suspect that it is really calling on QTP, and QTP is calling the C# code. In that case, only QTP could possibly call the VBScript back.

VBScript如何调用C#代码?我怀疑它确实在调用QTP,而QTP正在调用C#代码。在这种情况下,只有QTP可能会调用VBScript。

Under what circumstances would your C# code call back? I doubt that VBScript can be called back asynchronously.

在什么情况下你的C#代码会回电?我怀疑可以异步调用VBScript。

#3


That's a toughie.

这是一个艰难的过程。

You MAY want to try writing an event handler in the VBScript side for the .NET component and raising an event on the .NET side when you want the function to be called.

您可能希望尝试在VBScript端为.NET组件编写事件处理程序,并在希望调用该函数时在.NET端引发事件。

Just be warned it may not even work, as it really depends on QTP's scripting engine. And even if it should, don't be surprised if it becomes an exercise in frustration.

请注意它甚至可能不起作用,因为它真的取决于QTP的脚本引擎。即使它应该,如果它变得沮丧,也不要感到惊讶。

See examples on WSH and event handling http://msdn.microsoft.com/en-us/library/ms974564.aspx Again, this probably won't apply to QTP, but it's to give you an idea of potential approach to the problem.

请参阅有关WSH和事件处理的示例http://msdn.microsoft.com/en-us/library/ms974564.aspx同样,这可能不适用于QTP,但它可以让您了解问题的潜在方法。

Edit: Additional link which may or may not help!

编辑:可能有帮助或可能没有帮助的其他链接!

http://www.west-wind.com/presentations/dotnetfromVfp/DotNetFromVfp_EventHandling.asp

#1


In the end not so hard but finding it out was harder In vbscript set a=getref In c# declare the ref as an object https://community.saas.hpe.com/t5/Unified-Functional-Testing/C-compile-on-the-fly-thru-dotnetfactory/m-p/1611299#M22811

最后不是那么难但发现它更难在vbscript中设置a = getref在c#中将ref声明为对象https://community.saas.hpe.com/t5/Unified-Functional-Testing/C-compile-在即时-通dotnetfactory / MP / 1611299#M22811

private object _UFTCallBackFunction = null;

 public int callMeBack2()
 {
 string[] retParts = {"Yep this is value 1"};
 _UFTCallBackFunction.GetType().InvokeMember("", 
  System.Reflection.BindingFlags.InvokeMethod, null, 
_UFTCallBackFunction, retParts);
 return 0;
 }

 public void InitUFTCallBack(object UFTCallBackFunction)
 {
 _UFTCallBackFunction = UFTCallBackFunction;
 }

And then in vbs

然后在vbs中

Set oCallMe = GetRef("CallMeBackWithAParameter")
oTestCom.InitUFTCallBack(oCallMe)
oTestCom.callMeBack2()
Function callMeBackWithAParameter(P1)
 print "I wass called back from C# having value " & P1
End Function

#2


How does the VBScript call the C# code? I suspect that it is really calling on QTP, and QTP is calling the C# code. In that case, only QTP could possibly call the VBScript back.

VBScript如何调用C#代码?我怀疑它确实在调用QTP,而QTP正在调用C#代码。在这种情况下,只有QTP可能会调用VBScript。

Under what circumstances would your C# code call back? I doubt that VBScript can be called back asynchronously.

在什么情况下你的C#代码会回电?我怀疑可以异步调用VBScript。

#3


That's a toughie.

这是一个艰难的过程。

You MAY want to try writing an event handler in the VBScript side for the .NET component and raising an event on the .NET side when you want the function to be called.

您可能希望尝试在VBScript端为.NET组件编写事件处理程序,并在希望调用该函数时在.NET端引发事件。

Just be warned it may not even work, as it really depends on QTP's scripting engine. And even if it should, don't be surprised if it becomes an exercise in frustration.

请注意它甚至可能不起作用,因为它真的取决于QTP的脚本引擎。即使它应该,如果它变得沮丧,也不要感到惊讶。

See examples on WSH and event handling http://msdn.microsoft.com/en-us/library/ms974564.aspx Again, this probably won't apply to QTP, but it's to give you an idea of potential approach to the problem.

请参阅有关WSH和事件处理的示例http://msdn.microsoft.com/en-us/library/ms974564.aspx同样,这可能不适用于QTP,但它可以让您了解问题的潜在方法。

Edit: Additional link which may or may not help!

编辑:可能有帮助或可能没有帮助的其他链接!

http://www.west-wind.com/presentations/dotnetfromVfp/DotNetFromVfp_EventHandling.asp