且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何调用函数库中的函数(带参数),从变量中获取函数名?

更新时间:2021-09-12 23:46:58

简约的工作示例:

Lib.vbs:

Option Explicit

Function Twice(n)
  Twice = n + n
End Function

Main.vbs:

Option Explicit

Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")

ExecuteGlobal goFS.OpenTextFile(".\lib.vbs").ReadAll()

Dim fpTwice : Set fpTwice = GetRef("Twice")

WScript.Echo fpTwice(42)

输出:

cscript main.vbs
84

错误消息...运行时错误:无效的过程调用或参数:'GetRef'"表明(导入)函数库是罪魁祸首.

The error message "... runtime error: Invalid procedure call or argument: 'GetRef'" indicates that the (importing of the) function library is to blame.

更新:

认为可以假设 VBScript 引擎保留一个表,将子/函数/方法名称与可调用代码相关联,以便能够进行文字/直接调用:

I think that it is plausible to assume that the VBScript engine keeps a table associating sub/function/method names with callable code to be able to do literal/direct calls:

n = Twice(11)

并且 GetRef("Twice") 访问这个表.因此,当文字调用成功时,我永远不会期望间接/函数指针"调用或 GetRef() 失败.

and that GetRef("Twice") accesses this table. So I would never expect an indirect/'function pointer' call or a GetRef() to fail when the literal invocation succeeds.

但根据 这个那个,至少有四种方法可以将库/模块导入"到 QTP,因为我不使用 QTP,我不能排除这些方法中的一些(甚至全部)可以一些愚蠢的事情会导致您描述的错误行为.

But according to this and that, there are at least four ways to 'import' libraries/modules into QTP, and as I don't use QTP I can't rule out, that some (or even all) of these methods do something silly to cause the mis-behaviour you describe.