How can I write physicl disk?

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??ܢ

Hi,

for starters you should add error handling to your code and call
FormatMessage to get the string associated with the last error:

LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default
language
(LPTSTR) &lpMsgBuf,
0,
NULL
);

This may help you determine why things fail sometimes. It’s good practice
anyway :slight_smile:

Sander

-----Original Message-----
From: Lizhiwei [mailto:leezhiwei@263.net]
Sent: Tuesday, June 12, 2001 11:59 AM
To: NT Developers Interest List
Subject: [ntdev] How can I write physicl disk?

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> ?j)m?Ws???u???C-±?<+


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com