Board index » delphi » TIniFile.Create causes GPF - Help a Newbie?

TIniFile.Create causes GPF - Help a Newbie?

Quote
Ian Packer (ipac...@bloggs.win-uk.net) wrote:

:            IniFile.Create('contact.ini');

How about?

        IniFile := TIniFile.Create('contact.ini')

I believe that is the correct syntax and works just perfectly here in
MANY instances.

Mike.

--
==========================================================================
     Mike Frisch                    Internet: mfri...@saturn.tlug.org
     Northstar Technologies                    Compuserve: 76620,2534
     Newmarket, Ontario, CANADA       WWW: http://www.io.org/~mfrisch

 

Re:TIniFile.Create causes GPF - Help a Newbie?


Hello,

I'm trying to use the TIniFile object and its causing a GPF when I try
to create it, however neither the manual nor help file give much
information.

I have used the TIniFile succesfully previously and it seemed to
create the file on disk, I have tried using Windows API stuff to
create the first instance of the file but it doesn't make any
difference.

Can anyone tell me what the problem is?

PROCEDURE Initialise;
CONST
        PIniName:               Pchar = '\contact.ini';
VAR
  IniFile:                      TIniFile;
  PBuffer, PIni:                PChar;
  S:                    String;
  ReOpenBuff:           TOfStruct;
  F:                    TextFile;
  begin
        {Test if the ini file exists in the Windows directory, uses
some
         PChars  for the Windows API stuff}
     PBuffer := StrAlloc(255);
     PIni := StrNew(PIniName);
     GetWindowsDirectory(PBuffer, 254);
     PBuffer := StrCat(PBuffer, PIni);
     IF OpenFile(PBuffer, ReOpenBuff, OF_EXIST) = -1 THEN
           begin
           {User can chose to create ini or terminate application}
           IF MessageDlg('Can''t find ini file so need to create',
        mtInformation, [mbOK, mbAbort], 0) = mrAbort THEN
        Application.Terminate;
           GetDir(0, S);
           IniFile.Create('contact.ini');

Regards
Ian.

Ian Packer

Email: ipac...@bloggs.win-uk.net
Homepage http://www.ibmpcug.co.uk/~Sunrise

Re:TIniFile.Create causes GPF - Help a Newbie?


Quote
ipac...@bloggs.win-uk.net (Ian Packer) wrote:
>Hello,
>PROCEDURE Initialise;
>CONST
>    PIniName:               Pchar = '\contact.ini';
>VAR
>  IniFile:                  TIniFile;
>  PBuffer, PIni:            PChar;
>  S:                        String;
>  ReOpenBuff:               TOfStruct;
>  F:                        TextFile;
>  begin
>    {Test if the ini file exists in the Windows directory, uses
>some
>     PChars  for the Windows API stuff}
>     PBuffer := StrAlloc(255);
>     PIni := StrNew(PIniName);
>     GetWindowsDirectory(PBuffer, 254);
>     PBuffer := StrCat(PBuffer, PIni);
>     IF OpenFile(PBuffer, ReOpenBuff, OF_EXIST) = -1 THEN
>           begin
>           {User can chose to create ini or terminate application}
>           IF MessageDlg('Can''t find ini file so need to create',
>    mtInformation, [mbOK, mbAbort], 0) = mrAbort THEN
>    Application.Terminate;
>           GetDir(0, S);
>           IniFile.Create('contact.ini');

Man, that's a lot of code.  Use FileExists() from the SysUtils unit
and change your IniFile.Create('contact.ini') line to

IniFile := TIniFile.Create('contact.ini');

or skip the check for existence of the ini file since
TIniFile.Create() will use one if it already exists or will create one
otherwise.

Re:TIniFile.Create causes GPF - Help a Newbie?


In article <4c21af$...@gwen.pcug.co.uk>,
   ipac...@bloggs.win-uk.net (Ian Packer) wrote:

Quote
>Hello,

>I'm trying to use the TIniFile object and its causing a GPF when I try
>to create it, however neither the manual nor help file give much
>information.

>I have used the TIniFile succesfully previously and it seemed to
>create the file on disk, I have tried using Windows API stuff to
>create the first instance of the file but it doesn't make any
>difference.

>Can anyone tell me what the problem is?

>PROCEDURE Initialise;
>CONST
>    PIniName:               Pchar = '\contact.ini';
>VAR
>  IniFile:                  TIniFile;
>  PBuffer, PIni:            PChar;
>  S:                        String;
>  ReOpenBuff:               TOfStruct;
>  F:                        TextFile;
>  begin
>    {Test if the ini file exists in the Windows directory, uses
>some
>     PChars  for the Windows API stuff}
>     PBuffer := StrAlloc(255);
>     PIni := StrNew(PIniName);
>     GetWindowsDirectory(PBuffer, 254);
>     PBuffer := StrCat(PBuffer, PIni);
>     IF OpenFile(PBuffer, ReOpenBuff, OF_EXIST) = -1 THEN
>           begin
>           {User can chose to create ini or terminate application}
>           IF MessageDlg('Can''t find ini file so need to create',
>    mtInformation, [mbOK, mbAbort], 0) = mrAbort THEN
>    Application.Terminate;
>           GetDir(0, S);
>           IniFile.Create('contact.ini');

====> This shoule be IniFile := TIniFile.Create('contact.ini');

It would also be less hassle if you just used the FileExists function to
check for file existance. It just needs a string as an arguement.

-----------------------------------------------
Mike Chapin
Powder River
-----------------------------------------------

Re:TIniFile.Create causes GPF - Help a Newbie?


In article <4c21af$...@gwen.pcug.co.uk>,

Quote
ipac...@bloggs.win-uk.net (Ian Packer) wrote:
> I have used the TIniFile succesfully previously and it seemed to
> create the file on disk, I have tried using Windows API stuff to
> create the first instance of the file but it doesn't make any
> difference.

Also worth considering...: Why bother creating the first instance of
the file? Most apps seems to go with the default values, and don't
explicitly make the INI file.

If you're making a setup application of some sort, then it won't help
you much knowing that the INI file is there. What you want to know is
whether or not the actual key is present...

just my 2 cents which probably doesn't apply to your specific problem
(I see other followups pointed out the constructor thingy).

=\           Q: Where can you get Street Fighting Man, Like A Rolling Stone,
 *=- R.Moberg,  Not Fade Away, Shine A Light, Angie ...and... I'm Free?
=/           A: This and more is on The Rolling Stones' latest CD: stripped!

Re:TIniFile.Create causes GPF - Help a Newbie?


Ian,
your call of TInifile.create('file.ini'); returns a pointer. Your code
doesn't get the pointer and could potentially corrupt memory.

var Initest: Tinifile;
Initest := TiniFile.create('file.ini');

I haven't tried your code, but it just seemed odd. Good Luck!

Ron Zika
rz...@icon-stl.net

Other Threads