hello, everyone!
I want to write a block of binary code into physical disk from the first sector(mbr) to almost 10th sector. What I have done is
listed below:
HANDLE OpenDrive(char *drive)
{
char devname[64];
if (isalpha(*drive))
sprintf(devname,“\\.\%c:”,*drive);
else
sprintf(devname,“\\.\PHYSICALDRIVE%c”,*drive);
return CreateFile(devname, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, 0, NULL);
}
int LockVolume(HANDLE hDisk)
{
unsigned long count; // what’ does count mean???
return DeviceIoControl(hDisk,FSCTL_LOCK_VOLUME, NULL,
0,NULL,0,&count,NULL);
}
int UnlockVolume(HANDLE hDisk)
{
unsigned long count;
return DeviceIoControl(hDisk,FSCTL_UNLOCK_VOLUME, NULL,
0,NULL,0,&count,NULL);
}
int WINAPI WinMain(HMODULE hModule, HMODULE hNull, LPSTR lpCmdLine, int nShowCmd)
{
char *drive = “0”;
HANDLE hDiskDev = OpenDrive(drive);
LockVolume(hDiskDev);
WriteFile(hDiskDev, buf, 10 * 512, &dwWriten, NULL); // Can I write in???
UnlockVolume(hDiskDev);
CloseHandle(hDiskDev);
}
these code may be not stable, sometime they can write code into physical disk, other times can’t. why?
b???.???????&?v?'?ׯj?.n?Qyȩf??]?:.?˛???m??֛???zf???%y?ޞ?^?˛??^r*Lzfެ? ???l??ܢ