VC硬盘读写功能封装

硬盘读写功能主要运用在三个方面,分别是MBR操作,数据恢复以及绕过Windows安全机制。以下是私藏多年的硬盘读写功能封装的代码。为了效率起见,读写最好一次性读/写512字节,也就是一磁道为佳。




#include <Windows.h>
#include <string>

const wchar_t wDisk[22] = L"\\\\.\\PhysicalDrive0";
const wchar_t wDrev[10]  = L"\\\\.\\A:";


//读盘
BOOL WINAPI Disk_ReadSectors(int iDisk, DWORD dwStart, DWORD dwStartHigh, DWORD dwOffsetSectors, DWORD dwSize, LPBYTE lpReadBuff)
{
	//整理数据
	unsigned __int64 _64OffsetBase = (dwStart + dwStartHigh*0x100000000)*512+dwOffsetSectors;
	dwStart = (DWORD)_64OffsetBase;
	dwStartHigh = (DWORD)_64OffsetBase/0x100000000;

	//初始化字符串参数
	std::wstring wBuff = ::wDisk;
	wBuff[17] = (wchar_t)(iDisk + L'0');

	//开始读盘
	HANDLE hDisk = CreateFileW(wBuff.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if(INVALID_HANDLE_VALUE == hDisk)
		return FALSE;
	SetFilePointer(hDisk, dwStart, (PLONG)&dwStartHigh, FILE_BEGIN);
	BOOL bRet = ReadFile(hDisk, lpReadBuff, dwSize, &dwOffsetSectors, NULL);
	CloseHandle(hDisk);
	return bRet;
}


//写盘
BOOL WINAPI Disk_WriteSectors(int iDisk, DWORD dwStart, DWORD dwStartHigh, DWORD dwOffsetSectors, DWORD dwSize, LPBYTE lpWriteBuff)
{
	//整理数据
	unsigned __int64 _64OffsetBase = (dwStart + dwStartHigh*0x100000000)*512+dwOffsetSectors;
	dwStart = (DWORD)_64OffsetBase;
	dwStartHigh = (DWORD)_64OffsetBase/0x100000000;

	//初始化字符串参数
	std::wstring wBuff = ::wDisk;
	wBuff[17] = (wchar_t)(iDisk + L'0');

	//开始写盘
	HANDLE hDisk = CreateFileW(wBuff.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if(INVALID_HANDLE_VALUE == hDisk)
		return FALSE;
	SetFilePointer(hDisk, dwStart, (PLONG)&dwStartHigh, FILE_BEGIN);
	BOOL bRet = WriteFile(hDisk, lpWriteBuff, dwSize, &dwOffsetSectors, NULL);
	CloseHandle(hDisk);
	return bRet;
}


//读驱动器
BOOL WINAPI Drev_ReadSectors(wchar_t wDrev, DWORD dwStart, DWORD dwStartHigh, DWORD dwOffsetSectors, DWORD dwSize, LPBYTE lpReadBuff)
{
	//整理数据
	unsigned __int64 _64OffsetBase = (dwStart + dwStartHigh*0x100000000)*512+dwOffsetSectors;
	dwStart = (DWORD)_64OffsetBase;
	dwStartHigh = (DWORD)_64OffsetBase/0x100000000;

	//初始化字符串参数
	std::wstring wBuff = ::wDrev;
	wBuff[4] = wDrev;

	//开始读盘
	HANDLE hDrev = CreateFileW(wBuff.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
	if(INVALID_HANDLE_VALUE == hDrev)
		return FALSE;
	SetFilePointer(hDrev, dwStart, (PLONG)&dwStartHigh, FILE_BEGIN);
	BOOL bRet = ReadFile(hDrev, lpReadBuff, dwSize, &dwOffsetSectors, NULL);
	CloseHandle(hDrev);
	return bRet;
}


//写驱动器
BOOL WINAPI Drev_WriteSectors(wchar_t wDrev, DWORD dwStart, DWORD dwStartHigh, DWORD dwOffsetSectors, DWORD dwSize, LPBYTE lpWriteBuff)
{
	//整理数据
	unsigned __int64 _64OffsetBase = (dwStart + dwStartHigh*0x100000000)*512+dwOffsetSectors;
	dwStart = (DWORD)_64OffsetBase;
	dwStartHigh = (DWORD)_64OffsetBase/0x100000000;

	//初始化字符串参数
	std::wstring wBuff = ::wDrev;
	wBuff[4] = wDrev;

	//开始写盘
	HANDLE hDrev = CreateFileW(wBuff.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
	if(INVALID_HANDLE_VALUE == hDrev)
		return FALSE;
	SetFilePointer(hDrev, dwStart, (PLONG)&dwStartHigh, FILE_BEGIN);
	BOOL bRet = WriteFile(hDrev, lpWriteBuff, dwSize, &dwOffsetSectors, NULL);
	CloseHandle(hDrev);
	return bRet;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值