Solved

Reading a .cfg file

Posted on 2001-06-28
9
347 Views
Last Modified: 2012-06-21
Hi,

I would like to read a variable from a file Config.cfg which has a title in square brackets [MyValue] and then underneath it says MyValue = VALUE. How do I get VALUE using VB please?

Many thanks.
0
Comment
Question by:mustntgrumble
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 11

Expert Comment

by:Otana
ID: 6234414
try using the GetPrivateProfileString API Call.
0
 
LVL 11

Expert Comment

by:Otana
ID: 6234424
try using the GetPrivateProfileString API Call.
0
 
LVL 4

Accepted Solution

by:
WolfgangKoenig earned 0 total points
ID: 6234447
This message should understood as answer.
(Because of limitations in this community its only a comment.)
-----------------------------------------------------------
Here is the code:

Option Explicit

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

Private Declare Function WritePrivateProfileSection Lib _
        "kernel32" Alias "WritePrivateProfileSectionA" _
        (ByVal lpAppName As String, ByVal lpString As _
        String, ByVal lpFileName As String) As Long
       
Private Declare Function GetPrivateProfileSection Lib _
        "kernel32" Alias "GetPrivateProfileSectionA" _
        (ByVal lpAppName As String, ByVal lpReturnedString _
        As String, ByVal nSize As Long, ByVal lpFileName _
        As String) As Long
       
Dim File$, Field() As String

Private Sub Form_Load()
  Dim X%
    'Pfad der neuen ini-Datei
    File = App.Path & "\Test.ini"
    Frame1.Caption = File
   
    'Textfelder vordefinieren: Schreiben
    Text1.Text = "Section1"
    Text2.Text = "Key1"
    Text3.Text = "Hallo wie gehts?"
   
    'Textfelder vordefinieren: Lesen
    Text5.Text = "Section1"
    Text6.Text = "Key1"
    Text7.Text = ""
   
    Text8.Text = "Section2"
   
    'Feld zuweisen und ausgeben
    Text4.Text = "Section2"
    ReDim Field(0 To 5)
    Field(0) = "Bananen"
    Field(1) = "Gurken"
    Field(2) = "Erdbeeren"
    Field(3) = "Bohnen"
    Field(4) = "?pfel"
    Field(5) = "Kartoffeln"
   
    For X = 0 To UBound(Field)
      List1.AddItem Field(X)
    Next X
   
    List2.Clear
End Sub

Private Sub Command1_Click()
  'Wert "Key1" schreiben
  Call INISetValue(File, Text1.Text, Text2.Text, Text3.Text)
 
  'Feld schreiben
  Call INISetArray(File, Text4.Text, Field)
End Sub

Private Sub Command2_Click()
  Dim X%, xArray$()
    'Wert "Key1" lesen
    Text7.Text = INIGetValue(File, Text5.Text, Text6.Text)
   
    'Feld lesen
    ReDim xArray(0)
    Call INIGetArray(File, Text8.Text, xArray)
   
    'Feld ausgeben
    List2.Clear
    If UBound(xArray) > 0 Then
      For X = 0 To UBound(xArray) - 1
        List2.AddItem xArray(X)
      Next X
    End If
End Sub

Private Sub Command3_Click()
  Call INIDeleteKey(File, Text5.Text, Text6.Text)
  Call Command2_Click
End Sub

Private Sub Command4_Click()
  Call INIDeleteSection(File, Text5.Text)
  Call INIDeleteSection(File, Text8.Text)
  Call Command2_Click
End Sub

Private Sub INISetValue(ByVal Path$, ByVal Sect$, ByVal Key$, _
                        ByVal Value$)
  Dim Result&
    'Wert schreiben
    Result = WritePrivateProfileString(Sect, Key, Value, Path)
End Sub

Private Function INIGetValue(ByVal Path$, ByVal Sect$, ByVal Key$) _
                             As String
  Dim Result&, Buffer$
    'Wert lesen
    Buffer = Space$(32)
    Result = GetPrivateProfileString(Sect, Key, vbNullString, _
                                     Buffer, Len(Buffer), Path)
    INIGetValue = Left$(Buffer, Result)
End Function

Private Function INISetArray(ByVal Path$, ByVal Sect$, xArray() _
                             As String)
  Dim X%, Buffer$, Result&
    'Feld in einen String mit Trennzeichen Chr$(0) umwandeln
    For X = LBound(xArray) To UBound(xArray)
      Buffer = Buffer & xArray(X) & Chr$(0)
    Next X
   
    'String schreiben
    Buffer = Left$(Buffer, Len(Buffer) - 1)
    Result = WritePrivateProfileSection(Sect, Buffer, Path)
End Function

Private Sub INIGetArray(ByVal Path$, ByVal Sect$, xArray() As String)
  Dim Result&, Buffer$
  Dim l%, p%, z%
    'String lesen
    Buffer = Space(32767)
    Result = GetPrivateProfileSection(Sect, Buffer, Len(Buffer), Path)
   
    Buffer = Left$(Buffer, Result)
   
    If Buffer <> "" Then
      'String mit Trennzeichen Chr$(0) in ein Feld umwandeln
      l = 1
      ReDim xArray(0)
      Do While l < Result
        p = InStr(l, Buffer, Chr$(0))
        If p = 0 Then Exit Do
       
        xArray(z) = Mid$(Buffer, l, p - l)
        z = z + 1
        ReDim Preserve xArray(0 To z)
        l = p + 1
      Loop
    End If
End Sub

Private Sub INIDeleteKey(ByVal Path$, ByVal Sect$, ByVal Key$)
  Call WritePrivateProfileString(Sect, Key, 0&, Path)
End Sub
 
Private Sub INIDeleteSection(ByVal Path$, ByVal Sect$)
  Call WritePrivateProfileString(Sect, 0&, 0&, Path)
End Sub
0
 
LVL 4

Expert Comment

by:mcoop
ID: 6234470
Just to clarify...

Your config.cfg file seems to be formatted as a 'standard' INI file - so you can use the INI file API functions as described above.

When you call the functions, just tell it that your 'iniFile' is really a 'cfg file' !!!!

The Section and Item values relate to the ...

- -- -- -- -- -- -- -- -- -
[Section]
Item=aValue
myItem=myValue

[Another Section]
anotherItem=aValue

- -- -- -- -- -- -- -- -- -
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 22

Expert Comment

by:rspahitz
ID: 6235176
Just a warning--the GetPrivateProfileString routine won't seem to function on files greater than 64K.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7139932
Hi mustntgrumble,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will suggest to:

    Accept rspahitz's comment(s) as an answer.

mustntgrumble, if you think your question was not answered at all or if you need help, you can simply post a new comment here.  Community Support moderators will follow up.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7141101
I think you meant WolfgangKoenig.  Mine was a comment and not an answer.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7141232
Thanks for catching my error rspahitz.   Suggested disposition:

   Accept WolfgangKoenig's comment(s) as an answer.

DanRollins -- EE database cleanup volunteer
0
 
LVL 1

Expert Comment

by:Computer101
ID: 7173403
Comment from expert accepted as answer

Computer101
E-E Moderator
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 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

12 Experts available now in Live!

Get 1:1 Help Now