What is a DLL?

Dynamic Linked Library is MS implementation of shared library concept in Windows. To understand this term more clearly, DLL can be broken down into Dynamic Link(ed) + Library

  • Dynamic Link means that the subroutines of a library are loaded into an application program at runtime, rather than being linked in at compile time, and remain as separate files on disk.
  • Library is a collection of subroutines

How to know about the functions in a DLL?

It is assumed that if you intend to call a DLL, you should know the function to be called from inside and what that function does. If you are clueless about how to get the function names you can get download Microsoft Dependency Walker or a 3rd party utility called PE Explorer which can help you to find the functions.

How can the functions inside DLL be called from QTP?

This part is actually simple and a two step process…

  • Declare the method using Extern.Declare

Example
Extern.Declare micHwnd, “FindWindow”, “user32.dll”, “FindWindowA”, micString, micString
where:

  • micHwnd -the data type of value returned by method
  • FindWindow -the user supplied procedure name. You can set it to anything as long as it’s a valid syntax.
  • user32.dll -the DLL from where you wish to call the method
  • FindWindowA -The actual method name inside the DLL
  • Last two are the data types of the arguments that will be passed to the procedure
  • Call the method

Example:

  • Extern.FindWindow(“Notepad”, vbNullString)

To show the above process in action, here is an example to change the title of the Notepad window by calling the user32.dll

'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, "LearnQTP.com")

Simple copy-paste the code above in your QTP ‘Expert View’. Open a blank notepad window. Run this code. You will now see that the name has changed from Untitled-Notepad to LearnQTP.com

References:Wiki DLL