Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1683551
  • 博文数量: 584
  • 博客积分: 13857
  • 博客等级: 上将
  • 技术积分: 11883
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-16 09:34

分类: WINDOWS

2011-03-29 15:13:28

1.不要调用 format.exe DOS 命令的列子
2.不要这个API SHFormatDrive

DeviceIoControl 读写分区表
biosdisk 读写磁盘扇区
外+C语言里的f函数。

#include
#include
#include
#include
void init_mbr(),help(),restor_mbr(),backup_mbr(),display_mbr();
main()
{
char ch;
for(;;)
{
printf("-");
ch=getchar();
switch(tolower(ch))
{
case'd':display_mbr();
break;
case'b':backup_mbr();
break;
case'r':restor_mbr();
break;
case'?':help();
break;
case'i':init_mbr();
break;
case'q':exit(0);
}
getchar();
}
}

void display_mbr()
{
int result;
register int i;
unsigned char buffer[512];
result=biosdisk(4,0,0,0,1,1,buffer);
result&=0x02;
if(result)
{
biosdisk(2,0,0,0,1,1,buffer);
for(i=0;i<512;i++)
{
if(!(i%8))
{
printf(" ");
if(!(i%16))
{
printf("\n");
printf("%04x: ",i);
}
}
printf("%02x ",buffer);
}
}
else
printf("mbr not ready!");
}

void backup_mbr()
{
int result;
FILE *fp;
unsigned char buffer[512];
biosdisk(2,0,0,0,1,1,buffer);
if((fp=fopen("d:\\backup.dpt","wb"))==NULL)
{
printf("cannot creat file\n");
exit(1);
}
else
{
result=fwrite(buffer,sizeof(buffer),1,fp);
(result)?(printf("mbr backup finished"))printf("mbr backup failed"));
}
fclose(fp);
}

void restor_mbr()
{
int result;
FILE *fp;
unsigned char buffer[512];
if((fp=fopen("d:\\backup.dpt","rb"))==NULL)
{
printf("waring! not find the backup file!\n");
exit(1);
}
else
{
fread(buffer,sizeof(buffer),1,fp);
result=biosdisk(3,0,0,0,1,1,buffer);
(result)?(printf("mbr restore finished"))printf("mbr restore fail"));
}
fclose(fp);
}

void help()
{
printf("Commands may be abbrebiated. Supported commands are:\n\n");
printf("d display the mbr datas\n");
printf("b backup the mbr to d:\backup.dpt\n");
printf("r restore the mbr from d:\backup.dpt\n");
printf("i init the mbr to unuse\n");
printf("q exit the program\n");
printf("? display the help list\n");
}

void init_mbr()
{
int result;
unsigned char buffer[512];
FILE *fp;
int i;
for(i=0;i<512;i++)
{
buffer=0;
}
result=biosdisk(3,0,0,0,1,1,buffer);
(result)?(printf("mbr init finished"))printf("mbr init&nb
阅读(999) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~