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
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
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.
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.
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..