Results 1 to 6 of 6

Thread: File Handle Closing

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Posts
    94

    File Handle Closing

    Hey Everyone

    I have a quick question regarding the FindFile API Call, if im doing a recurse, and i do something like this:
    VB Code:
    1. lngFileHandle = FindFirstFile(DirectoryString & "*.*", WFD)

    and close the handle like this:
    VB Code:
    1. lngFileHandle = FindClose(lngFileHandle)

    What would happen, if, for whatever reason, the function it was in generated an error and the file handle was never closed. Would this have an adverse affect on my program? On the file? Or on anything related?

    Hope anyone can answer this

    Thanks, Gav

  2. #2
    Fanatic Member
    Join Date
    Dec 2003
    Posts
    703
    Why not check handles and close valid ones in your error handler?
    an ending

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Posts
    94
    I could do that... i was thinking of something like this:

    VB Code:
    1. if lngFileHandle <> 0 then
    2. 'Close Handle Here
    3. endif

    However, what i really wanted to know, is what would happen if file handles were left unclosed. Also, in the error handler, what would happen if it generated an error closing the file handle? I dont like putting things in error handles that could generate errors (Would this happen?)

    Hope that makes sense!

    Thanks, Gav

  4. #4
    Fanatic Member
    Join Date
    Dec 2003
    Posts
    703
    FindClose will not raise a VB error.

    If you don't close the handles, you'll leak some memory.
    an ending

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Posts
    94
    Okay Brilliant

    So, it wont interfere with the actual file itself? It would just leak a bit of memory (because VB has allocated the handle?). I think im gonna close the handle then, is what i suggested for the close handle correct?

    Thanks again!

    Gav

  6. #6
    Fanatic Member
    Join Date
    Dec 2003
    Posts
    703
    No I'm pretty sure it won't intefere with the file, because you haven't actually opened it.

    You should check against INVALID_HANDLE_VALUE for closing:

    if handle <> INVALID_HANDLE_VALUE then ..
    an ending

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width