?
Solved

Reading a .cfg file

Posted on 2001-06-28
9
Medium Priority
?
365 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

864 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