How to delete a file with open descriptors?

HISTORY OF THE PROBLEM:
Now I use the Windows Media Player SDK 9 to play AVI files in my desktop application. It works well in Windows XP, but when I try to run it in Windows 7 , I find an error - I can not delete the AVI file immediately after playback . The problem is that there are open file descriptors. In Windows XP, I have 2 open file descriptors during file playback, and they are closed after closing the playback window, but in Windows 7 I already have 4 open descriptors during file playback, and 2 of them remain after closing the playback window. They become free only after closing the application.

Question:
How can I solve this problem? How to delete a file with open descriptors? Could there be something like a "forced removal"?

+3
source share
4 answers

The problem is that you are not the only one who processes your file. Other processes and services may also open the file. Therefore, removal is not possible until they release their handles. You can rename the file while these descriptors are open. You can copy the file while these handles are open. Not sure if you can move the file to another container?

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

, " " 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;
}
+2

MoveFileEx MOVEFILE_DELAY_UNTIL_REBOOT. lpNewFileName NULL, Move . , , , .

+1

, avi? , handle.exe. / (), .

unlocker ( ).

.

0

Have you tried asking WMP to free handles instead? ( IWMPCore :: close seems to be doing this)

0
source

Source: https://habr.com/ru/post/1765872/


All Articles