title: CreateFile: direct write operation to raw disk "Access is denied" - Vista, Win7
body: <p>The relevant Microsoft doc is:<br/>
  <a href="http://msdn.microsoft.com/en-us/library/windows/hardware/ff551353%28v=vs.85%29.aspx" rel="nofollow noreferrer">Blocking Direct Write Operations to Volumes and Disks</a><br/>
  <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx#physical_disks_and_volumes" rel="nofollow noreferrer">CreateFile, remarks on Physical Disks and Volumes</a></p>
  
  <p>The executable is written in C++ and it calls <code>CreateFile()</code> to open an SD card that has <strong>no filesystem</strong>. The <code>CreateFile()</code> and consecutive <code>ReadFile()</code> calls are successful for <code>GENERIC_READ</code> without Administrator privileges.</p>
  
  <p><code>CreateFile</code> fails for <code>GENERIC_WRITE</code> even with Administrator privileges. In the explorer, I set Run as Administrator under Properties > Compatibility > Privilege Level. I also tried to run the executable from an Administrator cmd (started with Ctrl+Shift+Enter, "Administrator:" is in the window title, properly elevated). Still, I get <code>ERROR_ACCESS_DENIED</code> (0x5).</p>
  
  <p><strong>Do I have to pass something else to <code>CreateFile</code>?</strong> I have no idea what security attributes are, I just pass NULL, <a href="http://szte-wsn.svn.sourceforge.net/viewvc/szte-wsn/private/ali/RawToCType/Win32DeviceHelper.cpp?revision=2949&amp;view=markup" rel="nofollow noreferrer">relevant code is here</a> at line 92, and <a href="http://szte-wsn.svn.sourceforge.net/viewvc/szte-wsn/private/ali/RawToCType/Win32DriveFormatter.cpp?revision=2940&amp;view=markup" rel="nofollow noreferrer">here</a> at line 48.</p>
  
  <p>Or is there anything else that should be set to run the process with Administrator privileges?
  <hr/>
  A related questions:</p>
  
  <p><a href="https://stackoverflow.com/q/8742150/341970">Can I get write access to raw disk sectors under Vista and Windows 7 in user mode?</a><br/>
  <a href="https://stackoverflow.com/q/224942/341970">Raw partition access in Windows Vista</a><br/>
  <a href="https://stackoverflow.com/q/8541848/341970">How to obtain direct access to raw HD data in C?</a><br/>
  <a href="https://superuser.com/q/227564/76417">Is there a clean way to obtain exclusive access to a physical partition under Windows?</a><br/></p>