I just want to write a program to analyze MBR in C.
I've known some ways to read it through APIs on Windows or commands on Linux.
But, can I do it in C without any platform-dependent limitation?
If I can't, is there any reason?
Reading the master boot record is platform-dependent, the following code for instance works on Windows (if you have the privileges to do it)
#include <windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;
short ReadSect
(const char *_dsk, // disk to access
char *&_buff, // buffer where sector will be stored
unsigned int _nsect // sector number, starting with 0
)
{
DWORD dwRead;
HANDLE hDisk=CreateFile(_dsk,GENERIC_READ,FILE_SHARE_VALID_FLAGS,0,OPEN_EXISTING,0,0);
if(hDisk==INVALID_HANDLE_VALUE) // this may happen if another program is already reading from disk
{
CloseHandle(hDisk);
return 1;
}
SetFilePointer(hDisk,_nsect*512,0,FILE_BEGIN); // which sector to read
ReadFile(hDisk,_buff,512,&dwRead,0); // read sector
CloseHandle(hDisk);
return 0;
}
int main()
{
char * drv="\\\\.\\C:";
char *dsk="\\\\.\\PhysicalDrive0";
int sector=0;
char *buff=new char[512];
ReadSect(dsk,buff,sector);
if((unsigned char)buff[510]==0x55 && (unsigned char)buff[511]==0xaa) cout <<"Disk is bootable!"<<endl;
getchar();
}
http://www.cplusplus.com/forum/windows/18019/
On Linux you can even use a terminal command
sudo dd if=/dev/sda ibs=512 count=1 | hexdump -C
Best solution would be to #ifdef
your code and render it platform-dependent.