Monday, March 23, 2009

Using the Windows API in QTP

Hi All,
Here i am providing an overview of usage of Windows API in QTP.

Windows API :
Using the Windows API, you can extend testing abilities and add usability and flexibility to your tests and function libraries. The Windows operating system provides a large number of functions to help you control and manage Windows operations. You can use these functions to obtain additional functionality.
The Windows API is documented in the Microsoft MSDN Web site, which can be found at:
A reference to specific API functions can be found at:
To use Windows API functions:
1. In MSDN, locate the function you want to use in your test or function library.
2. Read its documentation and understand all required parameters and return value(s).
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 an external function. For more information, refer to the QuickTest Professional Object Model Reference.
The following example declares a call to a function called GetForegroundWindow, located in user32.dll:
Extern. Declare micHwnd, "GetForegroundWindow", "user32.dll", "GetForegroundWindow"
5. Call the declared function, passing any required arguments, for example, hwnd = Extern.GetForegroundWindow().
In this example, the foreground window's handle is retrieved. You can enhance your test or function library if the foreground window is not in the object repository or cannot be determined beforehand (for example, a window with a dynamic title). You may want to use this handle as part of a programmatic description of the window, for example:
In some cases, you may have to use predefined constant values as function arguments. Since these constants are not defined in the context of your test or function, you need to find their numerical value to pass them to the called function. The numerical values of these constants are usually declared in the function's header file. A reference to header files can also be found in each function's documentation under the Header section. If you have Microsoft Visual Studio installed on your computer, you can typically find header files under X:\Program Files\Microsoft Visual Studio\VC98\Include.

For example, the GetWindow API function expects to receive a numerical value that represents the relationship between the specified window and the window whose handle is to be retrieved. In the MSDN documentation, you can find the constants: GW_CHILD, GW_ENABLEDPOPUP, GW_HWNDFIRST, GW_HWNDLAST, GW_HWNDNEXT, GW_HWNDPREV and GW_HWNDPREV. If you open the WINUSER.H file, mentioned in the GetWindow documentation, you will find the following flag values:
* GetWindow() Constants
#define GW_HWNDFIRST 0
#define GW_HWNDLAST 1
#define GW_HWNDNEXT 2
#define GW_HWNDPREV 3
#define GW_OWNER 4
#define GW_CHILD 5
#define GW_MAX 6
The following example retrieves a specific menu item's value in the Notepad application.
' Constant Values:
Const MF_BYPOSITION = 1024

'API Functions Declarations
Extern.Declare micHwnd,"GetMenu","user32.dll","GetMenu", micHwnd
Extern.Declare micInteger,"GetMenuItemCount","user32.dll","GetMenuItemCount", micHwnd
Extern.Declare micHwnd,"GetSubMenu","user32.dll","GetSubMenu", micHwnd, micInteger
Extern.Declare micInteger,"GetMenuString","user32.dll","GetMenuString", micHwnd, micInteger, micString+micByRef, micInteger, micInteger
hwin = Window("Notepad").GetROProperty ("hwnd") ' Get Window's handle
MsgBox hwin
men_hwnd = Extern.GetMenu(hwin)' Get window's main menu's handle
MsgBox men_hwnd

‘Use API Functions
item_cnt = Extern.GetMenuItemCount(men_hwnd)
MsgBox item_cnt
hSubm = Extern.GetSubMenu(men_hwnd,0)
MsgBox hSubm
rc = Extern.GetMenuString(hSubm, 0,value, 64 ,MF_BYPOSITION)
MsgBox value


  1. Nice info, thank you, Sreekanth!

  2. very helpful...thanks!!!

  3. I was searching for this .....thanks to post this nice article