Arno Schoedl
2009-01-31 11:45:35 UTC
Hello,
I asked this in a previous thread, but did not get an answer, so I
will open a specific thread for it.
A file opened with FILE_FLAG_DELETE_ON_CLOSE will be deleted after all
handles to it have been released as documented in MSDN.
But I noticed that after closing the _original_ file handle with which
the file has been created, no more new handles to it can be opened.
Can anyone confirm this?
Here is my test code:
//////////////////////////////////
// Demonstration of CreateFile FILE_FLAG_DELETE_ON_CLOSE
behavior
// We must keep the original handle open!
{
TCHAR szFile[MAX_PATH];
APIERR( TempFile( szFile ) );
HANDLE hfileWrite=::CreateFile(
szFile,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
// GetTempFileName creates an empty file
TRUNCATE_EXISTING,
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED |
FILE_ATTRIBUTE_TEMPORARY |
FILE_FLAG_DELETE_ON_CLOSE,
NULL );
APIERR( hfileWrite!=INVALID_HANDLE_VALUE );
HANDLE hfileRead1=::CreateFile(
szFile,
GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE|
FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
0, // ignored on open
NULL );
APIERR( hfileRead1!=INVALID_HANDLE_VALUE );
APIERR( CloseHandle( hfileWrite ) );
HANDLE hfileRead2=::CreateFile(
szFile,
GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE|
FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
0, // ignored on open
NULL );
APIERRIGNORE( hfileRead2!=INVALID_HANDLE_VALUE,
(ERROR_ACCESS_DENIED) );
// CreateFile fails if the original handle has been
closed, even if
// another handle to the same file is still held.
_ASSERT( hfileRead2==INVALID_HANDLE_VALUE );
APIERR( CloseHandle( hfileRead1 ) );
I asked this in a previous thread, but did not get an answer, so I
will open a specific thread for it.
A file opened with FILE_FLAG_DELETE_ON_CLOSE will be deleted after all
handles to it have been released as documented in MSDN.
But I noticed that after closing the _original_ file handle with which
the file has been created, no more new handles to it can be opened.
Can anyone confirm this?
Here is my test code:
//////////////////////////////////
// Demonstration of CreateFile FILE_FLAG_DELETE_ON_CLOSE
behavior
// We must keep the original handle open!
{
TCHAR szFile[MAX_PATH];
APIERR( TempFile( szFile ) );
HANDLE hfileWrite=::CreateFile(
szFile,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
// GetTempFileName creates an empty file
TRUNCATE_EXISTING,
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED |
FILE_ATTRIBUTE_TEMPORARY |
FILE_FLAG_DELETE_ON_CLOSE,
NULL );
APIERR( hfileWrite!=INVALID_HANDLE_VALUE );
HANDLE hfileRead1=::CreateFile(
szFile,
GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE|
FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
0, // ignored on open
NULL );
APIERR( hfileRead1!=INVALID_HANDLE_VALUE );
APIERR( CloseHandle( hfileWrite ) );
HANDLE hfileRead2=::CreateFile(
szFile,
GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE|
FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
0, // ignored on open
NULL );
APIERRIGNORE( hfileRead2!=INVALID_HANDLE_VALUE,
(ERROR_ACCESS_DENIED) );
// CreateFile fails if the original handle has been
closed, even if
// another handle to the same file is still held.
_ASSERT( hfileRead2==INVALID_HANDLE_VALUE );
APIERR( CloseHandle( hfileRead1 ) );