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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELP PLEASE!!! Have to use .ini file (REALLY - HELP!) 2

Status
Not open for further replies.
Jan 8, 2001
163
0
0
US
I'm on a fast approaching deadline and my boss just told me to put a few of the variables in my VB6 application into a .ini file so we can change them easily down the line. Sounds all well and good but I've never used .ini files. Does anyone know A) how to set an ini file up with say 5 string variables and B) how to call those variables from inside the VB code? I see a lot of information in some old threads but I'm a little overwhelmed by what it's saying. All I need to do is read from the ini file and then use the variables I've read in. Please help if you can. I could really use it.

Thanks A LOT in advance,
CrystalVisualBORacle
 
I believe the best way to handle reading and writing to INI files is to use functions in the kernal32 Windows API. INI files are set up with a sections and keys. Sections are strings delimited with "[" and "]". Keys are strings within each section and located to the left of an equal sign. Key or variable values are to the right of the equal sign.

Sample ini file....

[Section]
Key=KeyValue

[filename]
file=version.txt

[Toolbar]
Button0=&Debit/Credit,Debit / Credit,DBCRGUI,Debit / Credit Processing
Button1=&Liberty Cash,LCash / LCASHGUI / Liberty Cash Processing

[Commands]
DBCRBatchReset=DBCRLib.BatchReset
DBCRCloseOutStatus=DBCRLib.CloseOutStatus
DBCRCloseout=DBCRLib.DBCRCloseout

'Sample Code for reading ini file....
'put declare in bas (common) module
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

'sample call to read version.txt into strFileName
strFileName = ReadIni(App.Path & "\" & ConfigFile, "Filename", "File")

'wrapper function for calling GetPrivateProfileString func
Private Function ReadIni(ByVal strIniFile As String, ByVal strSection As String, ByVal strKey As String) As String
Dim strBuffer As String
Dim lngChars As Long

'Buffer the string with NULL to prevent GPF
strBuffer = String$(256, 0)
lngChars = GetPrivateProfileString(strSection, strKey, "", strBuffer, 255, strIniFile)
ReadIni = Left$(strBuffer, lngChars)
End Function

Hope this helps... Dan Appleman's Guide to the Win32 API is a must have for VB programmers. Lots of good stuff. Alot of programmers use the registry rather than ini. A little bit more complicated and I'm still a little antsy about fooling the registry. "jamesprog@mindspring.com"
 
Sounds like you dont really need a standard ini file, just a simple text (.txt) file that contains a few strings (which can be created or edited with wordpad or notepad.
You could just store the strings as separate lines in the file, read it at the beginning of the application, and initialize the appropriate variables with each line.
 
'Found this stuff somewhere

By: Dr. John A. Nyhart


'******************************************************************
Function ReadWriteINI(Mode As String, tmpSecname As String, tmpKeyname As String, Optional tmpKeyValue) As String

Dim tmpString As String

On Error GoTo ReadWriteINIError
'

'******************************************************************
' Mode = "WRITE" or "GET"'
'******************************************************************
'Here are the declare functions
'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
'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
' *** set the return value to OKReadWriteINI = "OK"
' *** test for good data to work with
If IsNull(Mode) Or Len(Mode) = 0 Then
ReadWriteINI = "ERROR MODE" ' Set the return value
Exit Function
End If

If IsNull(tmpSecname) Or Len(tmpSecname) = 0 Then
ReadWriteINI = "ERROR Secname" ' Set the return value
Exit Function
End If

If IsNull(tmpKeyname) Or Len(tmpKeyname) = 0 Then
ReadWriteINI = "ERROR Keyname" ' Set the return value
Exit Function
End If

' *** set the ini file name
filename = &quot;C:\Vbasic\Test\WinPlace.ini&quot; ' <<<<< put your file name here''

' ******* WRITE MODE *************************************
If UCase(Mode) = &quot;WRITE&quot; Then
If IsNull(tmpKeyValue) Or Len(tmpKeyValue) = 0 Then
ReadWriteINI = &quot;ERROR KeyValue&quot;
Exit Function
Else
secname = tmpSecname
keyname = tmpKeyname
keyvalue = tmpKeyValue
anInt = WritePrivateProfileString(secname, keyname, keyvalue,filename)
End If
End If ' *******************************************************

' ' ******* GET MODE *************************************
If UCase(Mode) = &quot;GET&quot; Then
secname = tmpSecname
keyname = tmpKeyname
defaultkey = &quot;Failed&quot;
keyvalue = String$(50, 32)
anInt = GetPrivateProfileString(secname, keyname, defaultkey, keyvalue, Len(keyvalue), filename)
If Left(keyvalue, 6) <> &quot;Failed&quot; Then ' *** got it
tmpString = keyvalue
tmpString = RTrim(tmpString)
tmpString = Left(tmpString, Len(tmpString) - 1)
End If
ReadWriteINI = tmpString
End If

Exit Function ' *******

ReadWriteINIError:
MsgBox Error
Stop

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top