QTP API’s, DLL’s and more. |
You can broaden testing abilities and include usability and flexibility to your tests and function libraries with the help of Windows API. A great number of functions are provided by Windows operating system to help you control and manage Windows operations. These functions can be used to achieve additional functionality.
To use Windows API functions:
1. In MSDN, locate the function you want to use in your test or function library.
2. Understand all the required parameters and return values of the function.
3. Note the location of the API function. API functions are located inside Windows DLLs. The name of the DLL in which the requested function is located is usually identical to the Import Library section in the function’s documentation. For example, if the documentation refers to User32.lib, the function is located in a DLL named User32.dll, typically located in your System32 library.
4. Use the QuickTest Extern object to declare (see more of this in examples) an external function.
5. Call the declared function, passing any required arguments.
DLL
A DLL (Dynamic Link Library) is a library containing code and data that can be used by more than one program at the same time. For Example
KERNEL32.DLL - Contains functions for file handles memory management, input/output operations, and interrupts.
COMDLG32.DLL - Controls the dialog boxes
DLLs save memory, encourage modularization of code, reduce swapping, save disk space, upgrade easier, provide a mechanism to extend the MFC library classes, support Multilanguage programs, and ease the creation of international versions.
Dynamic linking is a method that links applications to libraries at run time. DLLs may contain links to other DLLs. DLLs may have file extensions such as .EXE, .DRV, .DLL, .OCX etc
You can see DLL files on you system at C:\WINDOWS\system32 folder.
[Read and understand these examples thoroughly to get a better understanding]
Example 1 of QTP API |
Const MF_BYPOSITION = 1024
'API Functions Declarations
'The GetMenu function retrieves a handle to the menu assigned to the specified window. It takes one input parameter i.e. Handle to the window whose 'menu handle is to be retrieved. The return value is a handle to the menu. If the specified window has no menu, the return value is NULL.Extern.Declare micHwnd,"GetMenu","user32.dll","GetMenu",micHwnd
'The GetSubMenu function retrieves a handle to the drop-down menu or submenu activated by the specified menu item. It takes two input parameters, one 'is Handle to the menu, second is the int value which specifies the zero-based relative position in the specified menu of an item that activates a drop-down 'menu or submenu. If the function succeeds, the return value is a handle to the drop-down menu or submenu activated by the menu item.
Extern.Declare micHwnd,"GetSubMenu","user32.dll","GetSubMenu",micHwnd,micInteger
'The GetMenuString function copies the text string of the specified menu item into the specified buffer.
Extern.DeclaremicInteger,"GetMenuString","user32.dll","GetMenuString",micHwnd,micInteger,micString+
micByRef,micInteger, micInteger
'Get Windows handle (Win_Hand), pass it to get Menu handle (menu_hwnd), pass it to get submenu handle (submen_hwnd), pass the submenu handle to get the resultant string.
Win_Hand = Window("text:=Untitled - Notepad").GetROProperty ("hwnd")
menu_hwnd = Extern.GetMenu(Win_Hand)
submen_hwnd = Extern.GetSubMenu(menu_hwnd,0) rc=Extern.GetMenuString(submen_hwnd,0,value,20 ,MF_BYPOSITION)
MsgBox value
Example 2 of QTP API |
'Declare FindWindow method. The FindWindow function provides a way for advanced developers to get a handle to a window by specifying its window class and window name. You can then use that handle to send messages directly to the window.
Extern.Declare micHwnd, "FindWindow", "user32.dll", "FindWindowA", micString, micString
'Declare SetWindowText method. The SetWindowText function changes the text of the specified window's title bar (if it has one).
Extern.Declare micLong, "SetWindowText", "user32.dll", "SetWindowTextA", micHwnd, micString
'Get HWND of the Notepad window. Two parameters are being passed - name of class i.e. Notepad and Windows caption/title i.e. Untitled - Notepad. This program will also work if you put vbNullString.vbNullString is a special VB constant that denotes a null string.
hwnd = Extern.FindWindow("Notepad", "Untitled - Notepad")
if hwnd = 0 then
MsgBox "Notepad window not found"
end if
'Change the title of the notepad window. Two parameters are being passed, first is the Windows handle and second is the text string that is needed to set on the window.
result=Extern.SetWindowText(hwnd, "Testing")
'if the function succeeds then the return value is non-zero i.e. in our case result will contain a non zero value which can be used further.
Example 3 of QTP API |
'Declare FindWindow method . The FindWindow function provides a way for advanced developers to get a handle to a window by specifying its window class and window name. You can then use that handle to send messages directly to the window.
Extern.Declare micHwnd, "FindWindow", "user32.dll", "FindWindowA", micString, micString
'Declare SetWindowText method. The SetWindowText function changes the text of the specified window's title bar (if it has one).
Extern.Declare micLong, "SetWindowText", "user32.dll", "SetWindowTextA", micHwnd, micString
'The GetWindowText function copies the text of the specified window's title bar (if it has one) into a buffer.
Extern.Declare micLong, "GetWindowText", "user32.dll", "GetWindowTextA", micHwnd, micString+micByRef, micLong
'Get HWND of the Notepad window . Two parameters are being passed - name of class i.e. Notepad and Windows caption/title i.e. Untitled - Notepad. This program will also work if you put vbNullString. vbNullString is a special VB constant that denotes a null string.
hwnd = Extern.FindWindow("Notepad", "Untitled - Notepad")
if hwnd = 0 then
MsgBox "Notepad window not found"
end if
'Three parameters are being passed - window handle, Pointer to the buffer that will receive the text, Specifies the maximum number of characters to copy to the buffer, including the NULL character.
result1=Extern.GetWindowText(hwnd,str,256)
strplus=str+str
'Change the title of the notepad window. Two parameters are being passed, first is the Windows handle and second is the text string that is needed to set on the window.
result=Extern.SetWindowText(hwnd, strplus)
No comments:
Post a Comment