Files with the extension .ini
are the legacy of the old Windows versions (16 bit). In the past, they were extensively used—and still are to some extent—ubiquitously to store the settings for applications. Nowadays, it is common practice to store settings in the registry. Though textual, such files have a very well-defined structure; there are sections and key-value pairs. A section starts with a label enclosed in square brackets: [section-name]
and a key-value is implemented as <variable name>=<value>
. Such a structure could be useful, for instance, if we wanted to keep the settings organized by environments or by user profiles within an.ini
file.
In this recipe, you will also see an example of how to use the Extern
reserved object to define references to methods in external DLLs, such as those of the Win32API. These methods can then be loaded and executed during runtime. A more elaborate description is available in the Drawing a rectangle on the screen with Win32 API methods (Extern) recipe of Chapter 8, Utility and Reserved Objects.
To complete this recipe, we need to use the global Extern
object, which with proper use provides the UFT with access to the methods of an external Dynamic Link Library (DLL). We will define a variable and assign it a reference to the global Extern
object (this is done to avoid persistence, as Extern
is a reserved object not released from memory until UFT closes):
Dim oExtern set oExtern = Extern
Then, we will declare the method or methods we wish to call from the relevant Win32API. In this case, the method is GetPrivateProfileString
, which retrieves the value of a given key within a specific section:
oExtern.Declare micInteger,"GetPrivateProfileString", "kernel32.dll","GetPrivateProfileStringA", _ micString, micString, micString, micString+micByRef, micInteger, micString
After defining the connection to the DLL with its returned value and arguments, we can retrieve the value of any key within a given section. In the following example, the ConfigFileVersion
key specified in the file wrls.ini
is located in the UFT/bin
folder. In the end, the Extern
object reference is destroyed at the end of the run:
call oExtern.GetPrivateProfileString("ProgramInformation", "ConfigFileVersion", "", RetVal, 255, "C:Program FilesHPUnified Functional Testinginwrls_ins.ini") print RetVal set oExtern = nothing