Solved

access violation when writing ini file

Posted on 2004-08-08
4
328 Views
Last Modified: 2010-04-05
I have been working on this for most of the day.  its driving me nuts.    I have a series of textfields that are filled in from an ini file.    Here is all relevent code:

to read the information

procedure TForm1.Settings2Click(Sender: TObject);
begin
settingsini := TIniFile.Create('.\settings.ini');
       try

       ftphost := settingsini.readstring('Server Settings','host', 'none' );
       ftpusername := settingsini.readstring('Server Settings','username', 'none');
       ftppassword := settingsini.readstring('Server Settings','password', 'none');
       ftppath := settingsini.readstring('Server Settings','path', 'none');

       frmsettings.txtFtpHost.text := ftphost;
       frmsettings.txtFtpusername.text := ftpusername;
       frmsettings.txtFtppassword.text := ftppassword;
       frmsettings.txtFtpPath.text := ftppath;
       except
      showmessage('Settings.ini - problem reading inifile');
       end;                                                            
       free;




frmsettings.Show;
end;







to write it back to file when "save" button is clicked

procedure Tfrmsettings.PDJXPButton1Click(Sender: TObject);
begin

settingsini := TIniFile.Create('.\settings.ini');
  with settingsini do
      begin
    try
          writestring('Server Settings','host', frmsettings.txtFtpHost.text);
          writestring('Server Settings','username', frmsettings.txtFtpusername.text);
          writestring('Server Settings','password', frmsettings.txtFtppassword.text);
          writestring('Server Settings','path', frmsettings.txtFtpPath.text);
     except
     showmessage('there was a problem writing the file');
     end;
        free;
      end;
      frmsettings.hide;
end;



/////////////////////////////////////end code

I have no problem READING the file.  When I try to WRITE the file again I run into an access violation.   I dont know why this happens.   I have tried a number of things many of them obviously incorrect even to me but still have not had luck.  I tried removing the second tinifile.create (the writing one) based on the idea that it had already been done.  This neither fixed the problem nor made it worse.

Anyone have any ideas? I'm always on a deadline but this one is rather worse than usual.




0
Comment
Question by:hibbidiji
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
LRHGuy earned 300 total points
ID: 11748872
It would be helpful, if you know it, to reveal at what point the error pops up? (which line is the culprit?)

I tried your code, and it worked fine here. The only change I made was:
 
in settings2clisk, change the Free to settingsini.free

I didn't try the creating the input fields, but I doubt if that's where the error lies.
0
 
LVL 7

Expert Comment

by:LRHGuy
ID: 11748889
I tried it again, and making it settingsini.free in settings2click should fix it. I put it back the way you had it, and forced it to read, then call the write routine, and I got your error message.

Hope that helps!
0
 
LVL 17

Assisted Solution

by:Wim ten Brink
Wim ten Brink earned 200 total points
ID: 11749055
What you're doing is basically plain stupid.

Sorry but lines like:
* writestring('Server Settings','host', frmsettings.txtFtpHost.text);
Just don't make sense if you define them in a class named Tfrmsettings. Chances are that the form just doesn't even know the right value for frmsettings. Try using
* writestring('Server Settings','host', txtFtpHost.text);
instead... The difference is that at the moment you call PDJXPButton1Click, the variable frmsettings is probably unassigned. If not, the value frmsettings might be pointing at the wrong form. And that's what creates your access violation...

Just an example of erroneous copying&pasting code, I guess. :-)
0
 
LVL 1

Author Comment

by:hibbidiji
ID: 11750029
The program was crashing because of both problems.   I gave the extra 100 to the first answer on the buzzer :)   Thanks guys and alex, dont apologize for my MISERABLE coding. I'm a php guy and new to delphi.  Its just my inexperiance talking...  :)
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question