vc++直接读写物理扇区源码,xp下编译通过!
#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;//一个扇区数据量是字节呀
ReadPhysicalSector(SectorStart, SectorCount, p);读第0扇区
WritePhysicalSector(5,1,p);写到第5扇区
}
好的.支持下 不错。。。。。。。。。 我真的想学习一下 好贴,绝对要支持下!!~~ 好帖,必须顶起! 谢谢分享! 好东西!值得收藏 毫不犹豫的收藏了。
页:
[1]