技术文章 > Win2000下直接读取硬盘扇区(BCB)

Win2000下直接读取硬盘扇区(BCB)

2018-09-19 10:46

文档管理软件,文档管理系统,知识管理系统,档案管理系统的技术资料:
注:本程序只能用于NT,Win9X无法使用。
/* -------------------------------------------------------------------------- *
* *
* File Name : main.cpp *
* *
* Purpose : Reads a sector on the disk *
* *
* -------------------------------------------------------------------------- */
#include
#include
#include
#include
#include
DWORD min(DWORD i,DWORD j)
{
return i>j?j:i;
}

VOID
__cdecl _tmain(
INT Argc,
PTCHAR Argv[]
)
{
TCHAR szName[MAX_PATH] = { 0 };
HANDLE hDisk;
//
// Validate the parameters
//
if (Argc != 3) {
_tprintf(_T("Reads a sector on the disk\n\n"));
_tprintf(_T("%s [disk number] [sector]\n"), Argv[0]);
return;
}
_sntprintf(szName, sizeof(szName) / sizeof(szName[0]) - 1, _T("\\\\.\\Physicaldrive%d"), _ttoi(Argv[1]));
//
// Open a handle to the disk
//
hDisk = CreateFile(szName,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
NULL,
0);
if (hDisk != INVALID_HANDLE_VALUE) {
DISK_GEOMETRY diskGeometry;
DWORD dwBytes;
//
// Obtain the layout of this disk
//
if (DeviceIoControl(hDisk,
IOCTL_DISK_GET_DRIVE_GEOMETRY,
NULL,
0,
&diskGeometry,
sizeof(DISK_GEOMETRY),
&dwBytes,
NULL)) {
DWORD dwSize = diskGeometry.BytesPerSector;
PVOID lpBuffer = new BYTE [dwSize];
if (lpBuffer) {
PARTITION_INFORMATION partitionInfo;
//
// Obtain the size of this disk
//
if (DeviceIoControl(hDisk,
IOCTL_DISK_GET_PARTITION_INFO,
NULL,
0,
&partitionInfo,
sizeof(PARTITION_INFORMATION),
&dwBytes,
NULL)) {
LONGLONG sectorCount = partitionInfo.PartitionLength.QuadPart / diskGeometry.BytesPerSector;
LONGLONG nIndex = _ttoi64(Argv[2]);
_tprintf(_T("Disk %d has 0x%I64x sectors with 0x%x bytes in every sector\n"), _ttoi(Argv[1]), sectorCount, diskGeometry.BytesPerSector);
//
// Read in the requested sector
//
if (nIndex < sectorCount) {
LARGE_INTEGER offset;
offset.QuadPart = (nIndex) * diskGeometry.BytesPerSector;
SetFilePointer(hDisk, offset.LowPart, &offset.HighPart, FILE_BEGIN);
if (ReadFile(hDisk, lpBuffer, dwSize, &dwBytes, NULL)) {
//
// The dwBytes field holds the number of bytes that were actually read [ <= dwSize ]
//
for (ULONG nOffset = 0; nOffset < dwBytes; nOffset += 0x10) {
ULONG nBytes, nIdx;
//
// Display the address
//
_tprintf(_T("%011I64x "), (offset.QuadPart) + nOffset);
//
// Display the data in hexadecimal
//
nBytes = min(0x10, dwBytes - nOffset);
for (nIdx = 0; nIdx < nBytes; nIdx++) {
_tprintf(_T("%02x %s"), ((PUCHAR)lpBuffer)[nOffset + nIdx], ((nIdx + 1) % 0x8) ? _T("") : _T(" "));
}
for ( ; nIdx < 0x10; nIdx++) {
_tprintf(_T(" %s"), ((nIdx + 1) % 0x8) ? _T("") : _T(" "));
}
//
// Display the data in ascii
//
for (nIdx = 0; nIdx < nBytes; nIdx++) {
_tprintf(_T("%c"), isprint(((PUCHAR)lpBuffer)[nOffset + nIdx]) ? ((PUCHAR)lpBuffer)[nOffset + nIdx] : _T(“.“));
}
_tprintf(_T("\n"));
}
} else {
_tprintf(_T("ReadFile() on sector 0x%I64x failed with error code: %d\n"), nIndex, GetLastError());
}
} else {
_tprintf(_T("The requested sector is out-of-bounds\n"));
}
} else {
_tprintf(_T("IOCTL_DISK_GET_PARTITION_INFO failed with error code %d\n"), GetLastError());
}
delete [] lpBuffer;
} else {
_tprintf(_T("Unable to allocate resources, exiting\n"));
}
} else {
_tprintf(_T("IOCTL_DISK_GET_DRIVE_GEOMETRY failed with error code %d\n"), GetLastError());
}
CloseHandle(hDisk);
} else {
_tprintf(_T("CreateFile() on %s failed with error code %d\n"), szName, GetLastError());
}
_tprintf(_T("\n"));
return;
}