|
#include <stdio.h>
#include <string.h>
#include <iostream.h>
#include <windows.h>
BOOL ReadPhysicalSector(unsigned long SectorStart, unsigned long SectorCount, unsigned char *p)
{
unsigned long BytesPerSector = 512;
unsigned long nBytes;
char Drive[] = "\\\\.\\PhysicalDrive0";//注意物理磁盘的大小写,这样才是正确的
BOOL result = FALSE;
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 = TRUE;
CloseHandle(hDeviceHandle);
}
return result;
}
BOOL WritePhysicalSector(unsigned long SectorStart, unsigned long SectorCount, unsigned char *p)
{
unsigned long BytesPerSector = 512;
unsigned long nBytes;
char Drive[] = "\\\\.\\PhysicalDrive0";//注意物理磁盘的大小写,这样才是正确的
BOOL result = FALSE;
HANDLE hDeviceHandle = CreateFile(Drive,GENERIC_WRITE,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(WriteFile(hDeviceHandle,p,SectorCount*BytesPerSector,&nBytes,NULL))
result = TRUE;
CloseHandle(hDeviceHandle);
}
return result;
}
//调用就这样
int main(int argc, char* argv[])
{
unsigned long SectorStart = 0;//比如我要读的是编号为的那个扇区开始的数据,这里写
//如果读的是从第扇区开始后的数据这里就写
unsigned long SectorCount = 1;//读多少个扇区,这里是个
unsigned char p[512];//一个扇区数据量是字节呀
ReadPhysicalSector(SectorStart, SectorCount, p);读第0扇区
WritePhysicalSector(5,1,p);写到第5扇区
}
|
-
本帖评分记录 | 金子 |
收起
理由
|
八喜
| + 10 |
神马都是浮云 |
|