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