4

PROBLEM HISTORY:
Now I use Windows Media Player SDK 9 to play AVI files in my desktop application. It works well on Windows XP but when I try to run it on Windows 7 I caught an error - I can not remove AVI file immediately after playback. The problem is that there are opened file handles exist. On Windows XP I have 2 opened file handles during the playing file and they are closed after closing of playback window but on Windows 7 I have already 4 opened handles during the playing file and 2 of them remain after the closing of playback window. They are become free only after closing the application.

QUESTION:
How can I solve this problem? How to remove the file which has opened handles? May be exists something like "force deletion"?

tshepang
  • 12,111
  • 21
  • 91
  • 136
SKINDER
  • 950
  • 3
  • 17
  • 39

4 Answers4

3

The problem is that you're not the only one getting handles to your file. Other processes and services are also able to open the file. So deleting it isn't possible until they release their handles. You can rename the file while those handles are open. You can copy the file while those handles are open. Not sure if you can move the file to another container, however?

Other processes & services esp. including antivirus, indexing, etc.

Here's a function I wrote to accomplish "Immediate Delete" under Windows:

bool DeleteFileNow(const wchar_t * filename)
{
    // don't do anything if the file doesn't exist!
    if (!PathFileExistsW(filename))
        return false;

    // determine the path in which to store the temp filename
    wchar_t path[MAX_PATH];
    wcscpy_s(path, filename);
    PathRemoveFileSpecW(path);

    // generate a guaranteed to be unique temporary filename to house the pending delete
    wchar_t tempname[MAX_PATH];
    if (!GetTempFileNameW(path, L".xX", 0, tempname))
        return false;

    // move the real file to the dummy filename
    if (!MoveFileExW(filename, tempname, MOVEFILE_REPLACE_EXISTING))
    {
        // clean up the temp file
        DeleteFileW(tempname);
        return false;
    }

    // queue the deletion (the OS will delete it when all handles (ours or other processes) close)
    return DeleteFileW(tempname) != FALSE;
}
Mordachai
  • 9,412
  • 6
  • 60
  • 112
  • But if these files (video files!) are too large and its amount is too large I will get a lack of free space using this strategy - temporary files would have eaten my drive space. I download file, play it, and remove many times. – SKINDER Jan 13 '11 at 11:33
  • 2
    Erm - I don't think you grasp the situation. The above technique doesn't create a copy of the files - it merely renames them so that the original file is instantly gone. The renamed file will still be deleted exactly as fast as it can be - no slower, no faster. There is no way to force the OS to delete it any sooner, and generally speaking, it's a matter of a couple of seconds delay (just long enough for you antivirus or indexing service to close its file handle and allow the file to disappear). – Mordachai Jan 13 '11 at 21:51
1

Technically you can delete a locked file by using MoveFileEx and passing in MOVEFILE_DELAY_UNTIL_REBOOT. When the lpNewFileName parameter is NULL, the Move turns into a delete and can delete a locked file. However, this is intended for installers and, among other issues, requires administrator privileges.

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
  • Can you give a proof link that MoveFileEx works this way that is "Move turns into a delete and can delete a locked file"? – SKINDER Jan 13 '11 at 11:36
  • 2
    In the function specifications on MSDN, the link in my post: 'If dwFlags specifies MOVEFILE_DELAY_UNTIL_REBOOT and lpNewFileName is NULL, MoveFileEx registers the lpExistingFileName file to be deleted when the system restarts. If lpExistingFileName refers to a directory, the system removes the directory at restart only if the directory is empty.' – Remus Rusanu Jan 13 '11 at 16:44
0

Have you checked which application is still using the avi file? you can do this by using handle.exe. You can try deleting/moving the file after closing the process(es) that is/are using that file.

The alternative solution would be to use unlocker appliation (its free).

One of the above two method should fix your problem.

Vikram.exe
  • 4,565
  • 3
  • 29
  • 40
  • I use COM interfaces to interact with Windows Media Player and I need to remove locked file programmatically. – SKINDER Jan 13 '11 at 11:24
  • in that case, you will have to write a kernel driver which will traverse the open file handle list and then get the name of the owner process and then do whatever you want with that process (from the kernel space) :) – Vikram.exe Jan 13 '11 at 13:08
0

Have you already tried to ask WMP to release the handles instead? (IWMPCore::close seems to do that)

Roman L
  • 3,006
  • 25
  • 37