Highlighted
Micro Focus Expert
Micro Focus Expert
415 views

<(UFT) Support Tip> How to read/write values from/to an .ini file?

Hi there,

Recently I came across a situation where the requirement was to read/write values from an initialization (.ini) file. Even though saving settings in .ini files is an old practice, it is still used by many applications, so I’d like to share my findings on how to do it.

Initialization files have an structure like this:

[section1]
Key1=value1
Key2=value2

[section2]
Key3=value3
Key4=value4

You can find more information about initialization files here.

Using the above structure, let’s say I want to retrieve the value of Key3 under section2 and change the value of Key1 under section1 to “New value”. From UFT, we can use WritePrivateProfileString and GetPrivateProfileString functions from kernel32.dll to accomplish this.

Example on how to retrieve/read the value:

Extern.Declare micLong, "GetPrivateProfileString", "kernel32.dll", "GetPrivateProfileStringA", micString, micString, micString, micString + micByRef, micDWord, micString
extern.GetPrivateProfileString("section2", "Key3", "Not found", returnValue, 256, "C:\mySampleINI.ini")
msgbox returnValue

Example on how to write a value:

extern.Declare micLong, "WritePrivateProfileString", "kernel32.dll", "WritePrivateProfileString", micString, micString, micString, micString
extern.WritePrivateProfileString "section1", "Key2", "New value", "C:\mySampleINI.ini"

Cheers!
Alvaro

If you find this useful, mark Kudos.
Note: The Opinions expressed in my post are mine alone, and do not reflect the opinions of my employer. No warranties express or implied for any answered posted.

Labels (1)
0 Replies
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.