tags:

views:

93

answers:

1

I am using C# and pinvoke to read/write a raw sd card. I need to get the total capacity with deviceiocontrol:

[StructLayout(LayoutKind.Sequential)]
public struct DISK_GEOMETRY
{
public long Cylinders;
public int MediaType;
public int TracksPerCylinder;
public int SectorsPerTrack;
public int BytesPerSector;
public long DiskSize
{
get
{
return Cylinders * (long)TracksPerCylinder * (long)SectorsPerTrack * (long)BytesPerSector;
}
}
}

...

uint dummy;
DISK_GEOMETRY diskGeo = new DISK_GEOMETRY();
DeviceIoControl(safeHandle, EIOControlCode.DiskGetDriveGeometry, IntPtr.Zero, 0,
                    ref diskGeo, (uint)Marshal.SizeOf(typeof(DISK_GEOMETRY)), out dummy, IntPtr.Zero);                
this.sectorSize = diskGeo.BytesPerSector;
this.bufferSize = this.sectorSize * 16384;
this.diskSize = diskGeo.DiskSize;

With a sd card of 1 GiB, the diskGeo.DiskSize is 1011709440. Why?

+1  A: 

That's approx. 965MB which is the actual size of the useable partition. The lost 59MB is an overhead partition which contains things like the SD Copyright Protection Mechanism. Also space may be lost to the file allocation table etc. depending on the file format (FAT16, FAT32, etc.)

AdamRalph
Thanks a lot, this is the answer I was looking for. I need to erase the info in the partition using pinvoke. A last note: With hexplorer I can read/write 1073741824 bytes, the whole 1 GiB.
ric