Solved

Reading a .cfg file

Posted on 2001-06-28
9
350 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
z = x + y – 1 6 67
Need VBS version of Excel date conversion 4 64
Updates not working for MS Windows 7 12 152
VBS file using code from 2nd file (txt or vbs) 4 30
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

914 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

23 Experts available now in Live!

Get 1:1 Help Now