GetProfileString Function

Declare Function GetProfileString Lib "kernel32.dll" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long

Platforms: Win 32s, Win 95/98, Win NT

GetProfileString reads an string value from the WIN.INI file. The parameters passed to the function specify which value will be read from. The function always returns the length in characters of the string put into the variable passed as lpReturnedString. If the function was successful, the string read from the INI file will be put into lpReturnedString. If not, it will instead receive the string given as lpDefault. This function is basically a watered-down version of GetPrivateProfileString because it, unlike this function, works with all INI files. Note that INI file support is only provided in Windows for backwards compatibility; using the registry to store information is preferred.

lpAppName
The header of the INI file section the value is in.
lpKeyName
The name of the value to read.
lpDefault
The value to return if a valid value cannot be read. Make it something that would definitely not be read, such as "(error)".
lpReturnedString
A fixed-length string that will receive either the string read from the file or lpDefault.
nSize
The length in characters of lpReturnedString.

Example:

' Read the value "Wallpaper" from under the [Desktop] section
' of WIN.INI.  If an error occurs, the function will return "(error)"
Dim wallpaper As String  ' receives string read from WIN.INI
Dim slength As Long  ' receives length of string read from WIN.INI

wallpaper = Space(255)  ' make room in the buffer to receive the string read from WIN.INI
' Read the string from WIN.INI
slength = GetProfileString("Desktop", "Wallpaper", "(error)", buffer, 255)
wallpaper = Left(wallpaper, slength)  ' extract the returned string from the buffer
If wallpaper = "(error)" Then
  Debug.Print "Could not read information from WIN.INI."
Else
  Debug.Print "Wallpaper file: "; wallpaper
End If

See Also: GetPrivateProfileString, GetProfileInt, WriteProfileString
Category: INI Files

Go back to the alphabetical Function listing.
Go back to the Reference section index.


Go back to the Windows API Guide home page.
E-mail: rogue953@st-louis.crosswinds.net
This page is at http://www.vbapi.com/ref/g/getprofilestring.html