ReadFile reading sector !!

Mar 22, 2012 at 5:11pm
hi guys

I was wandering what is wrong in my code


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
#include "stdafx.h"
#include <windows.h>
#include <winioctl.h>
#include <iostream>
#include <conio.h>

int main()
{
	  int sector = 1;
	  BYTE bBootSector[512];
          DWORD dwBytesRead;
	  char * d="\\\\.\\C:"; 

	  HANDLE h = CreateFile((LPCWSTR)d,    // Floppy drive to open
                GENERIC_READ,              // Access mode
                FILE_SHARE_READ,           // Share Mode
                NULL,                      // Security Descriptor
                OPEN_EXISTING,             // How to create
                0,                         // File attributes
                NULL);

	  if(h != NULL)
              {
	      SetFilePointer(h,512 * sector,0,FILE_BEGIN);
              // Read the boot sector
              ReadFile(h, bBootSector, 512, &dwBytesRead, 0);
              // Close the handle
	      CloseHandle(h);
              }
	  for ( int i=0;i<512;i++)
		  printf("%X ",(unsigned char)bBootSector[i]);
	  getch();
	  return 0;
}


when ever I execute it print the same output !!!!
I tried changing the sector but nothing happens

PS. Iam using VC++ 2010 and Win7
Mar 22, 2012 at 6:33pm
How to convert a screwdriver into a dinosaur? Easy, that's what casts are for! Or are they?
And what was it your parents taught you about checking return values?
Last, you cannot open the device with exclusive writing access, of course.
But even with all of that fixed, you'd need to run your program with administrator rights.
Mar 24, 2012 at 9:17pm
I did run the program with in administrator rights .... without it the return values are 0s
any solution ?
Mar 24, 2012 at 9:46pm
The solutions:
1. don't use casts when you don't know why you need them, call the right function (CreateFileA) or use wide strings (or rather, the _T macro and TCHAR).
2. Check return values.
3. Don't request exclusive write access.
Mar 25, 2012 at 2:19pm
ok thanks I will try it ......

btw the compiler wont compile and I tells me that can't convert const * char to LPCWSTR >>>> any solution except this casting ?

edit : Thanks man NOW all of it working good ..... The WRITE_SHARE_FLAG was my problem :(
Last edited on Mar 25, 2012 at 3:00pm
Topic archived. No new replies allowed.