As described here, using SetFileInformationByHandle with FILE_DISPOSITION_INFO allows one to set a file with an open handle to be deleted upon all handles being closed.
In order to make FILE_DISPOSITION_INFO
work you need to specify the DELETE access in the CreateFile
function as reported in https://msdn.microsoft.com/en-us/library/windows/desktop/aa365539(v=VS.85).aspx:
You must specify appropriate access flags when creating the file handle for use with SetFileInformationByHandle. For example, if the application is using FILE_DISPOSITION_INFO with the DeleteFile member set to TRUE, the file would need DELETE access requested in the call to the CreateFile function. To see an example of this, see the Example Code section. For more information about file permissions, see File Security and Access Rights. I.e.
//...
HANDLE hFile = CreateFile( TEXT("tempfile"),
GENERIC_READ | GENERIC_WRITE | DELETE, //Specify DELETE access!
0 /* exclusive access */,
NULL,
CREATE_ALWAYS,
0,
NULL);
But it seems that an handle created with OpenFileById()
cannot be used because the function cannot accept the DELETE
flag.
From https://msdn.microsoft.com/en-us/library/windows/desktop/aa365432(v=vs.85).aspx on OpenFileById()
it can be read:
dwDesired
Access [in]
The access to the object. Access can be read, write, or both.
Even setting DELETE
or GENERIC_ALL
the function fails.
If you replace the handle passed to SetFileInformationByHandle
with one created with the CreateFile
function having the DELETE
flag set, as above, it works.