# 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`

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.

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" under the [Desktop] section of WIN.INI
    Dim buffer As String * 255
    x = GetProfileString("Desktop", "Wallpaper", "(error)" buffer, 255)
    retstr = Left(buffer, x)  ' extract string
    If retstr = "(error)" Then
      Debug.Print "Wallpaper not found"
    Else
      Debug.Print "The wallpaper is " retstr
    End If

Related Call: GetPrivateProfileString, GetProfileInt, WriteProfileString
Category: INI Files
Back to the index.