The CreateFile function has a flag called FILE_, which means that the file will be deleted when the last handle to the file is closed. But what if you pass that flag and then change your mind? Is there a way to call take-backs?
No, there are no take-backs. The FILE_ flag is permanent.
So what do you do if you want to make a file deleted when the last handle is closed, but only based on some condition determined later?
What you can do is open the file normally, and then once you realize that you want to delete it on last close, you can turn the “delete on close” flag on.
BOOL MarkFileAsDeleteOnClose(HANDLE file)
{
FILE_DISPOSITION_INFO info{};
info.DeleteFile = TRUE;
return SetFileInformationByHandle(hfile,
FileDispositionInfo, &info, sizeof(info));
Unlike FILE_, you can take back the DeleteFile disposition.
BOOL MarkFileAsNoLongerDeleteOnClose(HANDLE file)
{
FILE_DISPOSITION_INFO info{};
info.DeleteFile = FALSE;
return SetFileInformationByHandle(hfile,
FileDispositionInfo, &info, sizeof(info));
Thank you! I was thinking about this exact problem and found no solution to my problem. I’m working on an encryption project where it’s important that the input file is shredded properly on successful encryption. It creates a memory mapped file and it uses BCryptGenRandom to defeat hidden compression logic that may exist. I also use the FILE_FLAG_WRITE_THROUGH to make sure it’s properly overwritten. This is what I looked for.
As about FILE_FLAG_DELETE_ON_CLOSE - there is one another big limitation. FILE_FLAG_DELETE_ON_CLOSE makes the file process-exclusive.
The use of that flag was clearly inspired by POSIX (UNIX) "unlink" - both file names and open handles are mere hardlinks (a la NTFS junctions, a la Linux /proc/*/fd), so you do not have to even have a file name to have a file.
Then the file volume is auto-collected when nobody uses it anymore (unless hardware/kernel crash, maybe, but then fsck should).
Simple and easy, IUnknown-like, IPC, when latency does not mean much. Like spawning helper processes and passing information.
Like... passing temporary report files...
no other feedback avenue it seems, so catchign a chance
https://devblogs.microsoft.com/oldnewthing/20151015-00/?p=91351
Down there above the table the “function was not intended for public consumption” – the link is broken