2

I am a beginner learning windows programming using C. My program to read boot sectors displays the same output for every drive i.e floppy or harddisk.the program isn't suppose to generate the same output for every drive.

#include <windows.h>
#include <stdio.h>
#include <conio.h>

#pragma pack(1)
struct boot
{
BYTE JUMP[3];
char bsOemName[8];
WORD bytesperSector;
BYTE sectorspercluster;
WORD sectorsreservedarea;
BYTE copiesFAT;
WORD maxrootdirentries;
WORD totalSectors;
BYTE mediaDescripter;
WORD sectorsperFAT;
WORD sectorsperTrack;
WORD sides;
WORD hiddenSectors;
char reserve[480];
WORD volumelabel;
};
void ReadSector(char *src,int ss,int num,void *buff);

void main()
{
struct boot b;
ReadSector("\\\\dell-PC\\c:",0,1,&b);
printf("Boot Sector name: %d\n",b.bsOemName);
printf("Bytes per Sector: %d\n",b.bytesperSector);
printf("Sectors per Cluster: %d\n",b.sectorspercluster);
printf("Total sectors: %d\n",b.totalSectors);
printf("copies FAT: %d\n",b.copiesFAT);
printf("hidden sectors: %d\n",b.hiddenSectors);
printf("volume label: %d\n",b.volumelabel);

}
void ReadSector(char *src,int ss,int num,void *buff)
{
HANDLE h;
unsigned int br;
h=CreateFile(src,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0);
SetFilePointer(h,(ss*512),NULL,FILE_BEGIN);
ReadFile(h,buff,512*num,&br,NULL);
    CloseHandle(h);
}

It is generating the same output for every argument i pass to the ReadSector() function. That is, if i pass d: or e:, the output is always the same. Am I getting garbage values as output?

Boot Sector name: 1637707
Bytes per Sector: 52428
Sectors per Cluster: 204
Total sectors: 52428
copies FAT: 204
hidden sectors: 52428
volume label: 52428 
3
  • For one, you'd probably better check if the result h is equal to INVALID_HANDLE_VALUE Commented Apr 5, 2012 at 21:03
  • Take note of what @wallyk has posted. You will find it 'very difficult' to achieve what you seem to want... Commented Apr 5, 2012 at 21:04
  • You have to check each and every API call for success or failure. You are not doing this. One imagines that the call to CreateFile fails but you will never know unless you check. Read the MSDN topic for each API function carefully. Commented Apr 5, 2012 at 21:24

1 Answer 1

2
  1. Check the return status from each call to determine whether it is giving an error. If it does, you can learn quite a bit about what you are doing wrong by looking up the error code and determining its meaning.

  2. To be sure you are getting values, initialize the structure before using it:

  struct boot b;   
  memset (&b, 0, sizeof b);

I have arbitrarily chosen zero, but any value will do.

Sign up to request clarification or add additional context in comments.

1 Comment

Canonical way to zero initialise a stuct is like this: struct boot b = { 0 };

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.