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

ini files 1

Status
Not open for further replies.

fergmj

Programmer
Feb 21, 2001
276
US
I am using a module I found on the web that reads from and writes to ini files. The problem is -- it reads and writes as a string.

My ini file looks like this:

[setup]
Time = 1

The function is like this:

Option Explicit

Declare Function GetPrivateProfileStringByKeyName& Lib "kernel32" Alias _
"GetPrivateProfileStringA" (ByVal lpApplicationName$, ByVal lpszKey$, ByVal lpszDefault$, ByVal lpszReturnBuffer$, ByVal cchReturnBuffer&, ByVal lpszFile$)
Declare Function GetPrivateProfileStringKeys& Lib "kernel32" Alias _
"GetPrivateProfileStringA" (ByVal lpApplicationName$, ByVal lpszKey&, ByVal lpszDefault$, ByVal lpszReturnBuffer$, ByVal cchReturnBuffer&, ByVal lpszFile$)
Declare Function GetPrivateProfileStringSections& Lib "kernel32" Alias _
"GetPrivateProfileStringA" (ByVal lpApplicationName&, ByVal lpszKey&, ByVal lpszDefault$, ByVal lpszReturnBuffer$, ByVal cchReturnBuffer&, ByVal lpszFile$)
Declare Function WritePrivateProfileStringByKeyName& Lib "kernel32" Alias _
"WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lplFileName As String)
Declare Function WritePrivateProfileStringToDeleteKey& Lib "kernel32" Alias _
"WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As Long, ByVal lplFileName As String)

Public strMySystemFile As String
Public strSection As String
Public Const BUFF_SIZ As Long = 9160
Public Const READ_BUFF As Long = 255
'**********
Public strLoginName As String
Public strPassword As String
Public strColor As String
Public lngColor As Long
Public lngRetVal As Long


Sub Main()
Dim strFileName As String
strFileName = "initest.INI" 'set this to the file you want created

strMySystemFile = App.Path & "\" & strFileName
Form1.Show
End Sub

Function ReadFromFile(strFileSection As String, strKey As String) As String
'parameters: strFileSection - string used as a file subheader for every section
' strKey - string used as key to read the value from file ( ex: UserName)
'returns: a string with the value tied to the key when written to file
' a null string "" if Key is not on file

Dim strValue As String
Dim lngRetLen As Long

strValue = String(READ_BUFF + 1, Space(1))
lngRetLen = GetPrivateProfileStringByKeyName(strFileSection, strKey, "", strValue, READ_BUFF, strMySystemFile)
If lngRetLen > 0 Then
ReadFromFile = Left(strValue, lngRetLen)
Else
ReadFromFile = ""
End If
End Function
-----------------------------

How can I get the "1" as a value for "Time"?

Thanks

fergmj
 
An ini file is just a collection of strings and you can only store values as strings.

However, when you read the value back into your VB program you can use the val() function to use it as a numerical value again.

Is this what you meant?

elziko
 
elziko

I should still get a string that has "1". But I get "".

Using Val() of "" gets "0"

I need a way for this to see the 1 as a string.

fergmj
 
Have you ran debug to check if your If statement is returning "1" instead of a "0"?
 
Yes and it really is returning a "0"

fergmj
 
I wrote a quick class called cIni that makes ini files nice and easy as this. It has limitations, but is a good general purpose class.

[tt]
dim oConfig as new cINI
dim MyValue as String
dim MyNumber as long

oConfig.Filename = "Myinifile.ini"

MyValue = oConfig.GetString("Section", "Keyname", "Default")
MyNumber = oConfig.GetNumber("Section", "KeyName", 0)

[/tt]

Create a new VB6 class and paste the following into it
'--snip--[tt]
' Encapsulate basic INI file Access

Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, 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 WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

Private sFile As String

Public Property Let Filename(ByVal vNewValue As Variant)
'
sFile = vNewValue

End Property

Public Function GetString(sSection As String, sKey As String, Optional sDefault As String = "None") As String
Dim ls0 As String
ls0 = Space(1024)


GetPrivateProfileString sSection, sKey, sDefault, ls0, 1024, sFile
If InStr(ls0, Chr(0)) Then
ls0 = Left$(ls0, InStr(ls0, Chr(0)) - 1)
End If

GetString = ls0

End Function

Public Function GetNumber(sSection As String, sKey As String, Optional lDefault As Long = 0) As Long
Dim ln0 As Long

ln0 = GetPrivateProfileInt(sSection, sKey, lDefault, sFile)

GetNumber = ln0


End Function

Public Function PutString(sSection As String, sKey As String, sValue As String)

WritePrivateProfileString sSection, sKey, sValue, sFile
End Function

Public Function PutNumber(sSection As String, sKey As String, lValue As Long)
Dim ls0 As String
ls0 = Str(lValue)
ls0 = Trim$(ls0)
WritePrivateProfileString sSection, sKey, ls0, sFile
End Function

[/tt]'-- end snip --

Wil Mead
wmead@optonline.net

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top