by Frank Joseph Mattia (1 Submission)
Category: Windows API Call/Explanation
Compatability: Visual Basic 3.0
Difficulty: Unknown Difficulty
Date Added: Wed 3rd February 2021
Rating: (39 Votes)
Access .ini files in the blink of an eye. Use one line of your input to quickly retrive .ini values. With the same one line of code write to your .ini file. If you have any improvements on this code, E-Mail me at "[email protected]".
Assumes
When you call this in your code, this is the syntax you will need to use.
Dim X As String
X = mfncGetFromIni(SectionHeader, VariableName, FileName)
Text1.Text = X
Code Returns
Returns the value of a string of an .ini file.
Side Effects
None.
API DeclarationsPublic Declare Function GetPrivateProfileString Lib "kernel32" 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
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Function mfncGetFromIni(strSectionHeader As String, strVariableName As String, strFileName As String) As String
'**********************************************************************************************
' DESCRIPTION:Reads from an *.INI file strFileName (full path & file name)
' RETURNS:The string stored in [strSectionHeader], line beginning
' strVariableName=
'**********************************************************************************************
' Initialise variable
Dim strReturn As String
' Blank the return string
strReturn = String(255, Chr(0))
'Get requested information, trimming the returned
' string
mfncGetFromIni = Left$(strReturn, GetPrivateProfileString(strSectionHeader, ByVal strVariableName, "", strReturn, Len(strReturn), strFileName))
End Function
Function mfncWriteIni(strSectionHeader As String, strVariableName As String, strValue As String, strFileName As String) As Integer
'*****************************************************************************************************
' DESCRIPTION:Writes to an *.INI file called strFileName (full path & file name)
' RETURNS:Integer indicating failure (0) or success (other) to write
'*****************************************************************************************************
mfncWriteIni = WritePrivateProfileString(strSectionHeader, strVariableName, strValue, strFileName)
End Function