Wednesday, May 13, 2009

How to call a function existing in a DLL file using QTP?

Generally we cannot directly open the DLL files in notepad or in some editors to check the functions in a DLL.
We can use Dependency Walker or PE Explorer to open the DLL files to check the functions.

Here is the method to call a function existing in a DLL file:
We can use Extern object which enables you to declare calls to external procedures from an external dynamic link library.
Once you use the Declare method for a method, you can use the Extern object to call the declared method.

Syntax:
Extern.Declare(RetType, MethodName, LibName, Alias [, ArgType(s)])

RetType - Data type of the value returned by the method.
MethodName - Any valid procedure name.
LibName - Name of the DLL or code resource that contains the declared procedure.
Alias - Name of the procedure in the DLL or code resource.
Note: DLL entry points are case sensitive.
Note: If Alias is an empty string, MethodName is used as the Alias.

Example:
The following example uses the Extern.Declare and Extern. methods to change the title of the Notepad window.
'Declare FindWindow method
Extern.Declare micHwnd, "FindWindow", "user32.dll", "FindWindowA", micString, micString
'Declare SetWindowText method
Extern.Declare micLong, "SetWindowText", "user32.dll", "SetWindowTextA", micHwnd, micString
'Get HWND of the Notepad window
hwnd = Extern.FindWindow("Notepad", vbNullString)
if hwnd = 0 then
MsgBox "Notepad window not found"
end if
'Change the title of the notepad window
res = Extern.SetWindowText(hwnd, "Uday")

To check the above code, open one blank notepad file.
Observe the title of the notepad window will change to Uday.

1 comment:

Yugesh said...

Hi,
If you have tried handling dlls that return string values, do let me know..
Mail to yugesh84@yahoo.com
Thanks,
Yugesh