VB icon

clsINI.cls

Email
Submitted on: 1/24/2015 4:03:00 AM
By: Bryan Johns (from psc cd)  
Level: Intermediate
User Rating: By 9 Users
Compatibility: VB 6.0
Views: 918
 
     Lately I've seen some posts for editing ini files that involve opening the ini file directly as a text file, looping line by line thru the file until locating the line desired and then altering that line. There is a much easier and more reliable way using the Windows API. This class module makes that easy. It also shows the proper way to handle errors that happen in a class module by raising custom error codes to be handled by the application that using using the class.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: clsINI.cls
' Description:Lately I've seen some posts for editing ini files that involve opening the ini file directly as a text file, looping line by line thru the file until locating the line desired and then altering that line. There is a much easier and more reliable way using the Windows API. This class module makes that easy. It also shows the proper way to handle errors that happen in a class module by raising custom error codes to be handled by the application that using using the class.
' By: Bryan Johns (from psc cd)
'
' Inputs:See the comments in the code.
'
' Returns:See the comments in the code.
'
' Assumes:It assumes a basic familiarity with how to use class modules.
'**************************************

Option Explicit
' API Declarations
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private 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
Private 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
' Local variables to hold property values.
Private mstrINIPath As String
Private mstrFileName As String
Private mstrWindowsPath As String
Public Property Get WindowsPath() As String
 WindowsPath = mstrWindowsPath
End Property
Private Property Let WindowsPath(ByVal strWindowsPath As String)
 mstrWindowsPath = strWindowsPath
End Property
'***************************
'* Procedure: WindowsPathGet
'* Copyright: (C) 2002, Bryan Johns
'* Purpose : Uses an API call to set the read only WindowsPath property.
'****************************
Private Sub WindowsPathGet()
 Dim Y As String
 On Error GoTo Error
 mstrWindowsPath = Space(255)
 Y = GetWindowsDirectory(mstrWindowsPath, 255)
 mstrWindowsPath = Left$(mstrWindowsPath, Y)
 Exit Sub
Error:
 Err.Raise 10001, "clsINI.cls", "Unable to read the windows path."
End Sub
Public Property Get FileName() As String
 FileName = mstrFileName
End Property
Public Property Let FileName(ByVal strFileName As String)
 mstrFileName = strFileName
End Property
'***************************
'* Procedure: WriteINI
'* Copyright: (C) 2002, Bryan Johns
'* Purpose : Exposes the private WriteTo sub.
'****************************
Public Sub WriteINI(Section As String, Field As String, Value As String)
 WriteTo Section, Field, Value
End Sub
'***************************
'* Function : ReadINI
'* Copyright: (C) 2002, Bryan Johns
'* Purpose : Exposes the Private ReadFrom function.
'***************************
Public Function ReadINI(Section As String, Field As String) As String
 ReadINI = ReadFrom(Section, Field)
End Function
Public Property Get INIPath() As String
 INIPath = mstrINIPath
End Property
Public Property Let INIPath(ByVal strINIPath As String)
 mstrINIPath = strINIPath
End Property
'***************************
'* Function : ReadFrom
'* Copyright: (C) 2002, Bryan Johns
'* Purpose : Returns values read from the INI file.
'***************************
Private Function ReadFrom(lstrSection As String, lstrField As String) As String
 Dim varReturnedString As Integer
 Dim lstrResults As String
 lstrResults = Space(255)
 varReturnedString = GetPrivateProfileString&(lstrSection, lstrField, "", lstrResults, 255, mstrINIPath & "\" & mstrFileName)
 lstrResults = Left$(lstrResults, varReturnedString)
 If Len(lstrResults) < 1 Then
Err.Raise 10000, "ReadFrom()", "Unable to read ini file entry."
Exit Function
 End If
 ReadFrom = lstrResults
End Function
'***************************
'* Procedure: WriteTo
'* Copyright: (C) 2002, Bryan Johns
'* Purpose : Writes values to the INI file.
'****************************
Private Sub WriteTo(lstrSection As String, lstrField As String, lstrDefaultValue As String)
 Dim X As Boolean
 X = WritePrivateProfileString&(lstrSection, lstrField, lstrDefaultValue, mstrINIPath & "\" & mstrFileName)
 If X = False Then
Err.Raise 10002, "WriteTo()", "There was a critical error writing to the" & mstrFileName & " file."
 End If
End Sub
Private Sub Class_Initialize()
 ' get the windows path and assign it to the INIPath property so that if the user of this
 ' class module doesn't supply a path it's defaulted to the windows path.
 WindowsPathGet
 mstrINIPath = mstrWindowsPath
End Sub


Other 1 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 Intermediate 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


 There are no comments on this submission.
 

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.