Lutz,

its fairly easy but you have to use the windows api functions if you want
speed.  see the code below...

What i have done is wrap the api function inside a mapbasic function to do
some basic error reporting and reduce the size of the function call  (the
original API call is longer than the mapbasic call because of extra
parameters that i dont use..)  you can just strip these away and use teh api
originals if you wish.

I have a function to read data from ini file (GetInidata) and a function to
write data to the inifile (writeinidata)

Getinidata takes three variables ( the inifile path-name, the datasection
and the datakey)
Writeinidata takes these three variables and an additional one for teh data
value you want to place in the file

the inifile structure is 

[datasection1]
 datakey1= sometext
 datakey2= somevalue as text

[datasection2]
 datakey1= more text but in a new section
etc etc etc

you can read write to each datakey randomly


the main limitation i have found is that you cannot 'drop' entire
datasections (as claimed in visual basic), you have to 'empty' each
datarecord, so your ini file will have wasteful unused sections if you are
doing a lot of deletion transactions.

for more details look up teh getprivateprofile and writeprivateprofilestring
functions on teh web  for visual basic..



code follows>>>>>


 
Declare Function WritePrivateProfileString Lib "kernel32" Alias
"WritePrivateProfileStringA" 
(ByVal lpSectionName As String, ByVal lpKeyName As string, ByVal lpString As
string, ByVal lpIniFileName As String) As Integer

Declare Function GetPrivateProfileString Lib "kernel32" Alias
"GetPrivateProfileStringA" 
(ByVal lpSectionName As String, ByVal lpKeyName As String, ByVal lpDefault
As String, 
lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As
String) As Integer


Declare Function Getinidata(ByVal sIniName As String,ByVal SIniSection as
string,ByVal sIniRecord as string) As string

Declare Function Writeinidata(ByVal sIniName As String,ByVal SIniSection as
string,ByVal sIniRecord as string,ByVal sIniValue as string) As logical






Function Getinidata(ByVal sIniName As String,ByVal sIniSection as
string,ByVal sIniRecord as string) As string
dim x,y as integer
dim sReturnstring as string
x=800
                                sReturnString=string$(x," ") 
        
y=GetPrivateProfileString(sIniSection,sIniRecord,"-1",sReturnString,x,sInina
me)
                                if y = 0 or sreturnstring = "-1" then 'error
on read
                                        Print "NOTE ! No Information stored
in INI file for :" 
                                        +chr$(10)+Sininame      
                                        +chr$(10)+Sinisection   
                                        +" : "+ Sinirecord      
                                        sReturnString = ""
                                else 

                                print Sininame +" : "+Sinisection+" : "+
Sinirecord+ ".............ok"   
                                end if
                
                GetIniData = sReturnString
End Function


Function Writeinidata(ByVal sIniName As String,ByVal SIniSection as
string,ByVal sIniRecord as string,ByVal sIniValue as string) As logical
dim x as integer
                        x=WritePrivateProfileString(sIniSection,sIniRecord,
sInivalue, sIniName)
                                if X = 0 then 'error on write
                                        print "Unable to write data to INI
file :" 
                                        +chr$(10)+Sininame      
                                        +chr$(10)+Sinisection   
                                        +" : "+ Sinirecord
                                        +" : "+ "VALUE: "+ SiniValue    
                                        WriteIniData = FALSE
                                else
                                        print "data written to INI file :" 
                                        +Sininame       
                                        +Sinisection    
                                        +" : "+ Sinirecord
                                        +" : "+ "VALUE: "+ SiniValue    

                                        WriteIniData = TRUE     
                                end if

End Function


now in your subs  you can issue the commands:

dim x as logical
dim sUsername,inifile, mydata1, datakey1 as string
dim iUserage, mydata2 as integer

inifile = "c:\adir\afolder\myini.ini"
datakey1 = "Age"
mydata1 = "Russell Lawley"
mydata2 = 99

x = writeinidata(inifile, "MYDATASECTIONNAME", "Username", mydata1)
x = writeinidata(inifile, "MYDATASECTIONNAME", datakey1,
str$(mydata2))...'inifiles read/write strings

if x then 
        note "data written to file"
else
        note "data NOT written"
end if

sUsername  = getinidata(inifile,"MYDATASECTIONNAME","Username")
iUserage = val(getinidata(inifile,"MYDATASECTIONNAME",datakey1))   'note the
conversion to value !

print sUsername & " is an amazingly youthful " & iUserage    

code ends <<<<<<<<<<<<<<<

POINT TO NOTE  if you use actual datasection or datakey names   i.e.
"MYDATASECTIONNAME"  first note the use of " marks and secondly check your
spelling v carefully.  I tend to use variables a lot so it is easier. it
also means you can program in section names like DATA1, DATA", DATA�  and
not get confused... v handy for quickly handling lots of 'settings' for
creating workspaces and layouts..  oh  and i also use this for license
management..

regards

r













-----Original Message-----
From: Lutz Hogen [mailto:[EMAIL PROTECTED]
Sent: 23 March 2004 08:41
To: [EMAIL PROTECTED]
Subject: MI-L Question


Hi,

does anyone know, which is the best (fast) way, to read from und to write
into an (*.ini-)File.

I would like to put different variables into one file to manage different
problems, for example:
---------
sdoc=home/prod/;
inumber=127,123,17;
sname=Paul Hogen;
...
--------

Has anyone an answer to this problem. I would be very happy!
Any and all suggestions will be appreciated.

Yours sincerely

Lutz Hogen


[EMAIL PROTECTED] 
-Town of Aix la Chapelle-
Germany


*********************************************************************
This  e-mail  message,  and  any  files  transmitted  with  it, are
confidential  and intended  solely for the  use of the  addressee. If
this message was not addressed to  you, you have received it in error
and any  copying,  distribution  or  other use  of any part  of it is
strictly prohibited. Any views or opinions presented are solely those
of the sender and do not necessarily represent  those of the British
Geological  Survey. The  security of e-mail  communication  cannot be
guaranteed and the BGS accepts no liability  for claims arising as a
result of the use of this medium to  transmit messages from or to the
BGS. .                            http://www.bgs.ac.uk
*********************************************************************


---------------------------------------------------------------------
List hosting provided by Directions Magazine | www.directionsmag.com |
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
Message number: 11020

Reply via email to