ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Enhancing Your Tests and Function Libraries Using the Windows API

Enhancing Your Tests and Function Libraries Using the Windows API

原创 Linux操作系统 作者:ipqiaojj 时间:2009-03-14 11:03:04 0 删除 编辑

Enhancing Your Tests and Function Libraries Using the Windows API

Using the Windows API, you can extend testing abilities and add usability and flexibility to your 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: http://msdn2.microsoft.com/en-us/library/Aa383750

A reference to specific API functions can be found at: http://msdn2.microsoft.com/en-us/library/Aa383749

To use Windows API functions:

  1. In MSDN, locate the function you want to use in your function library.
  2. Read its documentation and understand all required parameters and return values.
  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, see the HP QuickTest Professional Object Model Reference.
  5. The following example declares a call to a function called GetForegroundWindow, located in user32.dll:

    extern.declare micHwnd, "GetForegroundWindow", "user32.dll", "GetForegroundWindow"

  6. Call the declared function, passing any required arguments, for example, hwnd = extern.GetForegroundWindow().
  7. In this example, the foreground window's handle is retrieved. You can enhance your 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:

    Window("HWND:="&hWnd).Close

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 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_ENABLEDPOPUP        6

#define GW_MAX        6

Example

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

' Notepad.exe

hwin = Window("Notepad").GetROProperty ("hwnd")' Get Window's handle

MsgBox hwin

' Use API Functions

men_hwnd = Extern.GetMenu(hwin)' Get window's main menu's handle

MsgBox men_hwnd

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 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/16792402/viewspace-567255/,如需转载,请注明出处,否则将追究法律责任。

上一篇: Running DOS Commands
请登录后发表评论 登录
全部评论

注册时间:2009-02-22

  • 博文量
    140
  • 访问量
    73245