Read and Write easy your INI files with Blitz3D

Blitz3D Forums/Blitz3D Tutorials/Read and Write easy your INI files with Blitz3D

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

With external DLL calls capability, Blitz can achieve an high grade of programmability. In this tutorial i try to explain how to manage easily you ini files.

First you need to create in blitz 'userlibs' directory a decls file to import API routines. Save it as kernel32.decls

; Userlibs kernel32.decls 
.lib "kernel32.dll"
GetPrivateProfileString%(lpApplicationName$,lpKeyName$,lpDefault$,lpReturnedString*,nSize%,lpFileName$):"GetPrivateProfileStringA"
WritePrivateProfileString%(lpApplicationName$,lpKeyName$,lpString$,lpFileName$):"WritePrivateProfileStringA"


Done? Well. Now you must include in your source code two functions to write and retrieve values in ini file:

Function SetIniString%(FileINI$, Section$, Key$, Value$)
  ; FileINI = file (must include path, for current dir use .\) to write into (eg: ".\myinifile.ini")
  ; Section$ = section that will include your key and value (eg: "GeneralSetup")
  ; Key$ = Key's name for the value (eg: "BackColor")
  ; Value$ = Value to write in Key (eg: "ff00ff")
  ; If the function succedeed it return True otherwhise False

  ; result in myinifile.ini:
  ; [GeneralSetup]
  ; BackColor=ff00ff

  Ret%=WritePrivateProfileString(Section$, Key$, Value$, FileINI$)
  If Ret% <> 1 Then
    Return False
  Else
    Return True
  End If
  
End Function

Function GetIniString$(FileINI$,Section$,Key$,DefaultValue$)
  ; FileINI = file (must include path, for current dir use .\) to read (eg: "c:\windows\myinifile.ini")
  ; Section$ = section where search your key (eg: "GeneralSetup")
  ; Key$ = Key's name to search (eg: "BackColor")
  ; DefaultValue$ = Value to return if key or file or section is not found (eg: "ff00ff")
  ; Function return value retrieved from searched Key or the user's default value

  myString$=""
  myBank=CreateBank(255)
  LenString%=GetPrivateProfileString(Section$, Key$, DefaultValue$, myBank, 255, FileINI$)
  For x=1 To LenString%
    myString$=myString$+Chr(PeekByte(myBank,x-1))
  Next
  FreeBank myBank
  Return myString$
	
End Function


You can manage also integer or floating number, you dont need to convert it to string before writing or after read. That's all!