Ali Ali - 4 months ago 23
C Question

CreateFile: direct write operation to raw disk "Access is denied" - Vista, Win7

The relevant Microsoft doc is:

Blocking Direct Write Operations to Volumes and Disks

CreateFile, remarks on Physical Disks and Volumes

The executable is written in C++ and it calls

CreateFile()
to open an SD card that has no filesystem. The
CreateFile()
and consecutive
ReadFile()
calls are successful for
GENERIC_READ
without Administrator privileges.

CreateFile
fails for
GENERIC_WRITE
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
ERROR_ACCESS_DENIED
(0x5).

Do I have to pass something else to
CreateFile
?
I have no idea what security attributes are, I just pass NULL, relevant code is here at line 92, and here at line 48.

Or is there anything else that should be set to run the process with Administrator privileges?


A related questions:

Can I get write access to raw disk sectors under Vista and Windows 7 in user mode?

Raw partition access in Windows Vista

How to obtain direct access to raw HD data in C?

Is there a clean way to obtain exclusive access to a physical partition under Windows?

Answer

It's quite rare to want only GENERIC_WRITE. You most likely want GENERIC_READ|GENERIC_WRITE.