Results 1 to 4 of 4

Thread: INI files

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 1999
    Posts
    64

    Post

    Does anyone have any working code for getting data from an INI file? please help.

  2. #2
    Junior Member
    Join Date
    Nov 1999
    Location
    Sydney, NSW, Australia
    Posts
    16

    Post

    Hi,

    Here is some quick code I used in a simple project. Basically I load all the info from the INI file at the time the main form loads. These are then held in global variables so the data is available quickly at any time in the application.

    A couple of other things to note, I initialise all the variables with my default values, so that if the INI file does not exist or there are any missing values it will not cause a problem later on.

    CODE:
    Sub GetINI()
    Dim InBuf As String, Eq As Integer
    Dim Key As String, KeyValue As String
    DataFile = "Hotline.mdb"
    AttacheData = "Attache.mdb"
    Ho0 = 897796
    Ho1 = vbBlack
    Ho2 = vbBlue
    WeC = vbRed
    On Error GoTo NoINI
    Open "Hotline.ini" For Input As #1
    On Error GoTo 0
    Do Until EOF(1)
    Line Input #1, InBuf
    Eq = InStr(1, InBuf, "=")
    If Eq > 0 Then
    Key = Left$(InBuf, (Eq - 1))
    KeyValue = Mid$(InBuf, (Eq + 1), Len(InBuf))
    Select Case UCase$(Key)
    Case "DATABASE"
    DataFile = KeyValue
    Case "ATTACHEDATA"
    AttacheData = KeyValue
    Case "CURRENTCOLOR"
    Ho0 = Val(KeyValue)
    Case "COMPLETEDCOLOR"
    Ho1 = Val(KeyValue)
    Case "TASKCOLOR"
    Ho2 = Val(KeyValue)
    Case "WEEKENDCOLOR"
    WeC = Val(KeyValue)
    End Select
    End If
    Loop
    Close #1
    Exit Sub

    NoINI:
    End Sub

    This is very simple and very reliable.

    Alan

  3. #3
    Lively Member
    Join Date
    Dec 1999
    Posts
    106

    Post

    This is code that uses the Win32 API, if you change it a little it should work for you. Put the code below in a standard module in your project and change the name of the ini file and section it reads as required.

    Option Explicit

    Public 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


    ' Writes to the ini file XFer.ini in the application directory for a given key and value
    Public Sub gWriteIniFile(ByVal sKey As String, ByVal sValue As String)

    Dim sFileName As String ' Ini file path
    Dim nRet As Long ' Api result

    sFileName = App.Path & "\XFer.ini"

    nRet = WritePrivateProfileString("XFer", sKey, sValue, sFileName)
    End Sub


    ' Reads from the ini file XFer.ini in the application directory for a given key
    Public Function gsReadIniFile(ByVal sKey As String) As String

    Dim sBuffer As String ' String to pass to api call
    Dim nBufferSize As Integer ' Size of sBuffer
    Dim sFileName As String ' Ini file path
    Dim nValid As Integer ' No of bytes read

    On Error GoTo Err_gbReadIniFile

    sBuffer = Space$(255) ' Pad string to pass to api
    nBufferSize = Len(sBuffer) ' store length

    sFileName = App.Path & "\XFer.ini"

    ' Get value from INI file
    nValid = GetPrivateProfileString("XFer", sKey, "", sBuffer, nBufferSize, sFileName)

    ' Discard the trailing spaces and null character.
    gsReadIniFile = Left$(sBuffer, nValid)

    Exit Function

    Err_gbReadIniFile:
    gsReadIniFile = ""
    End Function

  4. #4
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    To get the data from INI file, first you have to know the structure of that file first. All INI files have the structure shown below:


    [Section Name]
    KeyName = Value


    So lets say you have an INI file with this structure and this file called C:\MyINI.ini:


    [Person]
    Name=John


    Now using GetPrivateProfileString API you can easilly get the value "John"
    Code:
    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
    
    
    '----Put this on any event you want
    Dim strBuffer As String
    Dim lRet As Long
    
    strBuffer = Space(125)
    lRet = GetPrivateProfileString("Person", "Name", "No Name", strBuffer, Len(strBuffer), "C:\MyINI.ini")
    If lRet Then strBuffer = Left(strBuffer, lRet)
    MsgBox "This person's name is " & strBuffer
    ------------------

    Serge

    Software Developer
    [email protected]
    [email protected]
    ICQ#: 51055819


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width