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?

Shruti
  • 1
  • 13
  • 55
  • 95
Saranya
  • 111
  • 2
  • 2
  • 6

2 Answers2

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.

Community
  • 1
  • 1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • 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. – Saranya May 24 '13 at 10:53
  • @Saranya - 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)? – Ekkehard.Horner May 24 '13 at 11:15
  • 1
    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? – Saranya Jun 05 '13 at 09:13
  • 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? – TheBlastOne Jun 05 '13 at 10:45
  • 2
    @Saranya - GetRef() works for Global/Public functions/subs *and* even for Local/Private ones. – Ekkehard.Horner Jun 05 '13 at 11:06
  • @TheBlastOne - 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. – Saranya Jun 11 '13 at 12:51
  • 2
    @Saranya - Instead of associating FLs via menu, use `ExecuteFile` and then `GetRef` will work. – Pankaj Jaju Jan 25 '17 at 13:35
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
Community
  • 1
  • 1
Pankaj Jaju
  • 5,371
  • 2
  • 25
  • 41