I want to use it during uninstall procedure to warn the user ahead. The procedure should work for W2000+, hence Vista API's are not allowed.
This seems to catch some conflicts:
if( GetFileAttributes( lpPath ) == INVALID_FILE_ATTRIBUTES )
{
// File does not exist
}
else
{
BOOL bCanDelete = FALSE ;
HANDLE hFile = CreateFile( path,
GENERIC_WRITE /*|DELETE*/,
0 /*FILE_SHARE_DELETE*/,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if( hFile != INVALID_HANDLE_VALUE )
{
DWORD size = 10000 ; // a number > file size allowed
if( size != INVALID_FILE_SIZE )
{
if( LockFile( hFile, 0,0, size,0) )
{
UnlockFile( hFile, 0,0, size,0) ;
bCanDelete = TRUE ;
}
}
CloseHandle( hFile ) ;
}
}
Namely it detects these situations: a) Deleting running exe file b) Deleting opened pdf
Using GENERIC_WRITE|DELETE seems to behave similarly. Using DELETE alone works for the case b), but not for a).
I have no positive evidence that LockFile() catches any meaningful conflict, but suppose it does.
Does anybody have a better idea?