VB icon

.INI read/write routines

Email
Submitted on: 1/1/2015 9:44:00 AM
By: Newsgroup Posting (from psc cd)  
Level: Not Given
User Rating: By 7 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit)
Views: 3593
 
     .INI read/write routines mfncGetFromIni-- Reads from an *.INI file strFileName (full path & file name) mfncWriteIni--Writes to an *.INI file called strFileName (full path & file name) sitush@aol.com
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :.INI read/write routines
'**************************************
Declare Function GetPrivateProfileString Lib "Kernel" (ByVal
lpApplicationName As String, lpKeyName As Any, ByVal lpDefault As String,
ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName
As String) As Integer
Declare Function WritePrivateProfileString% Lib "Kernel" (ByVal
lpApplicationName$, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal
lpFileName$)
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: .INI read/write routines
' Description:.INI read/write routines
mfncGetFromIni-- Reads from an *.INI file strFileName (full path & file name)
mfncWriteIni--Writes to an *.INI file called strFileName (full path & file name)
sitush@aol.com
' By: Newsgroup Posting (from psc cd)
'
' Returns:mfncGetFromIni--The string stored in [strSectionHeader], line beginning strVariableName
mfncWriteIni--Integer indicating failure (0) or success (other) to write
'**************************************

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=
'*** NOTE: Requires declaration of API call
GetPrivateProfileString
'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 mfncParseString (strIn As String, intOffset As Integer,
strDelimiter As String) As String
'*** DESCRIPTION:Parses the passed string, returning the value
indicated
'***by the offset specified, eg: the string "Hello,
World",
'***offset 2 = "World".
'*** RETURNS:See description.
'*** NOTE: The offset starts at 1 and the delimiter is the
character
'***which separates the elements of the string.
'Trap any bad calls
If Len(strIn) = 0 Or intOffset = 0 Then
mfncParseString = ""
Exit Function
End If
'Declare local variables
Dim intStartPos As Integer
ReDim intDelimPos(10) As Integer
Dim intStrLen As Integer
Dim intNoOfDelims As Integer
Dim intCount As Integer
Dim strQuotationMarks As String
Dim intInsideQuotationMarks As Integer
strQuotationMarks = Chr(34) & Chr(147) & Chr(148)
intInsideQuotationMarks = False
For intCount = 1 To Len(strIn)
'If character is a double-quote then toggle the In Quotation flag
If InStr(strQuotationMarks, Mid$(strIn, intCount, 1)) <> 0 Then
intInsideQuotationMarks = (Not intInsideQuotationMarks)
End If
If (Not intInsideQuotationMarks) And (Mid$(strIn, intCount, 1) =
strDelimiter) Then
intNoOfDelims = intNoOfDelims + 1
'If array filled then enlarge it, keeping existing contents
If (intNoOfDelims Mod 10) = 0 Then
ReDim Preserve intDelimPos(intNoOfDelims + 10)
End If
intDelimPos(intNoOfDelims) = intCount
End If
Next intCount
'Handle request for value not present (over-run)
If intOffset > (intNoOfDelims + 1) Then
mfncParseString = ""
Exit Function
End If
'Handle boundaries of string
If intOffset = 1 Then
intStartPos = 1
End If
'Requesting last value - handle null
If intOffset = (intNoOfDelims + 1) Then
If Right$(strIn, 1) = strDelimiter Then
intStartPos = -1
intStrLen = -1
mfncParseString = ""
Exit Function
Else
intStrLen = Len(strIn) - intDelimPos(intOffset - 1)
End If
End If
'Set start and length variables if not handled by boundary check above
If intStartPos = 0 Then
intStartPos = intDelimPos(intOffset - 1) + 1
End If
If intStrLen = 0 Then
intStrLen = intDelimPos(intOffset) - intStartPos
End If
'Set the return string
mfncParseString = Mid$(strIn, intStartPos, intStrLen)
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
'*** NOTE: Requires declaration of API call
WritePrivateProfileString
'Call the API
mfncWriteIni = WritePrivateProfileString(strSectionHeader,
strVariableName, strValue, strFileName)
End Function


Other 14 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

8/15/2015 1:48:13 AMJoão Garcia

ok
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.