|
-
Sep 9th, 2010, 05:57 AM
#1
Thread Starter
Member
Writing ini file
Hi, back again 
I am trying to use a module to write all my settings to a ini file. The example the module came with works, and created the ini file, however when I try use the module myself, it SAYS it worked, but I can't find the ini file anywhere, I'm pretty sure it's not even creating it.
The module:
Code:
'ReadWrite.Bas
'An original program by [email protected]
'Submitted for downloading Dec 6, 2000
'A module that creates, writes to, and retrieves data from a .INI file
'Can be used to store and retrieve info like passwords and user preferences
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 = "MySystem.ini" 'set this to the file you want created
strMySystemFile = App.Path & "\" & strFileName
Form1.Show
End Sub
Function WriteToFile(strFileSection As String, strKey As String, strValue As String) As Long
'parameters: strFileSection - string used as a file subheader for every section
' strKey - string used as key to write the value to file ( ex: UserName)
' strValue - the string value to write to file ( ex: Password)
'returns: -1 string to write is more than 255 chars
' 0 system write failure
' 1 write to file succesful
If Len(strKey) > READ_BUFF Or Len(strValue) > READ_BUFF Then
MsgBox "Can't write more than " & READ_BUFF & " characters for key or value."
WriteToFile = -1
Exit Function
End If
WriteToFile = WritePrivateProfileStringByKeyName(strFileSection, strKey, strValue, strMySystemFile)
End Function
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 > 1 Then
ReadFromFile = Left(strValue, lngRetLen)
Else
ReadFromFile = ""
End If
End Function
Function DeleteFromFile(strFileSection As String, strKey As String) As Long
'parameters: strFileSection - string used as file subheader for every section
' strKey - the string used in writing is also used for deleting
'returns: -1 key or section is a null string
' 0 for system delete failure
' 1 for successfule delete
If Len(strFileSection) = 0 Or Len(strKey) = 0 Then
MsgBox "Null string parameter not allowed for DeleteFromFile."
DeleteFromFile = -1
Exit Function
End If
DeleteFromFile = WritePrivateProfileStringToDeleteKey(strFileSection, strKey, 0, strMySystemFile)
End Function
My code:
Code:
Private Sub SaveVars_Click()
IntWriteRtn = WriteToFile("URLs", "URL1", "test")
If IntWriteRtn = 0 Then
Vars.Caption = "Failed to write to ini file!"
Else
Vars.Caption = "Ini file write OK!"
End If
End Sub
Example code:
Code:
'ready to write, call function to write
lngRetVal = WriteToFile(strSection, strUser, strPass)
If lngRetVal = 0 Then
lngRetVal = MsgBox("Problem in adding User " & strUser & "to File.", vbRetryCancel)
If lngRetVal = vbCancel Then
Text1 = "": Text2 = ""
Text1.SetFocus
Else
cmdAddToFile.SetFocus
End If
With the example, the mouse cursor shows the loading symbol briefly as it's saving it, but does not on mine, yet it returns a 1, meaning it worked OK  
Cheers,
Dan
-
Sep 9th, 2010, 06:06 AM
#2
Re: Writing ini file
Perhaps you didn't read these lines:
Code:
strFileName = "MySystem.ini" 'set this to the file you want created
strMySystemFile = App.Path & "\" & strFileName
-
Sep 9th, 2010, 06:11 AM
#3
Thread Starter
Member
Re: Writing ini file
I did, it doesn't matter what I change it to.
-
Sep 9th, 2010, 06:15 AM
#4
Re: Writing ini file
I assume your startup object for the project is Sub Main and your Form is Form1 ?
-
Sep 9th, 2010, 06:21 AM
#5
Thread Starter
Member
Re: Writing ini file
This is my whole project (very bloated, but I'm new to VB and it works, apart from the ini part )
Code:
Dim Hour As Integer
Dim Hour2 As Integer
Dim Hour3 As Integer
Dim Hour4 As Integer
Dim FileNum1 As Integer
Dim FileNum2 As String
Dim FileNum3 As Integer
Dim FileNum4 As String
Dim FileNum5 As Integer
Dim FileNum6 As String
Dim FileNum7 As Integer
Dim FileNum8 As String
Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
"URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, _
ByVal szFileName As String, ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long
Dim errcode As Long
Dim url As String
Dim localFileName As String
Dim errcode2 As Long
Dim URLG2 As String
Dim localFileName2 As String
Dim errcode3 As Long
Dim URLG3 As String
Dim localFileName3 As String
Dim errcode4 As Long
Dim URLG4 As String
Dim localFileName4 As String
Dim IntWriteRtn As Integer
Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _
Alias "DeleteUrlCacheEntryA" _
(ByVal lpszUrlName As String) As Long
Private Sub SaveVars_Click()
IntWriteRtn = WriteToFile("URLs", "URL1", url)
If IntWriteRtn = 0 Then
Vars.Caption = "Failed to write to ini file!"
Else
Vars.Caption = "Ini file write OK!"
End If
End Sub
Private Sub StartBtn1_Click()
StartBtn1.Enabled = False
URL1.Enabled = False
Dest1.Enabled = False
EXTBox1.Enabled = False
Tim1.Enabled = False
Timer1.Enabled = True
Call DownloadFile1
End Sub
Private Sub StartBtn2_Click()
StartBtn2.Enabled = False
URL2.Enabled = False
Dest2.Enabled = False
EXTBox2.Enabled = False
Tim2.Enabled = False
Timer2.Enabled = True
Call DownloadFile2
End Sub
Private Sub StopBtn1_Click()
Timer1.Enabled = False
StartBtn1.Enabled = True
URL1.Enabled = True
Dest1.Enabled = True
EXTBox1.Enabled = True
Tim1.Enabled = True
TR1.Caption = "-"
Hour = 0
FileNum1 = 0
End Sub
Private Sub StopBtn2_Click()
Timer2.Enabled = False
StartBtn2.Enabled = True
URL2.Enabled = True
Dest2.Enabled = True
EXTBox2.Enabled = True
Tim2.Enabled = True
Tr2.Caption = "-"
Hour2 = 0
FileNum3 = 0
End Sub
Private Sub Timer1_Timer()
Hour = Hour + 1
TR1.Caption = "Seconds until next image: " & Tim1 - Hour
If Hour = Tim1 Then
Hour = 0
Call DownloadFile1
End If
End Sub
Private Sub Timer2_Timer()
Hour2 = Hour2 + 1
Tr2.Caption = "Seconds until next image: " & Tim2 - Hour2
If Hour2 = Tim2 Then
Hour2 = 0
Call DownloadFile2
End If
End Sub
Sub DownloadFile1()
Dim EXT1 As String
EXT1 = EXTBox1.Text
EXT1 = Chr(46) & EXT1
Dim URLD1 As String
URLD1 = URL1.Text
Dim DestD1 As String
DestD1 = Dest1.Text
Dim CDT As String
Dim CDT1 As String
Dim CDT2 As String
CDT = Now
CDT1 = Replace(CDT, "/", "-")
CDT2 = Replace(CDT1, ":", ".")
url = URLD1
FileNum1 = FileNum1 + 1
FileNum2 = Str(FileNum1)
FileNum2 = Format(FileNum2, "000#")
localFileName = DestD1 & FileNum2 & " " & CDT2 & EXT1
Call DeleteUrlCacheEntry(url)
errcode = URLDownloadToFile(0, url, localFileName, BINDF_GETNEWESTVERSION, 0)
If errcode = 0 Then
Dld1.Caption = "Download Successful"
Lst1.Caption = "Last download: " & Now
Else
Dld1.Caption = "Error Downloading"
Lst1.Caption = "Failed download: " & Now
End If
End Sub
Sub DownloadFile2()
Dim EXT2 As String
EXT2 = EXTBox2.Text
EXT2 = Chr(46) & EXT2
Dim CDT3 As String
Dim CDT4 As String
Dim CDT5 As String
CDT3 = Now
CDT4 = Replace(CDT3, "/", "-")
CDT5 = Replace(CDT4, ":", ".")
URL2 = URL2.Text
FileNum3 = FileNum3 + 1
FileNum4 = Str(FileNum3)
FileNum4 = Format(FileNum4, "000#")
localFileName2 = Dest2.Text & FileNum4 & " " & CDT5 & EXT2
Call DeleteUrlCacheEntry(URL2)
errcode2 = URLDownloadToFile(0, URL2, localFileName2, BINDF_GETNEWESTVERSION, 0)
If errcode2 = 0 Then
Dld2.Caption = "Download Successful"
Lst2.Caption = "Last download: " & Now
Else
Dld2.Caption = "Error Downloading"
Lst2.Caption = "Failed download: " & Now
End If
End Sub
Private Sub StartBtn3_Click()
StartBtn3.Enabled = False
URL3.Enabled = False
Dest3.Enabled = False
EXTBox3.Enabled = False
Tim3.Enabled = False
Timer3.Enabled = True
Call DownloadFile3
End Sub
Private Sub StartBtn4_Click()
StartBtn4.Enabled = False
URL4.Enabled = False
Dest4.Enabled = False
EXTBox4.Enabled = False
Tim4.Enabled = False
Timer4.Enabled = True
Call DownloadFile4
End Sub
Private Sub StopBtn3_Click()
Timer3.Enabled = False
StartBtn3.Enabled = True
URL3.Enabled = True
Dest3.Enabled = True
EXTBox3.Enabled = True
Tim3.Enabled = True
TR3.Caption = "-"
Hour3 = 0
FileNum5 = 0
End Sub
Private Sub StopBtn4_Click()
Timer4.Enabled = False
StartBtn4.Enabled = True
URL4.Enabled = True
Dest4.Enabled = True
EXTBox4.Enabled = True
Tim4.Enabled = True
TR4.Caption = "-"
Hour4 = 0
FileNum7 = 0
End Sub
Private Sub Timer3_Timer()
Hour3 = Hour3 + 1
TR3.Caption = "Seconds until next image: " & Tim3 - Hour3
If Hour3 = Tim3 Then
Hour3 = 0
Call DownloadFile3
End If
End Sub
Private Sub Timer4_Timer()
Hour4 = Hour4 + 1
TR4.Caption = "Seconds until next image: " & Tim4 - Hour4
If Hour4 = Tim4 Then
Hour4 = 0
Call DownloadFile4
End If
End Sub
Sub DownloadFile3()
Dim EXT3 As String
EXT3 = EXTBox3.Text
EXT3 = Chr(46) & EXT3
Dim URLD3 As String
URLD3 = URL3.Text
Dim DestD3 As String
DestD3 = Dest3.Text
Dim CDT6 As String
Dim CDT7 As String
Dim CDT8 As String
CDT6 = Now
CDT7 = Replace(CDT6, "/", "-")
CDT8 = Replace(CDT7, ":", ".")
FileNum5 = FileNum5 + 1
FileNum6 = Str(FileNum6)
FileNum6 = Format(FileNum6, "000#")
localFileName3 = DestD3 & FileNum6 & " " & CDT8 & EXT3
Call DeleteUrlCacheEntry(URLD3)
errcode3 = URLDownloadToFile(0, URLD3, localFileName3, BINDF_GETNEWESTVERSION, 0)
If errcode3 = 0 Then
Dld3.Caption = "Download Successful"
Lst3.Caption = "Last download: " & Now
Else
Dld3.Caption = "Error Downloading"
Lst3.Caption = "Failed download: " & Now
End If
End Sub
Sub DownloadFile4()
Dim EXT4 As String
EXT4 = EXTBox4.Text
EXT4 = Chr(46) & EXT4
Dim CDT9 As String
Dim CDT10 As String
Dim CDT11 As String
CDT9 = Now
CDT10 = Replace(CDT9, "/", "-")
CDT11 = Replace(CDT10, ":", ".")
URLG4 = URL4.Text
FileNum7 = FileNum7 + 1
FileNum8 = Str(FileNum7)
FileNum8 = Format(FileNum8, "000#")
localFileName4 = Dest4.Text & FileNum8 & " " & CDT11 & EXT4
Call DeleteUrlCacheEntry(URLG4)
errcode4 = URLDownloadToFile(0, URLG4, localFileName4, BINDF_GETNEWESTVERSION, 0)
If errcode4 = 0 Then
Dld4.Caption = "Download Successful"
Lst4.Caption = "Last download: " & Now
Else
Dld4.Caption = "Error Downloading"
Lst4.Caption = "Failed download: " & Now
End If
End Sub
Cheers,
Dan
-
Sep 9th, 2010, 06:29 AM
#6
Re: Writing ini file
Dan,
By default, your Project Startup Object will be a Form. You need to change it to be Sub Main. To do this click on the 'Project' menu Item and select 'Project1 Properties'. A properties page will be displayed and you'll see a ComboBox named 'Startup Object', drop this down and select 'Sub Main'. Then click OK.
Wend your program runs it will execute Sub Main in your module which will set-up the .ini filename, it will then load Form1 of your Project.
-
Sep 9th, 2010, 06:48 AM
#7
Thread Starter
Member
Re: Writing ini file
Thanks Doogle, that fixed it 
Always the little stupid things that catch me out haha 
Cheers,
Dan
-
Sep 9th, 2010, 11:58 AM
#8
Re: Writing ini file
 Originally Posted by Things
Always the little stupid things that catch me out haha 
Dan
That's what makes learning so much fun !!
Good luck with the project
-
Sep 9th, 2010, 03:57 PM
#9
Re: Writing ini file
Well there is no need to start the app with Sub Main it should start with your Form if there is only one Form.
Sub Main()
Dim strFileName As String
strFileName = "MySystem.ini" 'set this to the file you want created
strMySystemFile = App.Path & "\" & strFileName
Form1.Show
End Sub
That Sub Main does nothing really only tells the app what the ini file name and location is the it shows Form1.
Now ini files can be edited by users or even deleted or renamed. What I always do is check if the ini file exists then ok, if it doesn't then create a default/basic settings.
You only need to use a Function for reading the ini file its not needed to write or delete items.
I use a Function to read data.
Code:
'Check if ini file exists.
Public Function IfFileExists(Fname As String) As Boolean
On Local Error Resume Next
Dim FF As Integer
FF = FreeFile()
Open Fname For Input As #FF
If Err Then
IfFileExists = False
Else
IfFileExists = True
End If
Close #FF
End Function
Public Function ReadIni(strSection As String, strKey As String, strDefault As String) As Variant
Dim IniFile As String
Dim Sp As String
Dim RetVal As Long
IniFile = App.Path & "\Your.ini"
Sp = String(255, 0)
RetVal = GetPrivateProfileString(strSection, strKey, "", Sp, Len(Sp), IniFile)
If RetVal = 0 Then
ReadIni = strDefault
Else
ReadIni = Left(Sp, InStr(Sp, Chr(0)) - 1)
End If
End Function
e.g. Use with
Private Sub Form_Load()
Me.Width = ReadIni("General", "Width", "8570")
End Sub
I use a Variant because ini files can hold any type of data, numbers, text etc.
Now because ini files can be edited its a good idea to send a default value just in case the file is missing.
Once you have read the data its a good idea to check if the data is valid depending on what the data is. If your checking for a number and you know the range expected then ok. If the number is outside that range then the ini file has been tampered with so give it a default value and write that new value back to the ini file.
Keith
I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|