[Tutorial/Snippet][VB6] Reading and writing INI Files

Posts 1–7 of 7 · Page 1 of 1
[Tutorial/Snippet][VB6] Reading and writing INI Files
Purpose: Saving settings, etc.

Setting it up

  • Create a new module
  • Name it something along the lines of "SaveSets"
  • Add the following code to your module:


Code:
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationname As String, ByVal lpKeyName As Any, ByVal lsString As Any, ByVal lplFilename As String) As Long
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
Public Check As String

Public Function Load(Section As String, Key As String) As String 
Dim lngResult As Long 
Dim strFileName 
Dim strResult As String * 300
strFileName = App.Path & "\settings.ini" 
lngResult = GetPrivateProfileString(Section, Key, strFileName, strResult, Len(strResult), strFileName) 
Check = App.Path & "\sets.ini" 
Load = Trim(strResult) 
End Function

Public Function Save(Section As String, Key As String, Content As String)
Dim lngResult As Long
Dim strFileName
strFileName = App.Path & "\settings.ini"
lngResult = WritePrivateProfileString(Section, Key, Content, strFileName)
End Function
  • Now, change where it says "settings.ini" to file name of which you want to read and write.
  • All of the rest of the code will stay the same, you shouldn't have much of a reason to chance it.


Using it

Saving

To save something, write the following under the code for your button or whatever it is your saving from.

Code:
Call Save("Section", "Label", "Value")
Now start to fill this out. Right now that would output the following:

Code:
[Section]
Label = Value
Knowing that, replace section, label, and value with whatever you want to save.

Loading

To load something, put the following code for when you want to load:

Code:
Call Load("Section", "Label")
or, to assign the value it loaded to a variable,

Code:
MyVariable = Load("Section", "Label")
This works the same way as above and needs to have the same values for section and label or else it will NOT work.

If you need anymore help, just post saying so and I'll do my best to help.
just what i need for my app. thanx a bunch
Edit: Some Problems in the code? @_@'
Quote Originally Posted by FatCat00 View Post
just what i need for my app. thanx a bunch
Edit: Some Problems in the code? @_@'
Where do you see any problems in the code?
The words in red are the error i get i try to fix them but i think i made it worse?
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationname As String, ByVal lpKeyName As Any, ByVal lsString As Any, ByVal lplFilename As String) As Long
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
Public Check As String

Public Function Load(Section As String, Key As String) As String
Dim lngResult As Long
Dim strFileName
Dim strResult As String * 300
strFileName = App.Path & "\settings.ini"
lngResult = GetPrivateProfileString(Section, Key, strFileName, strResult, Len(strResult), strFileName)
Check = App.Path & "\sets.ini"
Load = Trim(strResult)
End Function

Public Function Save(Section As String, Key As String, Content As String)
Dim lngResult As Long
Dim strFileName
strFileName = App.Path & "\settings.ini"
lngResult = WritePrivateProfileString(Section, Key, Content, strFileName)
End Function
Quote Originally Posted by FatCat00 View Post
The words in red are the error i get i try to fix them but i think i made it worse?
What are you coding in? This was made strictly for VB6, I don't do any of the other VBs.
Quote Originally Posted by FatCat00 View Post
The words in red are the error i get i try to fix them but i think i made it worse?

Those errors are from vb.net in .net you cant do string * 300 to create a strign with 300 characters. try using strResult = space(300)


For app.path use Application.ExecutablePath.

For the length of strResult you can use len() or strResult.length.


P.S ; I Forgot to mention that long in VB is equal to an integer in vb.net. Also for you API declarations you need to marshal the variables that are declared as any.
Thanks for this usefull tut..
Posts 1–7 of 7 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

Need help?