Problem Writing to the Boot Sector of a disk (ex. A: Drive)
Can anybody help me on this?
Thanks...
// Here is the code function it fails by // the WriteFile() function call
/////////////////
int CDiskViewDoc::WriteToSector(int drive, DWORD startinglogicalsector,
int numberofsectors)
{
CString szKey =
"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";
unsigned char key[25];
memset(key,0,sizeof(key));
// Converts string to hex
theApp.strtohex(szKey,(unsigned char*)key);
HANDLE hDevice = INVALID_HANDLE_VALUE;
DWORD dwbyteswritten=0, dwerror = 0;
// startinglogicalsector = 2
// drive = 0, // A:Drive
// numberofsectors = 1
char _devicename[] = "\\\\.\\A:";
_devicename[4] += drive;
hDevice = CreateFile(_devicename,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_DELETE |FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (hDevice == INVALID_HANDLE_VALUE)
return -1;
// Setting the pointer to point to the start of the sector we want to read ..
dwerror = SetFilePointer (
hDevice, (startinglogicalsector*512), NULL, FILE_BEGIN);
if(dwerror != (startinglogicalsector*512))
{
CloseHandle(hDevice);
return -1;
}
// It fails here. GetLastError() function returns an 87.
// The parameter is incorrect. ERROR_INVALID_PARAMETER
if (!WriteFile (hDevice, key, sizeof(key)-1,
&dwbyteswritten, NULL) )
{
dwerror = GetLastError();
return -1;
}
CloseHandle(hDevice);
return 0;
}
////////////////

