Announcement

Collapse
No announcement yet.

Intermittent Permission denied Error when trying to flush .INI-file

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Intermittent Permission denied Error when trying to flush .INI-file

    Hi

    I try to flush the Cache of a .INI-File in .DLL in order to make sure all data is written on the disk before I hand over control to an other program, that will read this .INI-file. The .INI-File is handled by means of WritePrivateProfileString/GetPrivateProfileString.

    However there seems to be a problem under certain circumstances: we do get an error 70. The occurence of the problem seems to be limited to 2 machines of one (1) customer (running WinXP). The DLL is installed on at least 25 other machines (none of them the same customer as above) too and there we have no reports of such a problem there. The calling application is unique to the customer having problems.

    I added 2 MSGBOXes to my code in order to hunt he error:
    Code:
    FUNCTION Ini_Flush(sFNPath AS STRING) AS LONG
    ' Flushs .INI-File Cache to Disk
    ' returns %FALSE for no errors, %TRUE for unable to write file
    LOCAL fhandle       AS LONG
    LOCAL szFNPath      AS ASCIIZ * 512
    LOCAL funcRes       AS LONG 
      ' Check for a existence of file/write-protected disk/Permission denied first! Exit routine if found.
      szFNPath = sFNPath + CHR$(0)
      ON ERROR GOTO WriteIniError
      fhandle = FREEFILE
      IF DIR$(szFNPath) = "" THEN  ' Avoid a file not found error on INPUT.
        OPEN szFNPath FOR OUTPUT AS fhandle
        CLOSE fhandle
      ELSE
        OPEN szFNPath FOR INPUT AS fhandle
        CLOSE fhandle
      END IF
    
      funcRes = WritePrivateProfileString(BYVAL %NULL, BYVAL %NULL, BYVAL %NULL, szFNPath)
    
      IF funcRes=0 THEN
        Ini_Flush = %FALSE
      ELSE
        ' Hmmm, I do not expect this ever to be executed. MSDN says: "... If the function 
        ' fails, or if it flushes the cached version of the most recently accessed initialization 
        ' file, the return value is zero. ..."
        MSGBOX "InifileOps.Ini_Flush  --  unexpected Result (1)"
        Ini_Flush = %TRUE
      END IF
      GOTO WriteIniExit
    
    WriteIniError:
      fhandle = ERRCLEAR
      MSGBOX "InifileOps.Ini_Flush  --  unexpected Result (2). ErrCode: " & STR$(fhandle) 
      Ini_Flush = %TRUE
    WriteIniExit:
      ON ERROR GOTO 0
    END FUNCTION
    The Messagebox shows up randomly and gives the following message:
    InfileOps.Ini_Flush -- unexpected Result (2). ErrCode: 70

    ErrCode: 70: Permission denied - (%ERR_PERMISSIONDENIED)
    -- You tried to write to a write-protected disk. This error can also be generated as a result of network permission errors, such as accessing a locked file, or a locked record. It can also occur when attempting to open a subdirectory as a file.

    Hmm, that explanation seems not fitting: the .INI generated by this DLL is on the local Drive C: and it uses always the same path and filename. Prior to writing to the file it is deleted and before I pass control to the calling application I rename the file. This seems to work, even aftert an Error 70. The .INI-file seems to be truncated in the application, meaning he first part of it seems to belong to the last call of the .DLL

    I thought about removing the code to 'Check for a existence of file/write-protected disk/Permission denied first!' But I think I think this might just postpone the the problem to a situation a flush is tried without having written to a .INI...

    Any good ideas what might cause this intermittent error on these machines?

  • #2
    The OPEN+CLOSE are unnecessary, as WritePrivateProfileString () will return an error code if the file is not found.

    I bring this up because I know CLOSE is sometimes delayed (windows considers it a low-priority operation), and WritePrivateProfileString may be encountering a file-in-use condition because the CLOSE has not been completed.

    Yes, I know the O/S caching logic is supposed to handle this but I have my doubts that it always works "as documented."

    I'd try checking existence of the file, if if found go ahead and WritePrivateProfileString() and if not found just exit. e.g
    Code:
      IF DIR$ (file) > "" THEN 
          WritePrivateProfileString...
      ELSE
         FUNCTION = errror meaning "something wrong, can't find INI file"
      END IF
    Or maybe it's just returning an error because there is nothing to flush? Maybe only when the file is being created?

    MCM
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Where is the file located? You know that even XP will lock access to some folders if the user is running in "limited" account mode.
      kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

      Comment


      • #4
        @Michael
        I think your proposal for code is a proper idea - and very simple! I like it much more than the current solution. I'll try this.

        I'm sure there is something to flush, I write the results into this file prior to the flush.

        Please note: not WritePrivateProfileString returns Error 70. It is the operations used to 'Check for a existence of file/write-protected disk/Permission' that causes this Error 70

        @Kev
        The file location is always C:\MyApplication\Temp\MyResult.INI. As not every call generates an Error 70 I think we can exclude the idea of userrestrictions.

        Comment


        • #5
          @Michael
          Are you sure about "...WritePrivateProfileString () will return an error code if the file is not found..."
          MSDN says: "... If the function fails, or if it flushes the cached version of the most recently accessed initialization file, the return value is zero. ...". To me this sounds like WritePrivateProfileString will always return zero, if you use it in order to flush the cache.

          I changed my code - and will test this:
          Code:
          FUNCTION Ini_Flush(sFNPath AS STRING) AS LONG
          ' Flushs Cache to Disk
          ' returns %FALSE for no errors, %TRUE for unable to write file
          LOCAL fhandle       AS LONG
          LOCAL szFNPath      AS ASCIIZ * 512
          local funcRes as long 
          ' Check for a existence of file/write-protected disk/Permission denied first! Exit routine if found.
            szFNPath = sFNPath + CHR$(0)
            ON ERROR GOTO WriteIniError
            IF DIR$(szFNPath) <> "" THEN  
              'file exists
              funcRes = WritePrivateProfileString(BYVAL %NULL, BYVAL %NULL, BYVAL %NULL, szFNPath)
              IF funcRes=0 THEN
                Ini_Flush = %FALSE
              ELSE
                MSGBOX "InifileOps.Ini_Flush  --  unexpected Result (1)"
                Ini_Flush = %TRUE
              END IF
            ELSE
              ' file does not exist
              MSGBOX "InifileOps.Ini_Flush  --  Tried to flush inexisting .INI-File - " & sFNPath
              Ini_Flush = %TRUE
            END IF
            GOTO WriteIniExit
          
          WriteIniError:
            fhandle = ERRCLEAR
            msgbox "InifileOps.Ini_Flush  --  unexpected Result (2). ErrCode: " & STR$(fhandle) & " " & sFNPath
            Ini_Flush = %TRUE
          WriteIniExit:
            ON ERROR GOTO 0
          END FUNCTION
          Last edited by Walter Schütz; 27 Nov 2008, 01:07 AM.

          Comment


          • #6
            I'm not sure about the flush (null params)... the doc goes into all kinds of registry stuff and has exceptions for Win/9x...

            ... but I AM sure if the file does not exist, there cannot be anything to flush.

            For that matter, I am sure if your function is called with a non-existent file, your principal problem is NOT with flushing any cache.

            MCM
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment

            Working...
            X