读取硬盘的第一扇区数据(MBR+DPT+Signature)

使用WIN API函数CreateFile和ReadeFile来读取主引导扇区、磁盘分区表、标记等信息,并以hex形式打印。这就是“一切都皆文件”的概念,包括硬盘这些设备,这样使得读取变得非常地容易了。

打印结果如下图:


 

 

image


 

 

多年后得第二张图:

 

 


 

 

源码如下:


 

 

/*********************************************************
* FileName: DiskInfo.cpp
* Author  : intret
* Data    : 2009-5-21 12:33 PM
*********************************************************/
 
#include <iostream>
#include <iomanip>
#include <tchar.h>
#include <Windows.h>
 
using namespace std;
 
int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE hDevice = CreateFile(_T(".//PHYSICALDRIVE0"), GENERIC_READ|GENERIC_WRITE,
        FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,OPEN_EXISTING,0,NULL);
 
    if ( hDevice != INVALID_HANDLE_VALUE)
    {
        int count=0;
        unsigned char buffer[512];      //主引导扇区数据(446B+ 64B+ 2B = MBR+DPT+Signature)
        DWORD NumberOfBytesRead;
 
        //读取数据到缓冲区
        BOOL ret = ReadFile(hDevice, buffer, sizeof(buffer), &NumberOfBytesRead, NULL);
        if (ret)
        {
            cout<<"The data of MBR(416), DPT(64) and Signature(2) is:"<<endl<<endl;   
            cout<<setiosflags(ios::uppercase);      //大写显示
 
            for (int i=0; i<512; i++)       //前446bytes是MBR
            {
 
                cout<<hex<<setw(2)<<setfill('0')<<buffer[i]+0;
                if (((i+1)%16) )
                    cout<<ends;
                else
                    cout<<endl;
                if (!((i+1)%256))
                    cout<<endl;
            }
            cout<<endl;
        }
        else
        {
            cout<<"Failed to read data of xx"<<endl;
        }
        CloseHandle(hDevice);
    }
    else
    {
        cout<<"打开物理驱动器失败!"<<endl;
    }
    return 0;
}

 


 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值