0

I want to print out boot sector using code below, but there is mistake.

#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <math.h>


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_READ,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;
int b = 1;

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;
else printf("%02hhX\n",(unsigned int)(unsigned char)buff[511]);

printf("\n");
while (b<513)
{
  if (b%16==0)
     printf(" %02hhX\n",(unsigned int)(unsigned char)buff[b-1]);

  else
     printf (" %02hhX ",(unsigned int)(unsigned char)buff[b-1]);
  b++;
}
getchar(); 
}

Instead of printing hexadecimal digits of boot sectors, microsoft visual studio prints out stream of "CDs". What is the mistake and how to fix the problem? Can anyone help?

Photo of output

Machavity
  • 30,841
  • 27
  • 92
  • 100
  • 3
    You should check if all your function calls succeed. For example, `ReadFile` might be failing, in which case `buf` is just uninitialized memory ([which MSVC initializes to `0xCDCDCDCD...` or some other value for debug builds](https://stackoverflow.com/questions/370195/when-and-why-will-an-os-initialise-memory-to-0xcd-0xdd-etc-on-malloc-free-new)). – Cornstalks May 27 '17 at 16:56
  • I checked the value of _buff in ReadFile and buff in ReadSect using debugger in MSVC and it shows a stream of unknown signs. After converting it into hex value, it showed CDCDCDCD...as output, which means that ReadFile or ReadSect doesn't work... – Nimportequi May 27 '17 at 17:30
  • 2
    That's not what I meant by making sure all your function calls succeed. For example, [`ReadFile` returns a `BOOL` where `TRUE` means success and `FALSE` means failure](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365467(v=vs.85).aspx). You should be checking the status of all your function calls to see if they return any errors. – Cornstalks May 27 '17 at 19:25
  • I checked function call for ReadFile, it returns true. But hDisk returns 0xffffffff, program goes through if condition and closes handle. What is considered as INVALID_HANDLE_VALUE? 0xffffffff is obviously invalid handle value and matches if condition... – Nimportequi May 28 '17 at 04:44
  • GetLastError() function returns 6, which means INVALID_HANDLE_VALUE. Any idea to fix it? – Nimportequi May 28 '17 at 11:30
  • `INVALID_HANDLE_VALUE` is `0xffffffff` (for 32-bit systems). FYI, If `CreateFile()` returns `INVALID_HANDLE_VALUE`, you don't need to close the (invalid) handle. Trying to close an invalid handle doesn't actually make sense. If `CreateFile()` is failing, you should check the value of `GetLastError()` immediately after it (before calling other functions). – Cornstalks May 28 '17 at 16:26

1 Answers1

1

First of all, start it as Administrator

remove space from

char *dsk=" \\\\.\\PhysicalDrive0"; 

must be

char *dsk="\\\\.\\PhysicalDrive0";

And, if you use char *dsk, make modification in:

hDisk=CreateFile(_dsk,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0);

must be: CreateFileA (......)

#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <math.h>


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 = CreateFileA( _dsk, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
    //CreateFile()
    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;
    int b = 1;

    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;
    else printf("%02hhX\n", (unsigned int)(unsigned char)buff[511]);

    printf("\n");
    while (b<513)
    {
        if (b % 16 == 0)
            printf(" %02hhX\n", (unsigned int)(unsigned char)buff[b - 1]);

        else
            printf(" %02hhX ", (unsigned int)(unsigned char)buff[b - 1]);
        b++;
    }
    getchar();
}

photo of output

WinAPI Unicode and ANSI functions

Mihail Kuznesov
  • 555
  • 2
  • 13