How to access sectors using c++

Is it possible to read sectors of hard disk or pendrive using c++ language.
If possible please tell me how it can be done.

I'd be thankful to you

Regards

Harsh
Last edited on
I think I read about that in MSDN online when I read either the ReadFile() or WriteFile() document. Or at least it was related. I think you can read the HDD sectors by bypassing caching and using an aligned buffer. MSDN explains how this can be done. I suggest that you read the documents carefully.

If you are asking how to read HDD sectors bypassing the OS, then I have no clue, but I would imagine it would require low level access to the disk, probably requiring a device driver.
Yes I think I need a device driver but I don't have any knowledge about device drivers. Can you suggest me something related to device drivers???
I'd be thankful to you

Regards

Harsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#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();

}

I have used the about program on the flash drive. And I'm successful in getting some data.

The output comes out to be "Disk is bootable"

I've taken the output of the buff on a text file.

The output is :

ëXMSDOS5.0 P ø ? ÿ & Ÿw Ø    € )¹ÙPrNO NAME FAT32 3ɎѼô{ŽÁŽÙ½ |ˆNŠV@´A»ªUÍrûUªu
öÁtþFë-ŠV@´Ís¹ÿÿŠñf¶Æ@f¶Ñ€â?÷â†ÍÀíAf·Éf÷áf‰Føƒ~[ u8ƒ~* w2f‹FfƒÀ » €¹ è+ é, ú}´}‹ð¬„Àt]<ÿt ´» Íëî û}ëå ù}ëà˜Í[Í>f`€~ „ fj fPSfh  ´BŠV@‹ôÍfXfXfXfXë3f;Førùë*f3Òf·N<f÷ñþŠÊf‹ÐfÁê÷v
†ÖŠV@ŠèÀä
̸Ífa‚uÿÃ f@Iu”ÃBOOTMGR

Remove disks or other media.ÿ

Disk errorÿ

Press any key to restart

¬ËØ Uª


How to interpert this output???
What exactly do you want to do with sectors? Dump or modify them?
I want to read the FAT32 table and then use this table to list the files and folders persent in the drive.
I know there are ways to do it using win32 api. But I want to do it manually at low level.

And further try to open and read files going from one sector to another sector.

Can you give me some hint to start???
Examining FAT boot sector:
http://www.codeguru.com/cpp/cpp/cpp_mfc/files/article.php/c13809/

FAT Root Directory Structure on Floppy Disk and File Information:
http://www.codeguru.com/cpp/cpp/cpp_mfc/files/article.php/c13831/

Long File Name (LFN) Entries in the FAT Root Directory of Floppy Disks:
http://www.codeguru.com/cpp/cpp/cpp_mfc/files/article.php/c13907/

Hope this helps


Topic archived. No new replies allowed.