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

 

简单地读出第一块硬盘第一扇区数据并显示出来,你也可以使用WinHex软件查看读出的数据是否正确。
/*********************************************************
* FileName:DiskInfo.cpp
* Author  :intret
* Data    :2009-5-21 12:33 PM
*
*********************************************************/

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#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)and DPT()and Signature is:"<<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;
}

        代码在vs2005上编译通过。

        其实可以很容易看出来,“一切皆文件”的概念,把硬件也当作文件。

-------------------------------------------------------------------------------------------------------------------------

        “The CreateFile function creates or opens a file, file stream, directory, physical disk, volume, console buffer, tape drive, communications resource, mailslot, or named pipe. The function returns a handle that can be used to access an object.”

        MSDN上的具体说明:

Physical Disks and Volumes

 

You can use the CreateFile function to open a physical disk drive or a volume. The function returns a handle that can be used with the DeviceIoControl function. This enables you to access the disk partition table. However, it is potentially dangerous to do so, because an incorrect write to a disk could make its contents inaccessible. The following requirements must be met for such a call to succeed:

 

  • The caller must have administrative privileges. For more information, see Running with Special Privileges.
  • The dwCreationDisposition parameter must have the OPEN_EXISTING flag.
  • When opening a volume or floppy disk, the dwShareMode parameter must have the FILE_SHARE_WRITE flag.

When opening a physical drive x, the lpFileName string should be the following form: \\.\PHYSICALDRIVE<x>. Hard disk numbers start at 0 (zero). The following table shows some examples of physical drive strings.

String Meaning
\\.\PHYSICALDRIVE0 Opens the first physical drive.
\\.\PHYSICALDRIVE2 Opens the third physical drive.

For an example of opening a physical drive, see Calling DeviceIoControl.

When opening a volume or floppy drive, the lpFileName string should be the following form: \\.\<x>:. Do not use a trailing backslash, which indicates the root directory of a drive. The following table shows some examples of drive strings.

String Meaning
\\.\A: Opens drive A (floppy drive).
\\.\C: Opens drive C (volume).

You can also open a volume by referring to its volume name. For more information, see Naming a Volume.

Volume handles can be opened as noncached at the discretion of the file system, even when the noncached option is not specified in CreateFile. You should assume that all Microsoft file systems open volume handles as noncached. The restrictions on noncached I/O for files also apply to volumes.

A file system may or may not require buffer alignment even though the data is noncached. However, if the noncached option is specified when opening a volume, buffer alignment is enforced regardless of the file system on the volume. It is recommended on all file systems that you open volume handles as noncached, and follow the noncached I/O restrictions.