Hi all,
I searched through all the past messages on FlushFileBuffers used on a raw device, and I couldn’t find anything that helps with my problem. It seems to be with Vista and Windows 7, with particular types of drives (seemingly IDE drives, although I’m not sure if it’s a subclass of IDE drives), my FlushFileBuffers does not cause all the file system cached data to be written to the device. My code looks like this:
HANDLE hDrive;
char drive[100] ; // buffer to construct the drive name to open
// Flush has to be open with shared access:
sprintf(drive, “\\.\PHYSICALDRIVE%d”, driveNum) ;
hDrive = CreateFile ( drive,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hDrive == INVALID_HANDLE_VALUE)
return ERR_OPEN ;
if( !FlushFileBuffers(hDrive) )
{
DWORD result = GetLastError();
CloseHandle(hDrive) ;
return (int) result;
}
CloseHandle(hDrive);
return RETURN_OK;
I am doing a sector-by-sector image of the drive, from sector 0 to sector N, but before I do that, I add some drivers to the image. What I find is that there is around a 30% chance of my drivers being present in the finished image - in the 70% case some of the drivers will be missing, or one of them will be corrupted. Immediately after this CloseHandle, I re-open the raw device without the Share attributes to get more exclusive access to the disk.
All of this works perfectly in XP, but it seems that Vista / Windows 7, with certain drives, the flush doesn’t work all the time (as stated above). Note that I am using a single partition installation in both Vista and Windows 7 case - I don’t have the 100mB system partition.
Should the “PHYSICALDRIVE0” filename work here, or should it be “\.\c:” (with appropriate escapes added for the back-slashes)? GetLastError is returning an error code of “1” (invalid function), which according the the google is normal for a raw device, although it seems a little weird to me.