title: Modifying the MBR of Windows
body: <p>I need to modify the MBR of Windows, and I would really like to do this from Windows.</p>
  
  <p>Here are my questions. I know that I can get a handle on a physical device with a call to CreateFile. Will the MBR always be on \\.\PHYSICALDRIVE0? Also, I'm still learning the Windows API to read directly from the disk. Is readabsolutesectors and writeabsolutesectdors the two functions I'm going to need to use to read/write to the disk sectors which contain the MBR?</p>
  
  <p>Edit from from what I've learned on my own.
  The MBR will not always be on \\.\PHYSICALDRIVE0. Also, you can write to the bootsector (at least as Administrator on XP) by call CreateFile with the device name of the drive that contains the MBR. Also, you can write to this drive by simply calling WriteFile and passing the handle of the device created by calling CreateFile.</p>
  
  <p>Edit to address Joel Coehoorn. 
  I need to edit the MBR because I'm working on a project that needs to modify hardware registers after POST in BIOS, but before Windows will be allowed to boot. Our plan is to make these changes by modifying the bootloader to execute our code before Windows boots up. </p>
  
  <p>Edit for Cd-MaN.
  Thanks for the info. There isn't anything in your answer, though, that I didn't know and your answer doesn't address my question. The registry in particular absolutely will not do what we need for multiple reasons. The big reason being that Windows is the highest layer among multiple software layers that will be running with our product. These changes need to occur even before the lower levels run, and so the registry won't work. </p>
  
  <p>P.S. for Cd-MaN.
  As I understand it, the information you give isn't quite correct. For Vista, I think you can write to a volume if the sectors being written to are boot sectors. See <a href="http://support.microsoft.com/kb/942448" rel="nofollow noreferrer">http://support.microsoft.com/kb/942448</a></p>