3

I have a good old fashioned win32 dll with functions of the form

void Foo1(int* value)
void Foo2(char* string)
void Foo3(MyType* data)
//ect...

I need to call this in QTP (vbscript) and retreive the data for use in the QTP application. Is this even possible in vbscript?

I have some controll over the DLL. It is written in c++. Building a COM server is not an option. Refactoring the code to include accessor methods with ordinal return types is flat out of the question (would be a maintainance and scabaility nightmare).

Editing to clarify the example...

I have...

void Add(int x, int y, int* result)

...I need to do the QTP equivalent of this...

int myX = 2;
int myY = 5;
int myResult = -1;
Add(myX, myY, &myResult);
//myResult should now be 7

...but in QTP.

Calling int Bar(int x, int y) in QTP is easy.
I need to know if its possible to call into void Foo(int* result)
in this way Foo(&myResult) and pass in a reference to result.

dFlat
  • 819
  • 7
  • 19
  • putting this down here... Last night there was another answer to my question but it is not there anymore. ?? Can you retract answers on SO? Is it still there and I am missing something obvious? – dFlat Jun 30 '11 at 15:12

2 Answers2

2

You can declare external functions like Win32 API but there are some limitations, I believe the function should have extern "C" linkage and not all types are supported. Here's an example of how to use the Win32 GetParent function from QTP, you may be able to extrapolate how to match your own function.

' Declare
Extern.Declare micHwnd, "GetParent", "user32.dll", "GetParent", micHwnd
' explanation:  retVal     name        sourceDll     name        parameter

' Usage
hwnd = Extern.GetParent(Browser(“xxx”).GetROProperty("hwnd"))

The reason the name appears twice is so you can rename it for your script (I don't remember which is the name to look for and which is the name that you will use).

Motti
  • 110,860
  • 49
  • 189
  • 262
  • please see my edits/clarification. I need to call Foo(int* x) and have it modify an x I declare in QTP. Is this possible? – dFlat Jun 30 '11 at 14:08
2

The answer is to pass by reference.

In the .h file declare the function:

extern "C" __declspec(dllexport) int HelloWorld(int &);

In the .cpp file define the function:

int HelloWorld(int& i)
{
    i = 10;
    return 55 + i;
}

QTP code:

Extern.Declare micInteger, "HelloWorld", "C:\test.dll", "HelloWorld", micInteger+micByRef 
myNumber = 5
Extern.HelloWorld(myNumber)
MsgBox(myNumber)
dFlat
  • 819
  • 7
  • 19