|
|
#include <Windows.h>
| #include <cstdio>
|
| VOID PrintHex(PBYTE Data, ULONG dwBytes) {
| for (ULONG i = 0; i < dwBytes; i += 16) {
| printf("%.8x: ", i);
|
| for (ULONG j = 0; j < 16; j++) {
| if (i + j < dwBytes) {
| printf("%.2x ", Data[i + j]);
| }
| else {
| printf("?? ");
| }
| }
|
| for (ULONG j = 0; j < 16; j++) {
| if (i + j < dwBytes && Data[i + j] >= 0x20 && Data[i + j] <= 0x7e) {
| printf("%c", Data[i + j]);
| }
| else {
| printf(".");
| }
| }
|
| printf("\n");
| }
| }
|
| int main() {
| // Open the disk device.
| HANDLE hDisk = CreateFile(L"\\\\.\\C:",
| 0,
| 0,
| NULL,
| OPEN_EXISTING,
| FILE_ATTRIBUTE_NORMAL,
| NULL);
| if (hDisk == INVALID_HANDLE_VALUE) {
| printf("CreateFile failed, %d\n", GetLastError());
| return 1;
| }
|
| // Obtain the output data, assuming that it will fit into 1024 bytes.
| BYTE geometry[1024];
| DWORD BytesReturned;
| if (!DeviceIoControl(hDisk, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, NULL, 0, &geometry, sizeof(geometry), &BytesReturned, NULL)) {
| printf("DeviceIoControl failed, %d\n", GetLastError());
| CloseHandle(hDisk);
| return 1;
| }
|
| // Dump the output data on screen and free resources.
| PrintHex(geometry, BytesReturned);
| CloseHandle(hDisk);
|
| return 0;
| }
|
|