# GetPrivateProfileString Function

`Declare Function GetPrivateProfileString Lib "kernel32.dll" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long`

GetPrivateProfileString reads an string value from any 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.

lpApplicationName
  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.
lpFileName
  The filename of the INI file to read from.

Example:

    ' Read the value "scrnsave.exe" under the [boot] section of SYSTEM.INI
    ' These next few lines get the Windows directory....
    Dim buffer As String * 255
    x = GetWindowsDirectory(buffer, 255)
    sysini = Left(buffer, x) & "\system.ini"
    ' Now read the value
    x = GetPrivateProfileString("boot", "scrnsave.exe", "(error)" buffer, 255, sysini)
    retstr = Left(buffer, x)  ' extract string
    If retstr = "(error)" Then
      Debug.Print "Screen saver not found"
    Else
      Debug.Print "The screen saver is " retstr
    End If

Related Calls: GetPrivateProfileInt, GetProfileString, WritePrivateProfileString
Category: INI Files
Back to the index.