- tsh73 advises, "Run it, write it, read it, break it and try to write again."
print" * WritePrivateProfileStructA / GetPrivateProfileStructA demo *"'based on code by Richard Russel (80%) and by Stefan Pendl (15%)printdim info$(1,1)'for fileExistsstruct config, soundon$ as char[12], fullscreen aslong
SoundOn$ ="sound is on"
config.soundon$.struct= SoundOn$
config.fullscreen.struct=4print"Initial values: [soundon$ = ";config.soundon$.struct;"] [fullscreen = ";config.fullscreen.struct;"]"ifright$(DefaultDir$,1)<>"\"thenDefaultDir$=DefaultDir$+"\"endif
inifile$ =DefaultDir$+"testINI.ini"print inifile$
if fileExists("", inifile$)thenprint"File exists"elseprint"No such file"print[mnu]print"1. Write INI"print"2. Read INI"print"3. Delete INI"print"4. Mess with INI with Notepad (don't forget to save it)"print"0. Exit"input"Enter selection: ";sel
if sel =0thenendif sel <1or sel >4then[mnu]selectcase sel
case1
size =len(config.struct)calldll#kernel32,"WritePrivateProfileStructA", _
"settings"asptr, _ ' Section name"config"asptr, _ ' Key name
config asstruct, _ ' Structure
size asulong, _ ' Size of structure
inifile$ asptr, ret aslongif ret=0then'If the function successfully copies the string to the initialization file, the return value is nonzero.print"Some error happened"call displayError
elseprint"Write OK"endifcase2
size =len(config.struct)calldll#kernel32,"GetPrivateProfileStructA", _
"settings"asptr, _ ' Section name"config"asptr, _ ' Key name
config asstruct, _ ' Structure
size asulong, _ ' Size of structure
inifile$ asptr, ret aslongif ret=0then'If the function succeeds, the return value is nonzero.print"Some error happened"call displayError
elseprint"Read OK"' To retrieve the information from the struct:'
SoundOn$ = config.soundon$.struct
FullScreen = config.fullscreen.structprint"SoundOn$: ";SoundOn$
print"FullScreen: ";FullScreen
endifcase3call tryToKill inifile$
if fileExists("", inifile$)thenprint"File still exists"elseprint"No such file"case4run"notepad.exe "+inifile$
endselectgoto[mnu]endsub tryToKill filename$
'try to kill it'don't care much if failedonerrorgoto[handler]kill filename$
[handler]endsubsub displayError
' by Stefan Pendlcalldll#kernel32,"GetLastError", _
ErrorCode asulong
dwFlags = _FORMAT_MESSAGE_FROM_SYSTEM
nSize =1024
lpBuffer$ =space$(nSize); chr$(0)
dwMessageID = ErrorCode
calldll#kernel32,"FormatMessageA", _
dwFlags asulong, _
lpSource asulong, _
dwMessageID asulong, _
dwLanguageID asulong, _
lpBuffer$ asptr, _
nSize asulong, _
Arguments asulong, _
result asulongprint"Error "; ErrorCode; ": "; left$(lpBuffer$, result)endsubfunction fileExists(path$, filename$)files path$, filename$, info$()
fileExists =val(info$(0,0))'non zero is trueendfunction
WritePrivateProfileStructA Demo
-Demonstration Program
Accompanies the article on Initialization Files-