Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Closed Error Message by ReSet command 1

Status
Not open for further replies.

LarryWheeler

Programmer
Oct 22, 2007
3
0
0
US
File Closed Error Message by ReSet command, I am getting this error message when I try to write to a file for the second time. The 103 error is not supposed to happen with ReSet since this is the command to open a closed file. This occurs in one area of a very large program what has been running in various versions for 20 years. I use Delphi 6. I have checked the CloseFiles commands and they are not in the path of the above process.
 
Check to see if the file is locked by another process or if the file attributes include read only before attempting to open the file.

Code:
function IsFileInUse(fName:  String):  Boolean;
var
   HFileRes:  HFILE;
   Res: String[6];

   function CheckAttributes(FileNam:  String; CheckAttr:  String):  Boolean;
   var
      fa:  Integer;
   begin
      fa := GetFileAttributes(PChar(FileNam));
      Res := '';
      if (fa and FILE_ATTRIBUTE_NORMAL) <> 0 then
      begin
         Result := False;
         Exit;
      end;
      if (fa and FILE_ATTRIBUTE_ARCHIVE) <> 0 then
         Res := Res+'A';
      if (fa and FILE_ATTRIBUTE_COMPRESSED) <> 0 then
         Res := Res+'C';
      if (fa and FILE_ATTRIBUTE_DIRECTORY) <> 0 then
         Res := Res+'D';
      if (fa and FILE_ATTRIBUTE_HIDDEN) <> 0 then
         Res := Res+'H';
      if (fa and FILE_ATTRIBUTE_READONLY) <> 0 then
         Res := Res+'R';
      if (fa and FILE_ATTRIBUTE_SYSTEM) <> 0 then
         Res := Res+'S';
      Result := AnsiContainsText(Res, CheckAttr);
   end;

   procedure SetAttr(fName:  String);
   var
      Attr:  Integer;
   begin
      Attr := 0;
      if AnsiContainsText(Res, 'A') then
         Attr := Attr + FILE_ATTRIBUTE_ARCHIVE;
      if AnsiContainsText(Res, 'C') then
         Attr := Attr + FILE_ATTRIBUTE_COMPRESSED;
      if AnsiContainsText(Res, 'D') then
         Attr := Attr + FILE_ATTRIBUTE_DIRECTORY;
      if AnsiContainsText(Res, 'H') then
         Attr := Attr + FILE_ATTRIBUTE_HIDDEN;
      if AnsiContainsText(Res, 'S') then
         Attr := Attr + FILE_ATTRIBUTE_SYSTEM;
      SetFileAttributes(PChar(fName), Attr);
   end;

begin
   Result := False;
   if not FileExists(fName) then
      Exit;
   if CheckAttributes(fName, 'R') then
   begin
      if MessageDlg(ExtractFileName(fName) +
                    ' is a READ-ONLY File.' + #13#10 +
                    'Do You Wish to Clear the READ-ONLY Flag?',
                    mtConfirmation, [mbYes, mbNo], 0) = mrNo then
      begin
         Result := True;
         Exit;
      end;
   end;
   SetFileAttributes(PChar(fName), FILE_ATTRIBUTE_NORMAL);
   SetAttr(fName);
   HFileRes := CreateFile(PChar(fName), GENERIC_READ or GENERIC_WRITE,
                      0,nil,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
   Result := (HFileRes = INVALID_HANDLE_VALUE);
   if Not Result then
      CloseHandle(HFileRes);
end;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top