Solved

access violation when writing ini file

Posted on 2004-08-08
4
324 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now