<p>In C, using the standard Windows API, what is the best way to read an unformatted disk? Specifically, I have an <a href="http://en.wikipedia.org/wiki/MultiMediaCard" rel="nofollow noreferrer">MMC</a> or <a href="http://en.wikipedia.org/wiki/Secure_Digital_card" rel="nofollow noreferrer">SD card</a> with data, but no file system (not FAT16, not FAT32, just raw data). If there was a simple way to open the entire card for byte by byte binary access, that would be great.</p>

<p>Thanks!</p>

[{'answer_id': 80533, 'author': 'Kasprzol', 'author_id': 5957, 'author_profile': 'https://Stackoverflow.com/users/5957', 'pm_score': 1, 'selected': False, 'text': '<p>You have to open the device file with <a href="http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx" rel="nofollow noreferrer">CreateFile</a> and then use <a href="http://msdn.microsoft.com/en-us/library/aa365467(VS.85).aspx" rel="nofollow noreferrer">ReadFile</a>/<a href="http://msdn.microsoft.com/en-us/library/aa365468(VS.85).aspx" rel="nofollow noreferrer">readFileEx</a>. Don\'t forget to close the file with <a href="http://msdn.microsoft.com/en-us/library/ms724211.aspx" rel="nofollow noreferrer">CloseHandle</a></p>\n'}, {'answer_id': 80542, 'author': 'szevvy', 'author_id': 11437, 'author_profile': 'https://Stackoverflow.com/users/11437', 'pm_score': 1, 'selected': False, 'text': '<p><a href="http://msdn.microsoft.com/en-us/library/aa363858.aspx" rel="nofollow noreferrer">CreateFile function reference on MSDN</a></p>\n\n<p>Scroll down to "Physical Disks and Volumes" - note the security restrictions on Vista do not apply for voulmes without a filesystem, so you\'ll be fine even on Vista under the conditions you have given.</p>\n'}, {'answer_id': 81420, 'author': 'Andreas Magnusson', 'author_id': 5811, 'author_profile': 'https://Stackoverflow.com/users/5811', 'pm_score': 3, 'selected': True, 'text': '<p>I would go with</p>\n\n<pre><code>HANDLE drive = CreateFile(_T("\\\\.\\PhysicalDrive0"), GENERIC_READ, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);\n// error handling\nDWORD br = 0;\nDISK_GEOMETRY dg;\nDeviceIOControl(drive, IOCTL_DISK_GET_DRIVE_GEOMETRY, 0, 0, &amp;dg, sizeof(dg), &amp;br, 0);\n//\nLARGE_INTEGER pos;\npos.QuadPart = static_cast&lt;LONGLONG&gt;(sectorToRead) * dg.BytesPerSector;\nSetFilePointerEx(drive, pos, 0, FILE_BEGIN);\nconst bool success = ReadFile(drive, sectorData, dg.BytesPerSector, &amp;br) &amp;&amp; br == dg.BytesPerSector;\n//\nCloseHandle(drive);\n</code></pre>\n\n<p>Please note that in order to verify that you\'ve successfully read a sector you must verify that the read byte count corresponds to the number of bytes you wanted to read, i.e. in my experience ReadFile() on a physical disk can return TRUE even when no bytes are read (or maybe I just have a buggy driver).</p>\n\n<p>The problem that remains is to determine your drive number (0 as is used in my example refers to C: which is probably not what you want). I don\'t know how to do that, but if you only have one drive connected which is not formatted, it ought to be possible by calling opening each PhysicalDrive in order and calling DeviceIOControl() with <code>IOCTL_DISK_GET_DRIVE_LAYOUT_EX</code> as a command:</p>\n\n<pre><code>DRIVE_LAYOUT_INFORMATION_EX dl;\nDeviceIOControl(drive, IOCTL_DISK_GET_DRIVE_LAYOUT_EX, 0, 0, &amp;dl, sizeof(dl), &amp;br, 0);\nif(dl.PartitionStyle == PARTITION_STYLE_RAW)\n{\n  // found correct disk\n}\n</code></pre>\n\n<p>But that\'s just a guess.</p>\n'}]
['https://Stackoverflow.com/questions/80493', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3175/']