开发者

Pass a pointer in a win32 dll from vbscript (qtp)

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. Refactor开发者_如何学运维ing 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.


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).


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜