How to call a function (with parameters) which is in function library, taking the function name from a variable?
Asked Answered
F

2

6

I'm trying to use getref to call a function from the function library associated with the test. My code -

In action1

str = "sample"
msg = "hi"
x = GetRef("Function_"&str)(msg)
msgbox x

In the function Library,

Function Function_sample(strMsg)
    Function_sample = strMsg
End Function

I'm getting the error -

"Invalid procedure call or argument."

But it works fine if the function is placed in the same action. How to call a function (with parameters) which is in function library, taking the function name from a variable?

Fretwell answered 22/5, 2013 at 12:8 Comment(3)
as your code 'works' as a plain VBScript, there must be a problem with your (including of the) library.Klarrisa
I don't think GetRef works in plain VBScript.Blithe
@AnsgarWiechers - of course it does, see my answer below.Klarrisa
K
5

Minimalistic working example:

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)

Output:

cscript main.vbs
84

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

Update:

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)

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.

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.

Klarrisa answered 23/5, 2013 at 13:18 Comment(7)
I don't think there is a problem with my (importing of the) function library as it works fine if I call the function directly (by the function name) without using getref.Fretwell
@Fretwell - verify that the error occurs on the GetRef() line; isolate the GetRef() call; publish more of your code (how do you 'import' a library in QTP?); could it be a problem of sequence/order (like calling before the library is loaded)?Klarrisa
I have associated the function library with my test and I'm able to call other functions from the action. Also, when I tried calling the function using getref within the function library, it worked fine. But I get the error when I call the function using getref from the action. Does getref work only for local functions?Fretwell
In my experience (for which I do not have an explaination), it should work, but fails if the GetRef call is in a library function, AND that function is registered as a test object method via RegisterUserFunc, AND the function called via the test object method. Is that your scenario?Hydrastine
@Fretwell - GetRef() works for Global/Public functions/subs and even for Local/Private ones.Klarrisa
@Hydrastine - I have not registered the function using RegisterUserFunc. I have associated the function library with my test by using ‘File > Settings > Resources > Associate Function Library’ option in QTP.Fretwell
@Fretwell - Instead of associating FLs via menu, use ExecuteFile and then GetRef will work.Physiology
P
2

I faced the same issue and found out that associating or loading the functional libraries doesn't work for GetRef. To work around this issue, all you need to do is instead of associating or loading your FL, execute it using ExecuteFile function.

In action1

ExecuteFile "/path/functionallibrary.vbs"
str = "sample"
msg = "hi"
x = GetRef("Function_" & str)(msg)
msgbox x
Physiology answered 25/1, 2017 at 13:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.