How to Reclaim “Lost Space” After Repartitioning USB Flash Drive

I have a 64MB USB flash drive and was experimenting putting different file systems on it. But when I removed the partition and re-partitioned and formatted using FAT32, I only got 52MB instead of the original 64MB. And even when I changed it back to FAT (the default), the capacity remained at 52MB. So it seems that I had lost about 8MB. A quick Google search revealed a few similar encounters by other users. And it seems that the only solution is to use the manufacture offered utility to recreate the partition on the drive. Well, mine is an old JMTek one, and the company’s website does not offer such a utility. So I decided to find an alternative solution myself. A flash drive is just another disk drive to the system and I believe that all I need to do is to wipe out the partition table and then try repartition again (so that the system will treat the flash drive as a “raw” disk instead of relying on the information that is recorded in the partition table). Back in the good old DOS days, this procedure is quite simple and all you really need to do is just a few lines of assembly code under the DOS prompt:

C:\>debug -f 1000 1200 0 -a 0B15:0100 mov ax, 0301 0B15:0103 mov bx, 1000 0B15:0106 mov cx, 0001 0B15:0109 mov dx, 0082 ‘your drive number 0B15:010C int 13 0B15:010E int 3 0B15:010F -g=100

Too bad that in modern OS’s (e.g. Windows XP, Windows 2003 and Windows 2000), direct disk access is not allowed via DOS interrupts. So I had to write a bit more code to do this. The following code I am going to show you was written under Visual C++ 2005, but the code should work for older Visual C++ as well since I am only using the Win32 system calls. To clear the partition table here’s all the code that you need (for simplicity, I omitted some error handling code):<

//Open the physical drive. Note replace the PhysicalDrive3 //with the actual drive number the flash device is assigned


//Move to the partition table which is at the begining

SetFilePointer(hFile, 0, NULL, FILE_BEGIN);

char buffer[512] = {‘0’}; DWORD bytewritten; WriteFile(hFile, buffer, 512, &bytewritten, NULL);

For this code to work, you will need to include the following:

#include <windows.h>

#include <stdio.h>

#include <windows.h>

#include <winioctl.h>

I tried this code on my flash drive, and it worked! After running the code, the system reports that I have a USB drive with a raw file system (under disk management tool). So I repartitioned it and this time the correct disk size was recognized. After reformatting using FAT32, I got a flash drive with all 64MB.

A warning for you if you intend to try the code provided here: please make sure you understand what you are doing, because you can easily wipe out a hard drive if you supplied the wrong physical drive number.

As a precaution, you should run the following code to see ensure that you are on the correct drive (making sure that the disk drive size is correct). Alternatively, you can always figure out the physical drive number from the disk management MMC snap-in.


BOOL bResult;

DWORD junk;

bResult = DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, pdg, sizeof(*pdg), &junk, (LPOVERLAPPED) NULL);

LONGLONG mb = pdg->Cylinders.QuadPart * pdg->TracksPerCylinder * pdg->SectorsPerTrack * pdg->BytesPerSector / 1024 / 1024;


bResult = DeviceIoControl(hFile, IOCTL_DISK_GET_PARTITION_INFO, NULL, 0, pPInfo, sizeof(*pPInfo), &junk, (LPOVERLAPPED) NULL);

Be Sociable, Share!

One Comment

  1. Abhik says:

    Is there any shortcut?
    I don’t know Visual C or Visual C++

Leave a Reply