Solved

how to create a ini file  wirh the version and unique information of my app

Posted on 2006-11-15
2
195 Views
Last Modified: 2011-09-20

I`m using an application of Indigo Rose call Visual Patch to create patches and update my programs but mainly because my exe don`t change a lot between each version it didn`t find any difference and don`t make the update so the indigo rose people advice me to create a unique file with the info of my project or put the information of the program in the windows registry changging each time the version of the software installed but I don`t know how to create a ini file, I have search a lot in the web with no good results please help me
0
Comment
Question by:pedro_arzac
  • 2
2 Comments
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 500 total points
ID: 17952404
Here is a class I use to manipulate INI files. Example of usage:

   Dim ConfigurationFile As clsINIFile
   Set ConfigurationFile = New clsINIFile
   ConfigurationFile.FilePath = "C:\Full\Path\To\ConfigFile.ini"

   ' Read the values from the configuration file
   ConfigurationFile.ReadDefault "Section Name", "EntryName", Variable, ""

   ' Write the values to the configuration file
   mConfigurationFile.WriteDefault "Section Name", "EntryName", Variable
   
   mConfigurationFile.FlushCache

The class module:

' INI files routines.

Option Explicit

Private Const kDefaultSection = "General"
Private Const kNoEntryTag = " [ N o  E n t r y ]"

Private Enum EntryFormat
   DefaultFormat = 0
   PercentFormat = 1
   TimeFormat = 2
End Enum

Public FilePath As String

Private mSection() As String
Private mFirstEntry() As Integer
Private mLastEntry() As Integer
Private mSectionListInitialized As Boolean
Private mCurrentSection As Integer

Private mEntry() As String
Private mEntryValue() As String
Private mNextEntry() As Integer
Private mEntryChanged() As Boolean
Private mEntryListInitialized As Boolean
Private mCurrentEntry As Integer
Private mEntryFound As Boolean

Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (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
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As Any, lpKeyName As Any, lpString As Any, ByVal lplFileName As String) As Integer

Private Sub Class_Terminate()

   FlushCache False

End Sub

Private Sub FindEntry( _
   ByVal SectionParm As String, _
   ByVal EntryParm As String, _
   ByVal AddEntry As Boolean)

' Finds section and entry names in lists, adding if missing and add requested.

   Dim Section As Integer
   Dim Entry As Integer

   ' Reset master pointers
   mCurrentSection = -1
   mCurrentEntry = -1
   mEntryFound = False
   
   ' Check if section already in list
   If mSectionListInitialized Then
      For Section = 0 To UBound(mSection)
         If SectionParm = mSection(Section) Then
            mCurrentSection = Section
            Exit For
         End If
      Next
   End If
   
   ' Add if section name not in list
   
   If mCurrentSection = -1 Then
   
      If AddEntry Then
   
         ' Add new section name to list
         If mSectionListInitialized Then
            Section = UBound(mSection) + 1
            ReDim Preserve mSection(Section)
            ReDim Preserve mFirstEntry(Section)
            ReDim Preserve mLastEntry(Section)
         Else
            Section = 0
            ReDim mSection(Section)
            ReDim mFirstEntry(Section)
            ReDim mLastEntry(Section)
            mSectionListInitialized = True
         End If
         mCurrentSection = Section
         mSection(mCurrentSection) = SectionParm
         mFirstEntry(mCurrentSection) = -1
         mLastEntry(mCurrentSection) = -1
         
      Else
         Exit Sub
      End If
     
   End If
   
   ' Check if entry already in list
   Entry = mFirstEntry(mCurrentSection)
   Do While Entry > -1
      If mEntry(Entry) = EntryParm Then
         mCurrentEntry = Entry
         mEntryFound = True
         Exit Do
      End If
      Entry = mNextEntry(Entry)
   Loop
   
   ' Add if entry name not in list
   
   If mCurrentEntry = -1 Then
   
      If AddEntry Then
   
         ' Add new entry name to list
         If mEntryListInitialized Then
            Entry = UBound(mEntry) + 1
            ReDim Preserve mEntry(Entry)
            ReDim Preserve mEntryValue(Entry)
            ReDim Preserve mEntryChanged(Entry)
            ReDim Preserve mNextEntry(Entry)
         Else
            Entry = 0
            ReDim mEntry(Entry)
            ReDim mEntryValue(Entry)
            ReDim mEntryChanged(Entry)
            ReDim mNextEntry(Entry)
            mEntryListInitialized = True
         End If
         mCurrentEntry = Entry
         mEntry(mCurrentEntry) = EntryParm
         mNextEntry(mCurrentEntry) = -1
         If mFirstEntry(mCurrentSection) > -1 Then
            mNextEntry(mLastEntry(mCurrentSection)) = mCurrentEntry
         Else
            mFirstEntry(mCurrentSection) = mCurrentEntry
         End If
         mLastEntry(mCurrentSection) = mCurrentEntry
         
         mEntryFound = True
         
      Else
         Exit Sub
      End If

   End If

End Sub

Public Sub FlushCache( _
      Optional ByVal CallDoEvents As Variant _
   )

' Write the cache to the INI file, calling DoEvents if requested.

   Dim Section As Integer
   Dim Entry As Integer
   Dim Result As Integer

   ' Exit if no cache
   If Not mSectionListInitialized Then Exit Sub
   
   If IsMissing(CallDoEvents) Then
      CallDoEvents = False
   End If
   
   ' Write each entry out to the INI file
   For Section = 0 To UBound(mSection)
      Entry = mFirstEntry(Section)
      Do While Entry > -1
         If CallDoEvents Then DoEvents
         If mEntryChanged(Entry) Then
            Result = WritePrivateProfileString(ByVal mSection(Section), ByVal mEntry(Entry), ByVal mEntryValue(Entry), ByVal FilePath)
            mEntryChanged(Entry) = False
         End If
         Entry = mNextEntry(Entry)
      Loop
   Next

   ' Tell Windows to flush its cache
   Result = WritePrivateProfileString(ByVal 0&, ByVal 0&, ByVal 0&, ByVal FilePath)

End Sub

Public Sub ReadDefault( _
   ByVal Section As String, _
   ByVal Entry As String, _
   ByRef EntryValue As Variant, _
   ByVal DefaultEntryValue As Variant)

' Read the entry value from the INI file.

   ReadFormat Section, Entry, EntryValue, DefaultEntryValue, DefaultFormat

End Sub

Private Sub ReadFormat( _
   ByVal Section As String, _
   ByVal Entry As String, _
   ByRef EntryValue As Variant, _
   ByVal DefaultEntryValue As Variant, _
   ByVal EntryFormat As EntryFormat)

' Read the entry value from the cache or, if not in the cache, from the INI
' file.
   
   Dim Buffer As String * 255
   Dim BufferLength As Integer
   Dim EntryValueWork As String

   ' Validate section name
   If Section = "" Then
      Section = kDefaultSection
   End If
   
   ' Read string
   FindEntry Section, Entry, False
   If mEntryFound Then
      EntryValueWork = mEntryValue(mCurrentEntry)
   Else
      BufferLength = GetPrivateProfileString(ByVal Section, ByVal Entry, ByVal kNoEntryTag, Buffer, Len(Buffer), ByVal FilePath)
      EntryValueWork = Left$(Buffer, BufferLength)
   End If

   ' Return default value if false INI entry found
   If EntryValueWork = kNoEntryTag Then
      EntryValue = DefaultEntryValue
      Exit Sub
   End If

   ' Save string in the cache
   FindEntry Section, Entry, True
   mEntryValue(mCurrentEntry) = EntryValueWork

   ' Initialize the return value

   Select Case VarType(EntryValue)
     
      Case vbEmpty
     
         Select Case VarType(DefaultEntryValue)
         
            Case vbBoolean
               EntryValue = False
     
            Case vbInteger
               EntryValue = CInt(0)
     
            Case vbLong
               EntryValue = CLng(0)
     
            Case vbSingle
               EntryValue = CSng(0)
     
            Case vbDouble
               EntryValue = CDbl(0)
     
            Case vbCurrency
               EntryValue = CCur(0)
     
            Case vbDate
               EntryValue = Now
     
            Case vbString
               EntryValue = ""
               
         End Select
     
      Case vbBoolean
         EntryValue = False

      Case vbInteger
         EntryValue = CInt(0)

      Case vbLong
         EntryValue = CLng(0)

      Case vbSingle
         EntryValue = CSng(0)

      Case vbDouble
         EntryValue = CDbl(0)

      Case vbCurrency
         EntryValue = CCur(0)

      Case vbDate
         EntryValue = 0

      Case vbString
         EntryValue = ""

   End Select
   
   ' Unformat the string based on the variable's type
   
   On Error Resume Next
   
   Select Case EntryFormat

      Case DefaultFormat
   
         Select Case VarType(EntryValue)
           
            Case vbBoolean
               EntryValue = BooleanValue(EntryValueWork)
     
            Case vbInteger
               EntryValue = CInt(EntryValueWork)
     
            Case vbLong
               EntryValue = CLng(EntryValueWork)
     
            Case vbSingle
               EntryValue = CDbl(EntryValueWork)
     
            Case vbDouble
               EntryValue = CDbl(EntryValueWork)
     
            Case vbCurrency
               EntryValue = CCur(EntryValueWork)
     
            Case vbDate
               EntryValue = CVDate(EntryValueWork)
     
            Case vbString
               EntryValue = EntryValueWork
     
         End Select

      Case PercentFormat

         Select Case VarType(EntryValue)
           
            Case vbInteger
               EntryValue = CInt(EntryValueWork)

            Case vbLong
               EntryValue = CLng(EntryValueWork)

            Case vbSingle
               EntryValue = CDbl(EntryValueWork)

            Case vbDouble
               EntryValue = CDbl(EntryValueWork)

         End Select

      Case TimeFormat

         Select Case VarType(EntryValue)
           
            Case vbDate
               EntryValue = CVDate(EntryValueWork)

            Case vbSingle
               EntryValue = CSng(CVDate(EntryValueWork))

         End Select
   
   End Select
   
End Sub

Public Sub ReadPercent( _
   ByVal Section As String, _
   ByVal Entry As String, _
   ByVal EntryValue As Variant, _
   ByVal DefaultEntryValue As Variant)

' Read the percent entry value from the INI file.

   ReadFormat Section, Entry, EntryValue, DefaultEntryValue, PercentFormat

End Sub

Public Sub ReadTime( _
   ByVal Section As String, _
   ByVal Entry As String, _
   ByVal EntryValue As Variant, _
   ByVal DefaultEntryValue As Variant)

' Read the time entry value from the INI file.

   ReadFormat Section, Entry, EntryValue, DefaultEntryValue, TimeFormat

End Sub

Public Sub SynchDefault( _
   ByVal Section As String, _
   ByVal Entry As String, _
   ByVal EntryValue As Variant, _
   ByVal DefaultEntryValue, _
   ByVal WriteEntry As Boolean)

' Read or write the entry value from or to the cache.

   If WriteEntry Then
      WriteFormat Section, Entry, EntryValue, DefaultFormat
   Else
      ReadFormat Section, Entry, EntryValue, DefaultEntryValue, DefaultFormat
   End If
   
End Sub

Public Sub SynchTime( _
   ByVal Section As String, _
   ByVal Entry As String, _
   ByVal EntryValue As Variant, _
   ByVal DefaultEntryValue, _
   ByVal WriteEntry As Boolean)

' Read or write the time entry value from or to the cache.

   If WriteEntry Then
      WriteFormat Section, Entry, EntryValue, TimeFormat
   Else
      ReadFormat Section, Entry, EntryValue, DefaultEntryValue, TimeFormat
   End If
   
End Sub

Public Function Value(ByVal Section As String, ByVal Entry As String, ByVal DefaultValue As Variant) As Variant

' Alternative function for the ReadDefault function.

   ReadDefault Section, Entry, Value, DefaultValue

End Function

Public Sub WriteDefault( _
   ByVal Section As String, _
   ByVal Entry As String, _
   ByVal NewEntryValue As Variant)

' Write the entry value to the cache.

   WriteFormat Section, Entry, NewEntryValue, DefaultFormat

End Sub

Private Sub WriteFormat( _
   ByVal Section As String, _
   ByVal Entry As String, _
   ByVal EntryValue As Variant, _
   ByVal EntryFormat As EntryFormat)

' Write the entry value to the cache.

   Dim EntryValueWork As String
   Dim CurrentEntryValue As Variant
   Dim EntryAlreadyExists As Boolean
   
   ' Validate the section name
   If Section = "" Then
      Section = kDefaultSection
   End If
   
   ' Format the string based on its type
   
   Select Case EntryFormat

      Case DefaultFormat
   
         Select Case VarType(EntryValue)
           
            Case vbBoolean
               EntryValueWork = BooleanText(EntryValue)
     
            Case vbInteger
               EntryValueWork = Format$(EntryValue, "General Number")
     
            Case vbLong
               EntryValueWork = Format$(EntryValue, "General Number")
     
            Case vbSingle
               If Not Abs(EntryValue) > 3402823! And Abs(EntryValue) - Int(Abs(EntryValue)) = 0 Then
                  EntryValueWork = Format$(EntryValue, "General Number")
               ElseIf Abs(EntryValue) < 3402823! And Not Abs(EntryValue) - Int(Abs(EntryValue)) < 0.0000001! Then
                  EntryValueWork = Format$(EntryValue, "0.0######")
               Else
                  EntryValueWork = Format$(EntryValue, "0.0#####E+00")
               End If
     
            Case vbDouble
               If Not Abs(EntryValue) > 179769313486231# And Abs(EntryValue) - Int(Abs(EntryValue)) = 0 Then
                  EntryValueWork = Format$(EntryValue, "General Number")
               ElseIf Abs(EntryValue) < 179769313486231# And Not Abs(EntryValue) - Int(Abs(EntryValue)) < 0.000000000000001 Then
                  EntryValueWork = Format$(EntryValue, "0.0#############")
               Else
                  EntryValueWork = Format$(EntryValue, "0.0#############E+00")
               End If
     
            Case vbCurrency
               EntryValueWork = Format$(EntryValue, "$#,##0.0###;($#,###0.0###)")
     
            Case vbDate
               EntryValueWork = Format$(EntryValue, "General Date")
     
            Case vbString
               EntryValueWork = EntryValue
     
         End Select

      Case PercentFormat

         Select Case VarType(EntryValue)
           
            Case vbInteger, vbLong, vbSingle, vbDouble
               EntryValueWork = Format$(EntryValue, "Percent")

            Case Else
               WriteFormat Section, Entry, EntryValue, DefaultFormat
               Exit Sub

         End Select

      Case TimeFormat

         Select Case VarType(EntryValue)
           
            Case vbDate, vbSingle, vbDouble
               EntryValueWork = Format$(EntryValue, "Medium Time")

            Case Else
               WriteFormat Section, Entry, EntryValue, DefaultFormat
               Exit Sub

         End Select
   
   End Select
   
   ' Write the entry to the cache, creating the entry if necessary
   ReadFormat Section, Entry, CurrentEntryValue, kNoEntryTag, EntryFormat
   If CurrentEntryValue = kNoEntryTag Then
      EntryAlreadyExists = False
   Else
      EntryAlreadyExists = True
   End If
   FindEntry Section, Entry, False
   If mCurrentEntry > -1 Or Not EntryValueWork = "" And Not EntryAlreadyExists Then
      FindEntry Section, Entry, True
      If Not mEntryValue(mCurrentEntry) = EntryValueWork Then
         mEntryValue(mCurrentEntry) = EntryValueWork
         mEntryChanged(mCurrentEntry) = True
      End If
   End If

End Sub

Public Sub WritePercent( _
   ByVal Section As String, _
   ByVal Entry As String, _
   ByVal EntryValue As Variant)

' Write the percent entry value to the cache.

   WriteFormat Section, Entry, EntryValue, PercentFormat

End Sub

Public Sub WriteTime( _
   ByVal Section As String, _
   ByVal Entry As String, _
   ByVal EntryValue As Variant)

' Write the time entry value to the cache.

   WriteFormat Section, Entry, EntryValue, TimeFormat

End Sub

Kevin
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 17959361
That's a lot of code there...why the "B" grade? Just curious...

Kevin
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now