Discussion:
[fpc-pascal] Detecting IO errors with INI file
James Richters via fpc-pascal
2021-04-29 01:13:40 UTC
Permalink
I'm trying to detect if an INI file is open by another program, but the way
I normally do this is not working.

// open the file for write and keep it open to test
Assign(myfile,'my.ini');
Rewrite(myfile);

{$i-}
Assign(myfile2, 'my.ini';
Rewrite(myfile2); //try to open it for write again
Writeln(ioresult); //correctly displays a 5 for access denied
{$i+}


{$i-}
Log_Ini := TIniFile.Create('my.ini'); // blows up with 217
Writeln(ioresult);
{$i+}

The error I get is:
An unhandled exception occurred at $005A57D0:
EFOpenError: Unable to open file "LOG.INI": The process cannot access the
file because it is being used by another process.

I want to just keep trying to open it until the other process is done. I
don't understand why it's an 'unhandled exception' when I was handling it.
I guess TiniFile.Create has an {$i+} in it and so that's why it's blowing
up?

Any ideas how to keep retrying to access with ini file until it is either
successful or a timeout?

James
Sven Barth via fpc-pascal
2021-04-29 05:46:35 UTC
Permalink
{$i-}
Log_Ini := TIniFile.Create(‘my.ini’); // blows up with 217
Writeln(ioresult);
{$i+}
EFOpenError: Unable to open file "LOG.INI": The process cannot access the
file because it is being used by another process.
I want to just keep trying to open it until the other process is done. I
don’t understand why it’s an ‘unhandled exception’ when I was handling it.
I guess TiniFile.Create has an {$i+} in it and so that’s why it’s blowing
up
TIniFile does not make use of IOResult, because it's from the Delphi time,
not the TP time. Thus you need to use Object Pascal exception handling:

=== code begin ===

try
Log_ini := TIniFile.Create('myini.ini');
except
on e: EFOpenError do
// handle this to decide whether to retry
on e: Exception do
// handle other problems
end;

=== code end ===

Regards,
Sven
James Richters via fpc-pascal
2021-04-29 08:33:22 UTC
Permalink
I get Error: Identifier not found “Try”
Because my unit must be compiled with {$MODE TP}
Otherwise quite a few procedures and functions won’t even compile that only work in Turbo Pascal mode.

I guess I can put it in another unit and make a call to that
 then I can use Try

Thanks for the information.

James
Sven Barth via fpc-pascal
2021-04-29 09:18:00 UTC
Permalink
Post by James Richters via fpc-pascal
I get Error: Identifier not found “Try”
Because my unit must be compiled with {$MODE TP}
You can try to use {$modeswitch exceptions}, I think.

Otherwise quite a few procedures and functions won’t even compile that only
Post by James Richters via fpc-pascal
work in Turbo Pascal mode.
I guess I can put it in another unit and make a call to that
 then I can use Try
Considering that you're using Object Pascal functionality with TIniFile it
might be best to put that into a separate unit.

Regards,
Sven
James Richters via fpc-pascal
2021-04-29 09:39:54 UTC
Permalink
I’m trying to put the Ini file stuff all in it’s own unit.

My unit is {$Mode OBJFPC}
and Uses IniFiles;

“Try” is compiling, but I’m getting Error: Identifier not found “EFOpenError”

And also Syntax error “Do” expected but “)” found

I have:

try
Log_ini := TIniFile.Create('myini.ini');
except
on e: EFOpenError do // Identifier not found “EFOPenError”
Writeln(EFOpenError);
on e: Exception do
Writeln(Exception);
end;

Do I need some other unit for EFOpenError to work?

James
Michael Van Canneyt via fpc-pascal
2021-04-29 11:08:12 UTC
Permalink
Post by James Richters via fpc-pascal
I’m trying to put the Ini file stuff all in it’s own unit.
My unit is {$Mode OBJFPC}
and Uses IniFiles;
“Try” is compiling, but I’m getting Error: Identifier not found “EFOpenError”
And also Syntax error “Do” expected but “)” found
try
Log_ini := TIniFile.Create('myini.ini');
except
on e: EFOpenError do // Identifier not found “EFOPenError”
Writeln(EFOpenError);
on e: Exception do
Writeln(Exception);
end;
Do I need some other unit for EFOpenError to work?
Best add SysUtils and Classes.

Michael.
James Richters via fpc-pascal
2021-04-29 11:38:30 UTC
Permalink
Post by Michael Van Canneyt via fpc-pascal
Best add SysUtils and Classes.
Thanks, that worked. But now I'm getting "cannot read or write variables of this type" for Writeln(Exception) is there an easy way to find out what the exception is?

James

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
https://lists.freepascal.org
Michael Van Canneyt via fpc-pascal
2021-04-29 11:41:55 UTC
Permalink
Post by James Richters via fpc-pascal
Post by Michael Van Canneyt via fpc-pascal
Best add SysUtils and Classes.
Thanks, that worked. But now I'm getting "cannot read or write variables of this type" for Writeln(Exception) is there an easy way to find out what the exception is?
On E : Exception do
Writeln(E.ClassName,' : ',E.Message);

Adapt E to whatever variable you used.

Michael.
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
https://lists.freepascal.org
James Richters via fpc-pascal
2021-04-29 19:08:33 UTC
Permalink
Thanks everyone for the help. I have it working now
I did some more searching and found out I can get "Try" to work with {$Mode TP}
If I add {$Modeswitch exceptions} and appearantly {$R+} to make sure range checking is on.
So I have it working in my {$Mode TP} Unit.

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
https://lists.freepascal.o

Loading...