萝卜头IT论坛

搜索
查看: 2621|回复: 3
收起左侧

简单MBR读取库的实现

[复制链接]
发表于 2016-7-31 22:27:19 | 显示全部楼层 |阅读模式
本帖最后由 20011010wo 于 2016-7-31 23:01 编辑
  1. <div class="blockcode"><blockquote>#define WIN32_LEAN_AND_MEAN            

  2. #include <windows.h>
  3. #include <io.h>
  4. #include <sys\stat.h>
  5. #include <sys\types.h>
  6. #include <share.h>
  7. #include <fcntl.h>
  8. #include <cstdlib>

  9. #include <string>
  10. struct MBRPartitionTable
  11. {
  12.         unsigned char bootsign;
  13.         unsigned char start_chsaddress[3];
  14.         unsigned char type;
  15.         unsigned char end_chsaddress[3];
  16.         unsigned char used_sectorsnum[4];
  17.         unsigned char total_sectorsnum[4];
  18. };
  19. struct MBR
  20. {
  21.         unsigned char bootstrapcode[446];
  22.         MBRPartitionTable a;
  23.         MBRPartitionTable b;
  24.         MBRPartitionTable c;
  25.         MBRPartitionTable d;
  26.         unsigned char endsign[2];
  27. };
  28. // 物理驱动器对象
  29. class PhysicalDrive
  30. {
  31. public:
  32.         PhysicalDrive(void) : drive(L"\\\\.\\PhysicalDrive"), drive_index(0){}        //构造函数
  33.         std::wstring GetDriveName(void)                                                                                        //获取存储设备名称  
  34.         {
  35.                 wchar_t drive_index_buffer[8];
  36.                 _itow_s(drive_index, drive_index_buffer, 8, 10);                                        //int 转 wcahr_t
  37.                 return drive + drive_index_buffer;
  38.         }
  39. private:
  40.         std::wstring drive;                                                     //存储设备部分名称
  41.         int drive_index;                                                                              //存储设备号
  42. };

  43. class Libmbr {
  44. public:
  45.         Libmbr(void) = delete;                                                          //不允许合成构造函数
  46.         Libmbr(PhysicalDrive);                                                                        //构造函数
  47.         bool ReadMBR();                                                                                       //读MBR函数
  48.         bool WriteMBR();                                                                                    //写MBR函数
  49.         MBR code = {0};                                                              //将要写入的数据
  50. private:
  51.         bool ReadWriteMBR(bool);                                                     //内部实现函数
  52.         PhysicalDrive m_drive;                                                          //存储设备对象
  53.         int iFileHandler;                                                              //文件句柄
  54. };
复制代码
  1. #include "libmbr.h"



  2. inline Libmbr::Libmbr(PhysicalDrive drive) :m_drive(drive) {}        //构造函数

  3. inline bool Libmbr::ReadMBR()
  4. {
  5.         return ReadWriteMBR(true);
  6. }

  7. inline bool Libmbr::WriteMBR()
  8. {
  9.         return ReadWriteMBR(false);
  10. }


  11. bool Libmbr::ReadWriteMBR(bool isRead)
  12. {
  13.         //打开设备
  14.         if (_wsopen_s(&iFileHandler, m_drive.GetDriveName().c_str(), _O_BINARY | _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE))
  15.                 return false;
  16.         //判断是否至EOF
  17.         if (_eof(iFileHandler))
  18.                 return false;
  19.         //移动至起始点
  20.         if (_lseek(iFileHandler, 0, SEEK_SET) == -1)
  21.                 return false;
  22.         //读写属性
  23.         if (isRead)
  24.         {
  25.                 if (_read(iFileHandler, &code, 512) == -1)        //读512个字
  26.                         return false;
  27.         }
  28.         else
  29.         {
  30.                 if (_write(iFileHandler, &code, 512) == -1)        //写512个字
  31.                         return false;
  32.         }
  33.         //刷新设备缓冲
  34.         if (_commit(iFileHandler) == -1)
  35.                 return false;
  36.         //关闭句柄
  37.         if (_close(iFileHandler) == -1)
  38.                 return false;
  39.         return true;
  40. }
复制代码


评分

1

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2016-7-31 22:32:35 | 显示全部楼层
补充:还有数据结构还没加入
回复

使用道具 举报

 楼主| 发表于 2016-7-31 23:02:10 | 显示全部楼层
加入了
回复

使用道具 举报

发表于 2016-7-31 23:12:54 | 显示全部楼层
赞~
回复

使用道具 举报

联系我们(Contact)|手机版|萝卜头IT论坛 ( 苏ICP备15050961号-1 )

GMT+8, 2024-3-29 14:58 , Processed in 0.100428 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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