Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read Write INI Files

Status
Not open for further replies.

ufobaby

MIS
Sep 25, 2001
238
US
Dear all,

This FAQ explains how to Read and Write an INI files. To give a backgruond for newbies; INI files are Information files and are used to store some information required frequently... The same is accessed by some API's

in a new class module write the following code

Code:
Option Explicit

' --------
'  Public
' --------
'
' Property for file to read
Public File As String

' ---------
'  Private
' ---------
'
' API to read/write ini's
#If Win32 Then
   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 Integer, ByVal lpFileName As String) As Integer
   Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal Appname As String, ByVal KeyName As Any, ByVal NewString As Any, ByVal Filename As String) As Integer
#Else
   Private Declare Function GetPrivateProfileString Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
   Private Declare Function WritePrivateProfileString Lib "Kernel" (ByVal Appname As String, ByVal KeyName As Any, ByVal NewString As Any, ByVal Filename As String) As Integer
#End If

Sub DeleteSection(ByVal Section As String)

Dim retval As Integer

   retval = WritePrivateProfileString(Section, 0&, "", File)

End Sub
Public Function SaveSetting(ByVal Section$, ByVal Key$, ByVal Value$)

Dim retval As Integer

   SaveSetting = WritePrivateProfileString(Section$, Key$, Value$, File)

End Function

Public Function GetSetting(ByVal Section As String, ByVal KeyName As String) As String

Dim retval As Integer
Dim t As String * 255

   ' Get the value
   retval = GetPrivateProfileString(Section, KeyName, "unknown value", t, Len(t), File)

   ' If there is one, return it
   If retval > 0 Then
      GetSetting = Left$(t, retval)
   Else
      GetSetting = "Unknown section or key"
   End If

End Function

Public Function GetSection(ByVal Section As String, KeyArray() As String) As Integer

Dim retval As Integer
' Allocate space for return value
Dim t As String * 2500
Dim lastpointer As Integer
Dim nullpointer As Integer
Dim ArrayCount As Integer
Dim keystring As String
   
   ReDim KeyArray(0)
   
   ' Get the value
   retval = GetPrivateProfileString(Section, 0&, "", t, Len(t), File)
   
   ' If there is one, return it
   If retval > 0 Then
      '
      ' Separate the keys and store them in the array
      nullpointer = InStr(t, Chr$(0))
      lastpointer = 1
      Do While (nullpointer <> 0 And nullpointer > lastpointer + 1)
         '
         ' Extract key string
         keystring = Mid$(t, lastpointer, nullpointer - lastpointer)
         '
         ' Now add to array
         ArrayCount = ArrayCount + 1
         ReDim Preserve KeyArray(ArrayCount)
         KeyArray(ArrayCount) = keystring
         '
         ' Find next null
         lastpointer = nullpointer + 1
         nullpointer = InStr(nullpointer + 1, t, Chr$(0))
      Loop
   End If
   '
   ' Return the number of array elements
   GetSection = ArrayCount
   
End Function

in a form use the following declarations
Code:
Dim objIni As New iniClass
to read from an ini file ; myini.ini which contains -
[first_ini]
idea=good
code=simple
[second_ini]
usage=frequent

U need to know the nomenclature also
&quot;[first_ini]&quot; is called a Section
&quot;idea&quot; is called a Key
&quot;good&quot; is called a Value

you can have 'n' number of these.....

- To read section key use
Code:
strSEc1 = objIni.GetSetting(&quot;frist_ini&quot;, &quot;idea&quot;)
- To Write to a section key use
Code:
objIni.SaveSetting &quot;frist_ini&quot;, &quot;idea&quot;, &quot;very_good&quot;

use the GetSection function to get all the possible Sections in a file.

Hope this helps .... lemme know

[cheers]
Niraj
[noevil]
 
I created a Class Module and pasted the info into it from above. When compiling I get &quot;variable not defined&quot; and points to &quot;objIni&quot;. Do I need to make any additional settings to use the module?

What I am trying to do is supply the runtime path and the data paths in my app to make it more portable.

Using win2000/vb6, my form the settings are:

Private Sub MasItmMnu_Click()
Dim Pid As Long
Dim buf As String
Dim buf_len As Long
strSEc1 = objIni.GetSetting(&quot;cobol&quot;, &quot;runpath&quot;)

' Start the program.
Pid = Shell(&quot;runcobol ivs800.cob&quot;, vbNormalFocus)
If Pid = 0 Then
MsgBox &quot;Error starting program&quot;
Exit Sub
End If

' Get the window handle.
child_hwnd = InstanceToWnd(Pid)

' Reparent the program so it lies inside
' the PictureBox.
old_parent = SetParent(child_hwnd, picChild.hwnd)

End Sub

My INI File is called aeros.ini (same as app):

[cobol]
runpath=c:\rmc
options=k x=windows.cfg
[link]
apl=e:\apl
ivo=E:\ivo
oeo=e:\oeo
 
yes,

Dim objIni as New <<class module name>>

[cheers]
Niraj
[noevil]
 
Uhhh, how is this code different from the two or three other INI file snippets in this forum's FAQ?

Chip H.
 
Are these API calls cover all windows platforms?

Please excuse me for being dumb, but I'm still learning about API calls and just would like to know that if I use this code to create an ini file for my app', will the calls be recognized even from Win98 up to WinXP?

Also, At the moment I'm storing my setting into a database file (eg: BackColor, ForeColor, FontColor, ButtonColor, etc). Would it be safe to say that an ini file would be a better choice than a database for storing such thing?

Andrew G.

 
Isn't this all a bit irrelevant?

As .INI files were declared as Obsolescent by MS in 1995, is there a GOOD reason why we don't all use Registry instead?


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Chiph,
haven't gone thur the other posts, can't comment i got a lot of mails asking the code and so i posted it here.

Andrew G
yes, this works with all the windows platforms. you can use this in an .INI file if the data ia not tooo critical , as any one can open the file and mess around.

Johnwm,
yes it could be irrelevant, but using the registry is a something which not every body will know. and for some little info of data why to use registry and keep messing with it. XML can be looked upon as an alterntive but then u have to carry the overhead of refrences. So i feel for a quick and easy solution the INI files till today remain a good option.....

[cheers]
Niraj
[noevil]
 
but if one is to mess about with INI files and the app' ceases to work correctly, then don't they deserve things go wrong? Anyone with a bit of common sence should know the old saying: &quot;IF IT AIN'T BROKE, DON'T FIX IT&quot;.

Andrew G.

 
Whereas I agree with the adage &quot;If it ain't broke, don't fix it&quot;, one must also understand that we're talking about being dependant on the Microsoft environment. That leads me to the statement:
Rational notions don't apply in irrational worlds.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
In the following line of code, where is 'strSEc1' declared and is it type string if I were to declare it?

strSEc1 = objIni.GetSetting(&quot;frist_ini&quot;, &quot;idea&quot;)

I assume that strSEc1 holds the value, correct?

Andrew G.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top