新手写的,大牛勿喷。。。
以后会一直更新
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include "HOSTSCODE.h"
#define DiskSize 512 //一个扇区512字节
#define SetTitle() SetConsoleTitle("Master Boot Record By:紫玫冰心")
void ReadMBR();//读取MBR
void WriteMBR(unsigned char *Byte);//改写MBR
void pause();
int WriteHOSTS(char *data);//设置域名重定向
BOOL EmptyHOSTS();//清空HOSTS
void ReadHOSTS();//读取HOSTS文件内容
void ReductionHOSTS();
void about();//关于
int info();//系统信息
void help();
int bl;
char command[1000];
int main(int argc,char* argv[])
{
if(argc==2 && !strcmp(argv[1],"ReadMBR"))
{
SetTitle();//设置控制台程序标题
ReadMBR();//读MBR
}
else if(argc==3 && !strcmp(argv[1],"WriteMBR"))
{
SetTitle();//设置控制台程序标题
//写MBR
FILE *MBRFile;
unsigned char MBR[DiskSize]={0};
MBRFile=fopen(argv[2],"rb+");
if(!MBRFile)
{
printf("Can't open file");
return 0;
}else if(!feof(MBRFile))
{
fread(MBR,1,DiskSize,MBRFile);
WriteMBR(MBR);
fclose(MBRFile);
}
}else if(argc==2 && !strcmp(argv[1],"information"))
{
SetTitle();//设置控制台程序标题
info();//系统信息
}
else if(argc==2 && !strcmp(argv[1],"command"))//CMD命令
{
while(1)
{
SetTitle();//设置控制台程序标题
printf("command->");
fgets(command,sizeof(command)+1,stdin);
if(!strcmp("exit\n",command))
break;
system(command);
}
return 0;
}else if(argc==2 && !strcmp(argv[1],"EmptyHOSTS"))
{
SetTitle();
if(EmptyHOSTS()==TRUE)
printf("EmptyHOSTS is successful");
else
printf("Can't EmptyHOSTS");
}else if(argc==4 && !strcmp(argv[1],"WriteHOSTS"))
{
FILE *File;
SetTitle();
char web[300]={0};
strcat(web,argv[3]);
strcat(web," ");
strcat(web,argv[2]);
if(WriteHOSTS(web)==-1)
printf("Can't write hosts");
else
printf("Complete!");
}else if(argc==2 && !strcmp(argv[1],"about"))
{
SetTitle();
about();
}else if(argc==2 && !strcmp(argv[1],"ReadHOSTS"))
{
SetTitle();
ReadHOSTS();
}else if(argc==2 && !strcmp(argv[1],"ReductionHOSTS"))
{
SetTitle();
ReductionHOSTS();
}else if(argc==2 && !strcmp(argv[1],"help"))
{
SetTitle();
help();
}else{
SetTitle();
help();
}
return 0;
}
void ReadMBR()
{
//硬盘文件
FILE *Disk;
//保存文件
FILE *File;
int j;
//引导记录
unsigned char MBRCode[DiskSize]={0};
Disk=fopen("\\\\.\\PhysicalDrive0","rb+");//打开硬盘
if(!Disk)
printf("Hard Disk not found!");//没有打开硬盘
else if(!feof(Disk))
{
fseek(Disk,0,SEEK_SET);//移动到引导记录的位置
fread(MBRCode,1,DiskSize,Disk);//读取引导记录
fclose(Disk);//关闭硬盘
File=fopen("MBR.ima","wb+");//硬盘文件
if(!File)
puts("Can't create 'MBR.ima'!");//没有创建文件
else if(!feof(File))
{
//将引导记录保存
fwrite(MBRCode,DiskSize,1,File);
fclose(File);//关闭文件
printf("Master Boot Record(Machine Code-char)\n");
for(j=0;j<DiskSize;j++)
printf("%c",MBRCode[j]);
printf("\nMaster Boot Record(Machine Code-int)\n");
for(j=0;j<DiskSize;j++)
{
if(j<511)
printf("%d\t",MBRCode[j]);
else
printf("%d\n",MBRCode[j]);
}
if(MBRCode[510]==85 && MBRCode[511]==170)//引导记录结束标记
printf("The Master Boot Record is ok!");
else
printf("Operating system not found!");
}
}
}
void WriteMBR(unsigned char *Byte)
{
//硬盘文件
FILE *ADisk;
//保存文件
FILE *AFile;
//引导记录
unsigned char AMBRCode[DiskSize]={0};
ADisk=fopen("\\\\.\\PhysicalDrive0","rb+");//打开硬盘
if(!ADisk)
puts("Hard Disk not found!");//没有打开硬盘
else if(!feof(ADisk))
{
fseek(ADisk,0,SEEK_SET);//移动到引导记录的位置
fread(AMBRCode,1,DiskSize,ADisk);//读取引导记录
AFile=fopen("MBR.ima","wb+");//硬盘文件
if(!AFile)
puts("Can't create 'MBR.ima'!");//没有创建文件
else if(!feof(AFile))
{
printf("MBRCode:\n");
for(bl=0;bl<DiskSize;bl++)
printf("%c",Byte[bl]);
//将引导记录保存
fwrite(AMBRCode,DiskSize,1,AFile);
fseek(ADisk,0,SEEK_SET);//移动到引导记录的位置
fwrite(Byte,DiskSize,1,ADisk);
fclose(AFile);//关闭文件
}
fclose(ADisk);//关闭硬盘
}
}
int info()
{
/* 查看系统信息 */
SYSTEM_INFO s;
OSVERSIONINFOEX o;
GetSystemInfo(&s);
printf("OemID:%u\n",s.dwOemId);
printf("ProcessorArchitecture:%u\n",s.wProcessorArchitecture);
printf("PageSize:%u\n",s.dwPageSize);
printf("MinimumApplicationAddress:%u\n",
s.lpMinimumApplicationAddress);
printf("MaximumApplicationAddres:%u\n",
s.lpMaximumApplicationAddress);
printf("ActiveProcessorMask:%u\n",s.dwActiveProcessorMask);
printf("NumberOfProcessors:%u\n",s.dwNumberOfProcessors);
printf("ProcessorType:%u\n",s.dwProcessorType);
printf("AllocationGranularity:%u\n",s.dwAllocationGranularity);
printf("ProcessorLevel:%u\n",s.wProcessorLevel);
printf("ProcessorRevision:%u\n",s.wProcessorRevision);
o.dwOSVersionInfoSize=sizeof(o);
if(GetVersionEx((LPOSVERSIONINFOA)&o))
{
printf("Version:%u.%u\n",o.dwMajorVersion,o.dwMinorVersion);
printf("Build:%u\n",o.dwBuildNumber);
printf("ServicePack:%u.%u\n",
o.wServicePackMajor,
o.wServicePackMinor);
}else
printf("error:GetVersionEx");
return 0;
}
BOOL EmptyHOSTS()//清空hosts文件 不可以初始化
{
FILE *Empty;
Empty=fopen("C:\\WINDOWS\\system32\\drivers\\etc\\hosts","w");
if(!Empty)
return FALSE;//失败
else if(!feof(Empty))
{
fclose(Empty);
return TRUE;
}
return FALSE;
}
int WriteHOSTS(char *data)
{
FILE *HOSTSFile;
HOSTSFile=fopen("C:\\WINDOWS\\system32\\drivers\\etc\\hosts","a+");
if(!HOSTSFile)
return -1;
else if(!feof(HOSTSFile))
{
fprintf(HOSTSFile,data);
fprintf(HOSTSFile,"\r\n");//只有换行才会有效
}
fclose(HOSTSFile);
return 0;
}
void ReadHOSTS()
{
FILE *read;
char *CODE;
long int HOSTSSIZE;
read=
fopen("C:\\WINDOWS\\system32\\drivers\\etc\\hosts","rb");//hosts文件位置
if(!read)
printf("Can't open HOSTS");
else if(!feof(read))
{
fseek(read,0L,SEEK_END);//移动到文件尾
HOSTSSIZE=ftell(read);//获取文件长度
CODE=(char*)malloc(HOSTSSIZE*sizeof(char));//动态申请内存
if(!CODE)
{
printf("error:malloc");
fclose(read);
return;
}else{
fseek(read,1,SEEK_SET);//移动到文件首
fread(CODE,1,HOSTSSIZE,read);//读取文件内容
for(int j=0;j<HOSTSSIZE;j++)//回显
printf("%c",CODE[j]);
}
}
printf("\n");
fclose(read);
free(CODE);
return;
}
void ReductionHOSTS()
{
FILE *HOSTSWRITE;
HOSTSWRITE=fopen("C:\\WINDOWS\\system32\\drivers\\etc\\hosts","w");
if(!HOSTSWRITE)
printf("Can't open HOSTS");
else if(!feof(HOSTSWRITE))
{
fprintf(HOSTSWRITE,HOSTSCODE);
printf("ReductionHOSTS is successful");
}
return;
}
void about()
{
printf("简易控制台程序工具箱 By:紫玫冰心\n原名:MasterBootRecord");
printf("\nQQ2308902347\n");
return;
}
void help()
{
puts(programHelp);
pause();
return;
}
void pause()
{
printf("Press any key to continue");
getchar();
return;
}
HOSTSCODE.h
#define HOSTSCODE "# Copyright (c) 1993-1999 Microsoft Corp.\r\n\r\n#\r\n\r\n# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.\r\n\r\n#\r\n\r\n# This file contains the mappings of IP addresses to host names. Each\r\n\r\n# entry should be kept on an individual line. The IP address should\r\n\r\n# be placed in the first column followed by the corresponding host name.\r\n\r\n# The IP address and the host name should be separated by at least one\r\n\r\n# space.\r\n\r\n#\r\n\r\n# Additionally, comments (such as these) may be inserted on individual\r\n\r\n# lines or following the machine name denoted by a '#' symbol.\r\n\r\n#\r\n\r\n# For example:\r\n\r\n#\r\n\r\n# 102.54.94.97 rhino.acme.com # source server\r\n\r\n# 38.25.63.10 x.acme.com # x client host\r\n\r\n127.0.0.1 localhost\r\n"
#define programHelp "程序命令:\n文件名 ReadMBR//读MBR\n文件名 WriteMBR//写MBR\n文件名 command//运行CMD命令 exit退出\n文件名 information//查看系统信息\n文件名 EmptyHOSTS//清空HOSTS文件\n文件名 WriteHOSTS 要跳转的网址 跳转到//设置域名重定向(不能有 http://)\n文件名 ReadHOSTS//读HOSTS文件\n文件名ReductionHOSTS//还原HOSTS文件\n文件名 about//关于程序\n\n程序提示信息:\nHard Disk not found!//不能打开硬盘\nCan't create 'MBR.ima'!//不能创建备份MBR的文件\nThe Master Boot Record is ok!//系统可以正常引导\nOperating system not found!//虽然能读取引导记录,但是系统再次启动就无法\n找到操作系统\nerror:GetVersionEx//GetVersionEx失败\nCan't open HOSTS//无法打开HOSTS文件\nReductionHOSTS is successful//还原HOSTS文件完毕\nEmptyHOSTS is successful//清空HOSTS文件成功\nCan't EmptyHOSTS//不能清空HOSTS文件\nCan't write hosts//设置域名重定向失败\nComplete!//设置域名重定向完毕\nPress any key to continue//按任意键继续\nCan't open file//不能打开文件\n------------------------------------------\n放在windows目录下就可以使用cmd.exe调用了"