Results 1 to 10 of 10

Thread: [RESOLVED] Help with iniFile

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2020
    Posts
    35

    Resolved [RESOLVED] Help with iniFile

    Hi Guys

    How can I write value in IniFile this way

    Write("Section")("Key") = "Value"

    Thank you in advance..

  2. #2
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,393

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2020
    Posts
    35

    Re: Help with iniFile



    Hey jdc2000

    thank you for your reply

    But as you noticed I did put a special synthax where "Write" is an object

    I have the code for reading as I use 2 dictionaries ..I want to reverse that

    thanks

  4. #4
    Member Dragokas's Avatar
    Join Date
    Aug 2015
    Location
    Ukraine
    Posts
    740

    Re: Help with iniFile

    You can use my clsIniFile class

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2020
    Posts
    35

    Re: Help with iniFile

    Quote Originally Posted by Dragokas View Post
    You can use my clsIniFile class
    thank you
    yet this is not what I want

  6. #6
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: Help with iniFile

    Write a Wrapper around the writeprivateprofilestring-API
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  7. #7
    Fanatic Member
    Join Date
    Jul 2007
    Location
    Essex, UK.
    Posts
    578

    Re: Help with iniFile

    This is the closest I have.

    UAP & "MM.ini" is the name of the file I want to write to. You will have to do a 'little' work.

    Code:
    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
    Code:
    Private Function WriteIni(ByVal Section As String, ByVal Key As String, ByVal Value As String) As Boolean
        On Error Resume Next
        Dim X As Long, Buff As String * 255
        Buff = Value + Chr$(0)
        X = WritePrivateProfileString(Section, Key, Buff, UAP & "MM.ini")
        WriteIni = X
        On Error GoTo 0
    End Function

  8. #8
    Member Dragokas's Avatar
    Join Date
    Aug 2015
    Location
    Ukraine
    Posts
    740

    Re: Help with iniFile

    Quote Originally Posted by HKcom View Post
    thank you
    yet this is not what I want
    Than, I don't understand what you want.

    My class internally uses Dictionary of Dictionaries. This is exactly the one code, you have in first post.

    Just do mo_Sect variable public, e.g. try withing the class (in private LoadFile() method):

    Code:
    Debug.Print "value: " & mo_Sect("Section")("Param")
    mo_Sect("Section")("Param") = "New"
    Debug.Print "value: " & mo_Sect("Section")("Param")
    EDIT.
    Quote Originally Posted by HKcom View Post
    Hi Guys

    How can I write value in IniFile this way

    Write("Section")("Key") = "Value"
    Wait, so you would like to write a value to parameter (key) that isn't already exist?
    So, you need to mimic the dictionary object syntax... Let me remember how to do it.
    Last edited by Dragokas; Jan 19th, 2021 at 07:37 AM.
    Malware analyst, VirusNet developer, HiJackThis+ author || my CodeBank works

  9. #9
    Member Dragokas's Avatar
    Join Date
    Aug 2015
    Location
    Ukraine
    Posts
    740

    Re: Help with iniFile

    Ok, here it is:

    cIniInline.cls
    Code:
    
    Option Explicit
    
    Private m_Sect As Object
    
    Public Property Get Item(key As Variant) As cSection
        If Not m_Sect.Exists(key) Then m_Sect.Add key, New cSection
        Set Item = m_Sect(key)
    End Property
    
    Private Sub Class_Initialize()
        Set m_Sect = CreateObject("Scripting.Dictionary")
    End Sub
    
    
    cSection.cls
    Code:
    
    Option Explicit
    
    Private m_Param As Object
    
    Public Property Get Item(key As Variant) As Variant
        If m_Param.Exists(key) Then Item = m_Param(key)
    End Property
    
    Public Property Let Item(key As Variant, Value As Variant)
        If m_Param.Exists(key) Then
            m_Param(key) = Value
        Else
            m_Param.Add key, Value
        End If
    End Property
    
    Private Sub Class_Initialize()
        Set m_Param = CreateObject("Scripting.Dictionary")
    End Sub
    
    
    Sample form:
    Code:
    
    Option Explicit
    
    Private Sub Form_Load()
    
        Dim cIni As cIniInline
        Set cIni = New cIniInline
    
        Debug.Print "Value: " & cIni("section")("param")
    
        cIni("section")("param") = "data"
    
        Debug.Print "Value: " & cIni("section")("param")
    
    End Sub
    
    "Item" members should be marked as default (VB_UserMemId = 0) => Tools - Procedure attributes - Procedure Id - Default.

    I leave read/write ini file implementation as an exercise for yourself.
    Attached Files Attached Files
    Malware analyst, VirusNet developer, HiJackThis+ author || my CodeBank works

  10. #10

    Thread Starter
    Member
    Join Date
    Nov 2020
    Posts
    35

    Re: Help with iniFile

    Quote Originally Posted by Dragokas View Post
    Ok, here it is:

    cIniInline.cls
    Code:
    
    Option Explicit
    
    Private m_Sect As Object
    
    Public Property Get Item(key As Variant) As cSection
        If Not m_Sect.Exists(key) Then m_Sect.Add key, New cSection
        Set Item = m_Sect(key)
    End Property
    
    Private Sub Class_Initialize()
        Set m_Sect = CreateObject("Scripting.Dictionary")
    End Sub
    
    
    cSection.cls
    Code:
    
    Option Explicit
    
    Private m_Param As Object
    
    Public Property Get Item(key As Variant) As Variant
        If m_Param.Exists(key) Then Item = m_Param(key)
    End Property
    
    Public Property Let Item(key As Variant, Value As Variant)
        If m_Param.Exists(key) Then
            m_Param(key) = Value
        Else
            m_Param.Add key, Value
        End If
    End Property
    
    Private Sub Class_Initialize()
        Set m_Param = CreateObject("Scripting.Dictionary")
    End Sub
    
    
    Sample form:
    Code:
    
    Option Explicit
    
    Private Sub Form_Load()
    
        Dim cIni As cIniInline
        Set cIni = New cIniInline
    
        Debug.Print "Value: " & cIni("section")("param")
    
        cIni("section")("param") = "data"
    
        Debug.Print "Value: " & cIni("section")("param")
    
    End Sub
    
    "Item" members should be marked as default (VB_UserMemId = 0) => Tools - Procedure attributes - Procedure Id - Default.

    I leave read/write ini file implementation as an exercise for yourself.
    hi Dragokas

    I very much appreciate your efforts to help me and I think I will be satisfied with your example..
    thank you very much..

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