CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2008
    Posts
    163

    File Open Failed After CopyFileW

    Hi,

    I am facing an issue of file open failure after CopyFileW.
    I suspect that some of the handle which is part of copy is still holding the file.
    How can i wait till the OS handle free the file.
    Any API's available to check this ?

    -Dave

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: File Open Failed After CopyFileW

    1. Define "failed".
    2. What is the reason of your "suspection"?
    3. You may also want to show your code.
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: File Open Failed After CopyFileW

    I am facing an issue of file open failure after CopyFileW.
    On the original? On the copy?

    As Victor said in his post #2, it would be helpful if you posted the code so we could comment further.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: File Open Failed After CopyFileW

    If you copied over the network, then yes, this can happen. The OS will release the copy command early.
    It can also happen with a local copy as well if the copy and open are in another process. The copy is complete, but it takes the OS file system a while to finish up all the linkages in the filesystem for the other thread to see it (this can take up to a few seconds), and it can take considerably longer still on slower devices such as memorysticks.

    There's not much you can do about it, try to open the file, if it fails, wait/sleep a short while and retry.

  5. #5
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: File Open Failed After CopyFileW

    The same issue can arise if you programmatically terminate a process that has open files. It takes a while for the OS to clean up the mess and release the file handles, so a retry loop will be needed.
    Nobody cares how it works as long as it works

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

Featured