C++ Program to Read and display the Boot Sector of a USB Pendrive

This program reads the 512 bytes of the boot sector of a USB pendrive and displays them on the console window. The program has been tested on Windows 7, 8 and 10. It is based on the Win32 API CreateFile and Read. The program MUST be run with administrative access. Otherwise it fails.

Categories | About |     |  

Parveen,
  1. The program must be run with administrative access. Either run visual studio as admin. OR run the compiled exe as admin.
  2. For safety reasons, the pendrive must be less than 32GB. But you can change this limit as per need.
  3. The program is not restricted to a USB drive. It can work even with an old style floppy, CD and hard disks without any modification.
  4. User is asked to enter a drive letter.
  5. Then the program opens a handle to the USB volume and reads the drive number and the byte size of the USB. If the byte size exceeds 32GB the program exits.
  6. Next it opens a handle to the USB drive and reads the first 512 bytes and displays in a 32x16 matrix. Non-ASCII characters are printed as a dot.
IMPORTANT NOTE: ONLY FOR WINDOWS. NEEDS A COMPILER WITH WINDOWS SET UP. COMPILE USING Visual Studio C++, Visual C++ Express, any edition, any version

The Source Code

The code can be copy-pasted to any .cpp file and compiled with a C++ compiler.

You will most likely be interested in this article also: C++ Program to write to the boot sector of a USB Pendrive

#include <cstdio>
#include <windows.h>
using namespace std;

// COMPILE WITH A WINDOWS COMPILER 
// SUCH AS VISUAL STUDIO C++ 2005 
// OR LATER. PROGRAM TESTED ON 
// WINDOWS 10, 8 and 7 
int main()
{

  // for safety we restrict to 
  // USB less than 32 GB so that 
  // accidental access to hdd 
  // can be avoided 
  const int MAX_USB_GB = 32;

  // get the drive letter 
  // from the user 
  printf("Drive letter of USB?: ");

  // scanf used for readability 
  // and simplicity only. 
  char path[] = "\\\\.\\?:";

  scanf_s("%c", path + 4, 1);

  // open the volume 
  HANDLE hVol = CreateFileA(
  path,
  GENERIC_READ,
  FILE_SHARE_READ,
  NULL,
  OPEN_EXISTING, 0,
  NULL);

  // if handle fails 
  if (INVALID_HANDLE_VALUE == hVol)
  {

    printf("Check drive letter.");

    return -1;

  }

  // get the physicaldrive number 
  // and the capacity of the disk 
  VOLUME_DISK_EXTENTS vde = { 0 };

  DWORD dw;

  DeviceIoControl(
  hVol,
  IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
  NULL,
  0,
  (LPVOID)&vde,
  (DWORD)sizeof(VOLUME_DISK_EXTENTS),
  &dw, NULL
  );

  // check disk size 
  if (
  vde.Extents[0].ExtentLength.QuadPart
  >
  LONGLONG(MAX_USB_GB * 1000000000LL)
  )
  {

    printf("USB too large\n");

    printf("Use < %d GB", MAX_USB_GB);

    CloseHandle(hVol);

    return -2;

  }

  // open the disk now by using 
  // the disk number 
  // path format is \\.\PhysicalDriveN 
  // so a buffer of 20 sufficient 
  char diskPath[20] = { 0 };

  // use sprintf to construct 
  // the path by appending 
  // disk number to \\.\PhysicalDrive 
  sprintf_s(diskPath,
  sizeof(diskPath),
  "\\\\.\\PhysicalDrive%d",
  vde.Extents[0].DiskNumber
  );

  // open a handle to the disk 
  HANDLE hUsb = CreateFileA(
  diskPath,
  GENERIC_READ,
  FILE_SHARE_READ,
  NULL,
  OPEN_EXISTING, 0,
  NULL);

  // if handle fails 
  if (INVALID_HANDLE_VALUE == hUsb)
  {

    printf("Run as admn.");

    CloseHandle(hVol);

    return -3;

  }

  // buffer to store the boot sector 
  // only 512 bytes to be read 
  BYTE sector[512];

  dw = 0;

  // file pointer begins at 0 
  // boot sector also starts at 0 
  // so read now... 
  ReadFile(hUsb,
  sector,
  sizeof(sector),
  &dw, NULL);

  printf("Boot sector: \n");

  for (int i = 0; i < 512; i++)
  {

    // create a row after every 16 
    // columns so that display 
    // looks good 
    if (0 == i % 16)
    {

      printf("\n");

    }

    BYTE b = sector[i];

    printf("%c ", isascii(b) ? b : '.');

  }

  // release handles 
  CloseHandle(hVol);

  CloseHandle(hUsb);

  return 0;

}

The Output Display

Following is the display of the boot sector of a USB pendrive.

Max.USB size is 32GB.Drive letter ? : h
Boot sector :
  .X.M S D  S 5 . 0    . 
           . ? .
  c .   . < 
     
  .   ) H . 2 0 N O   N A M E
  F A T 3 2       3 . . . . .
  { . . . . . | .V @.N .V

    @.A ..U .  r  ..U.u
    . .  t .F  . - .V @ . .
     s  . . . ..f  . . @ f  .
    . . . ? . . . . . .  A f  . .
    f ..f.F . . ~   u 9 . ~*
    w 3 f.F  f . .  .   . . 
    ., . .  . . } . . | . . .
    ..t  < .t    .  .    .  .
    . . . } . . . } . . . . .  . 
    f ` . ~   .f j   f P 
    S f h    .B.V @ . . . 
    f X f X f X f X . 3 f; F.r 
    . .* f 3.f .N  f . . . .
    ..f ..f . . .v  . ..V
    @ . . . . 
    . .   .  f a 
    .t . . .    f @ I u ..B O O
    T M G R
    D i
    k   e r r o r .
    P r e s s
    a n y   k e y   t o   r e s t
    r t
    .  .      U .

This Blog Post/Article "C++ Program to Read and display the Boot Sector of a USB Pendrive" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Comments and Discussion