Results 1 to 5 of 5

Thread: VB6 and write ini file

  1. #1
    Raw Wave pierrotm777's Avatar
    Join Date
    May 2008
    Location
    Bordeaux, France
    Posts
    2,839

    Smile VB6 and write ini file

    I have an issue with an api class !
    I want to write a key into a ini file but each time a write a new key's value the old value is not delete and the new value is written before the old value !

    If somebody can help me .

    This is my ClsIniFile.cls class:
    Code:
    Option Explicit
    
    'Windows API calls
    
    'This writes an entry in the INI file
    'It takes as parameters the section name of the INI file,
    'the name of the entry, its value and the INI filename
    Private Declare Function WritePrivateProfileString _
        Lib "kernel32" Alias "WritePrivateProfileStringA" _
        (ByVal lpApplicationKey As String, _
        ByVal lpKeyName As Any, _
        ByVal lsString As Any, _
        ByVal lplFilename As String) As Long
    
    'This reads an entry in the INI file
    'It takes as parameters the section name of the INI file,
    'the name of the entry, its default value
    'the string returned, its length and the INI filename.
    Private Declare Function GetPrivateProfileString _
        Lib "kernel32" Alias "GetPrivateProfileStringA" _
        (ByVal lpApplicationKey As String, _
        ByVal lpKeyName As String, _
        ByVal lpDefault As String, _
        ByVal lpReturnedString As String, _
        ByVal nSize As Long, _
        ByVal lpFileName As String) As Long
    
    'Variable for the filename
    Private m_strFile As String
    
    'Variable for the section name
    Private m_AppKey As String
    
    Private Sub Class_Initialize()
    
        m_strFile = ""
        m_AppKey = ""
    
    End Sub
    
    Public Property Get FileName() As String
    
        FileName = m_strFile
    
    End Property
    
    Public Property Let FileName(ByVal strFile As String)
        
        m_strFile = Trim(strFile)
    
    End Property
    
    Public Property Get ApplicationKey() As String
        
        ApplicationKey = m_AppKey
    
    End Property
    
    Public Property Let ApplicationKey(ByVal strAppKey As String)
        
        m_AppKey = Trim(strAppKey)
    
    End Property
    
    Private Function Asc2String(str As String) As String
    'Remove all unnecessary characters from the returned string
    
    Dim i As Integer
    Dim strOP As String
    Dim intAsc As Integer
    
        str = Trim(str)
        strOP = ""
        For i = 1 To Len(str)
            intAsc = Asc(Mid(str, i, 1))
            If intAsc > 31 Then strOP = strOP & Chr(intAsc)
        Next
        Asc2String = strOP
    
    End Function
    
    Public Function GetValue(ByVal keyName As String, ByVal strDefault As String) As String
    'Returns the key value
    
    Dim strVal As String * 1000
    Dim ret As Long
    
        ret = GetPrivateProfileString(m_AppKey, keyName, strDefault, strVal, Len(strVal), m_strFile)
        If ret <> 0 Then
            GetValue = Asc2String(strVal)
        Else
            GetValue = ""
        End If
    
    End Function
    
    Public Function SetValue(ByVal keyName As String, ByVal strValue As String) As Boolean
    'Sets the key value, returns True if succeeds
    
    Dim ret As Long
    
        ret = WritePrivateProfileString(m_AppKey, keyName, strValue, m_strFile)
        If ret = 0 Then
            SetValue = False
            Exit Function
        End If
        SetValue = True
    
    End Function
    and my code is that:
    Code:
            Case "rrlanguage_select"
                iniFile.SetValue "language", RRSDK.GetInfo("=$LISTTEXT$")
                RRSDK.Execute ("SETVAR;RRLanguage_Info;The language is now " & RRSDK.GetInfo("=$LISTTEXT$") & "||WAIT;3||RELOADRR")
                ProcessCommand = 2
    Thanks for your help !!!

  2. #2
    Super Moderator
    Auto Apps:loading...
    JohnWPB's Avatar
    Join Date
    Sep 2005
    Location
    West Palm Beach, Florida
    Posts
    4,691
    Blog Entries
    1
    Blue could probably help with this one. However, if this is a scrip/plugin for RR, then RRExtended has ini reading and writing built into it.


    "VarToIniKey;<PATH TO INI FILE>;<INI SECTION>;<SECTION KEY>;<NAME OF VAR>"

    Would look something like this:

    "VarToIniKey;$PLUGINPATH$RRLanguage_Settings.ini;L anguage;CurrentLanguage;English"
    Front End of Choice: Ride Runner (Is there anything else??? ) & Powered by the DFX5.1 Skin Available in the Mobile App Mart

    My Fiero Build Thread

  3. #3
    Raw Wave pierrotm777's Avatar
    Join Date
    May 2008
    Location
    Bordeaux, France
    Posts
    2,839
    RRextended is a great RR plugin , but i try to learn vb6 and vb.net , so i should like to create my own plugin :-)

  4. #4
    Mod - Driveline forum
    Auto Apps:loading...
    ClockWorK's Avatar
    Join Date
    Oct 2003
    Location
    Michigan
    Posts
    703
    I don't do VB6 anymore, but I still have my old INI code for WritePrivateProfileString.

    I don't know if it matters (maybe I should probably do some testing), but all my VB6 code terminates the written strings with a null char at the end. For example:

    WritePrivateProfileString sect$ & Chr$(0), valName$ & Chr$(0), inp$ & Chr$(0), pth$ & Chr$(0)


    In fact, if it helps, here's my entire modINI.bas file for reading/writing INI, including deleting sections.

    Code:
    Option Explicit
    
    Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, 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 String, ByVal lpString As String, ByVal lpFileName As String) As Long
    Public Sub INI_DeleteValue(ByVal pth$, ByVal sect$, ByVal valName$)
    
    WritePrivateProfileString sect$ & Chr$(0), valName$ & Chr$(0), vbNullString, pth$ & Chr$(0)
    
    End Sub
    Public Sub INI_DeleteSection(ByVal pth$, ByVal sect$)
    
    WritePrivateProfileString sect$ & Chr$(0), vbNullString$, vbNullString, pth$ & Chr$(0)
    
    End Sub
    Public Function INI_GetValue(ByVal pth$, ByVal sect$, ByVal valName$) As String
    'this is for retrieving info from an INI file.
    
    Dim sData As String
    Dim rDataLen As Long
    Dim sDataLen As Long
    
    sDataLen = 4092
    sData$ = Space$(sDataLen)
    rDataLen = GetPrivateProfileString(sect$, valName$, "", sData$, sDataLen, pth$)
        
    ' strip off the NULL that API routines put on the end of strings
    INI_GetValue = Left$(sData$, rDataLen)
        
    End Function
    Public Sub INI_SetValue(ByVal pth$, ByVal sect$, ByVal valName$, ByVal inp$)
    
    'this stores data in an INI file, using simple commands.
    'example:
    'INI_SetValue "C:\windows\win.ini", "SECTION", "VARIABLE", "DATA"
    
    WritePrivateProfileString sect$ & Chr$(0), valName$ & Chr$(0), inp$ & Chr$(0), pth$ & Chr$(0)
        
    End Sub

  5. #5
    Raw Wave pierrotm777's Avatar
    Join Date
    May 2008
    Location
    Bordeaux, France
    Posts
    2,839
    Thanks ClockWork, i try this module as soon as possible :-)

Similar Threads

  1. File Based Write Filter (FBWF) for win 7 from embedded standard
    By feridoun in forum Software & Software Development
    Replies: 0
    Last Post: 09-21-2009, 04:36 PM
  2. Replies: 1
    Last Post: 06-22-2007, 12:44 PM
  3. Write your own GPS software
    By boxzer in forum Software & Software Development
    Replies: 0
    Last Post: 08-23-2006, 09:12 AM
  4. OSK to write in hebrew???
    By OFIRN in forum Map Monkey
    Replies: 0
    Last Post: 12-11-2005, 04:18 PM
  5. MPX Play best way to use file that have long file names ?
    By Vampyre in forum Software & Software Development
    Replies: 7
    Last Post: 09-16-2001, 02:39 PM

Bookmarks

Posting Permissions

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