title: Read specific sector on hard drive using C language on windows
body: <p>i have tried this code it works when i read a sector from an USB flash drive but it does'nt work with any partiton on hard drive , so i want to know if it's the same thing when you try to read from usb or from hard drive </p>
  
  <pre><code>int ReadSector(int numSector,BYTE* buf){
  
  int retCode = 0;
  BYTE sector[512];
  DWORD bytesRead;
  HANDLE device = NULL;
  
  device = CreateFile("\\\\.\\H:",    // Drive to open
                      GENERIC_READ,           // Access mode
                      FILE_SHARE_READ,        // Share Mode
                      NULL,                   // Security Descriptor
                      OPEN_EXISTING,          // How to create
                      0,                      // File attributes
                      NULL);                  // Handle to template
  
  if(device != NULL)
  {
      SetFilePointer (device, numSector*512, NULL, FILE_BEGIN) ;
  
      if (!ReadFile(device, sector, 512, &amp;bytesRead, NULL))
      {
          printf("Error in reading disk\n");
      }
      else
      {
          // Copy boot sector into buffer and set retCode
          memcpy(buf,sector, 512);
          retCode=1;
      }
  
      CloseHandle(device);
      // Close the handle
  }
  
  return retCode;}
  </code></pre>