readsector.c

来自「直接读写硬盘扇区」· C语言 代码 · 共 51 行

C
51
字号
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include <Windows.h>
#include <Winbase.h>


//以下是读取硬盘扇区函数
int ReadPhysicalSector(unsigned long SectorStart, unsigned long SectorCount, unsigned char *p)
{
    unsigned long BytesPerSector = 512;
    unsigned long nBytes;
    char Drive[] = "\\\\.\\PHYSICALDRIVE0";
    int result = 0;
    HANDLE hDeviceHandle = CreateFile(Drive,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,0);
    if(hDeviceHandle)
    {
        long pointer;
        long phigh;
        pointer = SectorStart;
        pointer = pointer*BytesPerSector;
        phigh = pointer>>32;
        SetFilePointer(hDeviceHandle,(unsigned long)pointer,&phigh,FILE_BEGIN);
        if(ReadFile(hDeviceHandle,p,SectorCount*BytesPerSector,&nBytes,NULL))
            result = 1;
        CloseHandle(hDeviceHandle);
    }
    return result;
}

//主函数调用
int main(int argc, char* argv[])
{
    unsigned long SectorStart = 0;	//比如我要读的是编号为的那个扇区开始的数据,这里写
                                    //如果读的是从第扇区开始后的数据这里就写
    unsigned long SectorCount = 1;  //读多少个扇区,这里是个
    unsigned char p[512];		    //一个扇区数据量是字节呀
	int i;
    ReadPhysicalSector(SectorStart,SectorCount,p);
	//输出的
    for(i=0;i<512;i++)			
    {
        printf("%02X ",p[i]);
        if(i%26 == 0)
		{
            printf(" ");
		}
    }
    return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?