|
本帖最后由 20011010wo 于 2016-7-31 23:01 编辑
- <div class="blockcode"><blockquote>#define WIN32_LEAN_AND_MEAN
- #include <windows.h>
- #include <io.h>
- #include <sys\stat.h>
- #include <sys\types.h>
- #include <share.h>
- #include <fcntl.h>
- #include <cstdlib>
- #include <string>
- struct MBRPartitionTable
- {
- unsigned char bootsign;
- unsigned char start_chsaddress[3];
- unsigned char type;
- unsigned char end_chsaddress[3];
- unsigned char used_sectorsnum[4];
- unsigned char total_sectorsnum[4];
- };
- struct MBR
- {
- unsigned char bootstrapcode[446];
- MBRPartitionTable a;
- MBRPartitionTable b;
- MBRPartitionTable c;
- MBRPartitionTable d;
- unsigned char endsign[2];
- };
- // 物理驱动器对象
- class PhysicalDrive
- {
- public:
- PhysicalDrive(void) : drive(L"\\\\.\\PhysicalDrive"), drive_index(0){} //构造函数
- std::wstring GetDriveName(void) //获取存储设备名称
- {
- wchar_t drive_index_buffer[8];
- _itow_s(drive_index, drive_index_buffer, 8, 10); //int 转 wcahr_t
- return drive + drive_index_buffer;
- }
- private:
- std::wstring drive; //存储设备部分名称
- int drive_index; //存储设备号
- };
- class Libmbr {
- public:
- Libmbr(void) = delete; //不允许合成构造函数
- Libmbr(PhysicalDrive); //构造函数
- bool ReadMBR(); //读MBR函数
- bool WriteMBR(); //写MBR函数
- MBR code = {0}; //将要写入的数据
- private:
- bool ReadWriteMBR(bool); //内部实现函数
- PhysicalDrive m_drive; //存储设备对象
- int iFileHandler; //文件句柄
- };
复制代码- #include "libmbr.h"
- inline Libmbr::Libmbr(PhysicalDrive drive) :m_drive(drive) {} //构造函数
- inline bool Libmbr::ReadMBR()
- {
- return ReadWriteMBR(true);
- }
- inline bool Libmbr::WriteMBR()
- {
- return ReadWriteMBR(false);
- }
- bool Libmbr::ReadWriteMBR(bool isRead)
- {
- //打开设备
- if (_wsopen_s(&iFileHandler, m_drive.GetDriveName().c_str(), _O_BINARY | _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE))
- return false;
- //判断是否至EOF
- if (_eof(iFileHandler))
- return false;
- //移动至起始点
- if (_lseek(iFileHandler, 0, SEEK_SET) == -1)
- return false;
- //读写属性
- if (isRead)
- {
- if (_read(iFileHandler, &code, 512) == -1) //读512个字
- return false;
- }
- else
- {
- if (_write(iFileHandler, &code, 512) == -1) //写512个字
- return false;
- }
- //刷新设备缓冲
- if (_commit(iFileHandler) == -1)
- return false;
- //关闭句柄
- if (_close(iFileHandler) == -1)
- return false;
- return true;
- }
复制代码
|
评分
-
1
查看全部评分
-
|