Skip to content

Instantly share code, notes, and snippets.

@robindegen
Created January 30, 2015 06:39
Show Gist options
  • Save robindegen/c96fcd3ab393c887889f to your computer and use it in GitHub Desktop.
Save robindegen/c96fcd3ab393c887889f to your computer and use it in GitHub Desktop.
Reading raw data from a physical drive in windows
#define _CRT_SECURE_NO_WARNINGS
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
HANDLE handle = CreateFile(
"\\\\.\\PhysicalDrive0",
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, // No security attributes
OPEN_EXISTING,
0,
NULL
);
if (handle == INVALID_HANDLE_VALUE)
return 1;
char bootsector[512];
DWORD bytes_read;
ReadFile(handle, bootsector, 512, &bytes_read, NULL);
CloseHandle(handle);
FILE *f = fopen("bootsector.bin", "wb");
fwrite(bootsector, 512, 1, f);
fclose(f);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment