鱼C论坛

 找回密码
 立即注册
查看: 3934|回复: 2

winAPI 读取扇区 得不到正确的数据

[复制链接]
发表于 2012-11-13 12:50:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <WINDOWS.H>
#include <STDIO.H>
/************************************************************************/
/* 功能描述: 读取硬盘扇区 并输出文件                                    */
/************************************************************************/
bool readSector(unsigned long sectorstart,unsigned long sectorcount,char *p);
int main(int argc, char* argv[]){
        unsigned long  start = 0; //从哪里开始读
        unsigned long  sectorcount = 0; //读取扇区的数量
        char  sectorsize[512];
       
        if(readSector(start,sectorcount,sectorsize)){
                //遍历
                for(int i= 0;i<512;i++){
                        printf("%02X",sectorsize[i]);
                }
        }else{
                printf("ERROR");
        }

        getchar();
                return 0;
}
bool readSector(unsigned long sectorstart,unsigned long sectorcount,char *p){

        bool resuslt =false;
        unsigned long PerSector =512; //每个扇区大小

        unsigned long bytesIndex; //指向实际读取字节数
       
        char Driver[] ="\\.\PhysicalDrive0"; //指定打开扇区位置

        HANDLE hOpen = CreateFile(Driver,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,0);

        if(hOpen){
                //long pl ; //存放低位
                //long ph ; //存放高位

                //pl = sectorstart*PerSector;
                //ph =pl>>32;
                SetFilePointer(hOpen,512,0,FILE_BEGIN); //指定读取的位置

                //读取文件
                ReadFile(hOpen,p,512,&bytesIndex,NULL);

                CloseHandle(hOpen); //关闭句柄
                resuslt =true;
                return resuslt;
        }
        return resuslt;
}
输出 的是 FFFFFFCC 就一只循环去了  不知道那个函数 里面的值写错了 求教
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2012-11-13 13:10:36 | 显示全部楼层
貌似是 CreateFile返回的是-1 导致的 但就是不知道为什么没有打开
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2012-11-13 13:50:47 | 显示全部楼层
已解决 貌似是 路径写错了  应该是 \\\\.\\PHYSICALDRIVE0
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-16 07:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表