#include <windows.h>
#include <stdio.h>
const UINT uSectorSize = 512 ;
const UINT uBegSector = 0 ;
const UINT uSectorNum = 1 ;
const UINT uReadSize = uSectorSize * uSectorNum ;
BYTE bBuffer[uReadSize] = {0} ;
const char pDiskPath[] = "////.//PHYSICALDRIVE0" ;
void ShowByteInform ( PBYTE pBuf, UINT uSize )
{
for ( UINT i = 1 ; i <= uSize; i++ )
{
printf ( " %02X", pBuf[i-1] ) ;
if ( i % 16 == 0 )
printf ( "/n" ) ;
else if ( i % 8 == 0 )
printf ( " " ) ;
}
}
int main()
{
HANDLE hDisk ;
__try {
hDisk = CreateFile ( pDiskPath, GENERIC_READ, /
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 ) ;
if ( hDisk == INVALID_HANDLE_VALUE )
{
MessageBox ( 0, "磁盘打开错误!", 0, 0 ) ;
return 0;
}
SetFilePointer ( hDisk, uBegSector * uSectorSize, 0, FILE_BEGIN ) ;
DWORD dwReadByte ;
ReadFile ( hDisk, (LPVOID)bBuffer, uReadSize, &dwReadByte, NULL ) ;
if ( dwReadByte == 0 )
{
MessageBox ( 0, "磁盘读取错误!", 0, 0 ) ;
return 0;
}
ShowByteInform ( bBuffer, uReadSize ) ;
}
__finally {
CloseHandle ( hDisk ) ;
}
return 0;
}
本文展示了一个使用C语言编写的简单程序,该程序通过Windows API读取物理硬盘的特定扇区,并将读取到的数据以十六进制形式打印出来。程序首先尝试打开指定的物理硬盘,设置文件指针到起始扇区位置,然后读取指定数量的扇区数据。
756

被折叠的 条评论
为什么被折叠?



