Code archives/File Utilities/simple ini file commands (broken)

This code has been declared by its author to be Public Domain code.

Download source code

simple ini file commands (broken) by dan_upright2003
*** for some reason this stuff doesn't work reliably anymore - it overwrites strings at random and can cause serious grief to blitz (it stopped recognising two identical values as equal for me) but i'll leave the code and stuff in case anyone wants to try and debug it/use it despite the warnings

set up the .decls file and then use the functions to read/write values

parameters are:
ReadIni:
----
Default is the value that will be returned if the file can't be read for some reason
String is the string that will contain the value read
size is the maximum number of characters that can be read (i use 255)

WriteIni:
----
Value is the value you want to store

Both:
----
AppName is (bizarrely) the name of the ini file section the value belongs to
KeyName is the name of the value you want
FileName is the name of the ini file (must include full path)
kernel32.decls:
----
.lib "kernel32.dll"

ReadIni%(AppName$, KeyName$, Default$, String$, size%, FileName$):"GetPrivateProfileStringA"
WriteIni%(AppName$, KeyName$, Value$, FileName$):"WritePrivateProfileStringA"

Comments

gman2004
this works fine, you just need to size the string passed into read since GetPrivateProfileString will not resize it for you. a simple wrapper could look like:

Function read_ini$(section$,key$,dflt$,file$)
Local temp$=String(" ",1024)

ReadIni(section$,key$,dflt$,temp$,1024,file$)
Return Trim(temp$)
End Function


Extron2004
temp$ need to be a pointer to a buffer not a null terminated string.


Extron2004
Like this.

kernel32.decls:
----
.lib "kernel32.dll"

ReadIni%(AppName$, KeyName$, Default$, Buffer*, size%, FileName$):"GetPrivateProfileStringA"
WriteIni%(AppName$, KeyName$, Value$, FileName$):"WritePrivateProfileStringA"

; Various var
Section$="Blitz Basic 3D"
Key$="Start"
Value$="2264"
File$="c:\Blitz Basic 3D.ini"
Default_string$="Null"
; Write section/key/value
Write_ini$(Section$,Key$,Value$,File$)
; Read section/key
val$=Read_ini$(Section$,Key$,Default_string$,File$)
Text 0,0,val$
;
WaitKey
End
;
;*********************************************************
;
Function Write_ini$(Section$,Key$,Value$,File$) 
	WriteIni(Section$, Key$, Value$, File$) 
End Function
;
Function Read_ini$(Section$,Key$,Default_string$,File$) 
	bank=CreateBank(255)
	ReadIni(Section$,Key$,Default_string$,bank,BankSize(bank),File$) 
	If PeekByte(bank,0)=0 Then
		value$=Default_string$
		Return value$
	EndIf
	For char=0 To BankSize(bank)-1
		chartemp=PeekByte(bank,char)
		If chartemp=0 Then Exit
		value$=value$+Chr$(chartemp)
	Next
	Return value$
End Function



gman2004
interesting. thx for the heads up Extron. i guess my thinking that the userlibs functionality of Blitz was taking care of something for me somewhere down the line was incorrect. any idea why my routine has been working for me? puzzled now :( and thx for the code example :)

thx.


Extron2004
I don't know, your code don't work for me.

Just a minor correction "If PeekByte(bank,0)=0 Then" or "If Not PeekByte(bank,0) Then" after "ReadIni(Section$....".


gman2004
huh. i wonder if its a difference between BlitzPlus and Blitz3D which, given your example, looks like you are using? in any case thx. to be safe i will be switching. one more Q... how did you get the code to show up as code in your original post? thx :)


Extron2004
Blitz3D 1.87


how did you get the code to show up as code in your original post? thx


Use code, see the FAQ ;)


gman2004
lol thx for the pointer to the FAQ :) was looking all over for it in the community area. didnt see it there on the main page since i bookmarked the community area. guess i need to be a bit more thorough :( anyways, i dont have the full version of blitz3d, but the demo does not function with the code i posted and the full version of blitzplus v1.39 does. interesting they operate differently... in any case, the safer, more accepted and trustworthy method is the way im headed :) nice talking with ya extron :)


Code Archives Forum