Make your own free website on Tripod.com
dumpSector

 
 1  #include <windows.h>
 2  #include <winioctl.h>
 3  #include <stdio.h>
 4
 5  int main(int argc,char *argv[]){
 6          BOOL            result;
 7          HANDLE          rawDevice;
 8          LPCTSTR         deviceName;
 9          char            buffer[512];
10          int             driveNumber;
11          union{
12                  unsigned long long ull;
13                  unsigned long ul[2];
14          }sectorNumber;
15          int             numberOfBytesRead;
16          int             i,j;
17
18
19
20
21          /*
22           * Everything inside this do while...to have a single exit point!
23           */
24          do{
25
26
27                  /*
28                   * Allocate 256 bytes for the device name
29                   */
30                  deviceName=(LPCTSTR)malloc(256);
31
32                  driveNumber=atoi(argv[1]);
33                  sprintf(deviceName,"\\\\.\\PhysicalDrive%d",driveNumber);
34                  sectorNumber.ull=atoll(argv[2]);
35
36
37                  rawDevice = CreateFile(deviceName,  // drive to open
38                                  FILE_SHARE_READ | FILE_SHARE_WRITE, // no access to the drive
39                                  0,
40                                  NULL,             // default security attributes
41                                  OPEN_EXISTING,    // disposition
42                                  0,                // file attributes
43                                  NULL);            // do not copy file attributes
44
45
46
47                  if (rawDevice == INVALID_HANDLE_VALUE){
48                          printf("Could not open the device\n");
49                          break;
50                  }
51
52                  printf("%s has been opened successfully\n",deviceName);
53
54                  sectorNumber.ull*=512;
55                  SetFilePointer(rawDevice, sectorNumber.ul[0], &sectorNumber.ul[1], FILE_BEGIN);
56
57                  result=ReadFile(rawDevice, (LPVOID)buffer, 512, &numberOfBytesRead,(LPOVERLAPPED)NULL);
58                  if(!result){
59                          printf("Error while reading\n");
60                          break;
61                  }
62
63                  for(i=0;i<512;i++){
64                          printf("%02X ",buffer[i]&0xff);
65                          if(i&&!((i+1)%16)){
66                                  for(j=(i-15);j<=i;j++){
67                                          if(buffer[j]>=32)printf("%c",buffer[j]);
68                                          else printf(".");
69                                  }
70                                  printf("\n");
71                          }
72                  }
73
74
75          }while(0);
76
77
78          return 0;
79
80
81  }
82