question

PaikuHan-3271 avatar image
0 Votes"
PaikuHan-3271 asked PaikuHan-3271 commented

Why writing to //./PhysicalDrive0 with _sopen_s() not working

I'm trying to write to physical disk using Microsoft C's _sopen_s() function but it doesn't work at all. Here is my code:

Code-Listing 1:

 #include <stdio.h>
 #include <stdlib.h>
 #include <windows.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <share.h>
 #include <fcntl.h>
 #include <io.h>
    
 int main(int argc, char * argv[])
 {
    
  int drive_descriptor;
    
  if((errno = _sopen_s(&drive_descriptor,"//./PhysicalDrive0", _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE)) != 0)
  {
  printf("Failed to open //./PhysicalDrive0 for writing : %s\n", strerror(errno));
  exit(EXIT_FAILURE);
  }
    
  return(EXIT_SUCCESS);
 }

When I compile and run the code I get, as administrator :

Failed to open //./PhysicalDrive0 for writing : Invalid argument

And without the administrator privilege I get :

Failed to open //./PhysicalDrive0 for writing : Permission denied


Note that PhysicalDrive0 is not the system drive and I'm not interested in using another function like CreateFile(). Only function that look similar to their POSIX counterpart (e.g. open(), write(), etc.) can be used as I want to make my code easily portable across platforms.

Also note that the code should be in C not C++. Unfortunately There is no C tag in Microsoft Q&A.



c++
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered PaikuHan-3271 commented

What happens if you use _O_RDONLY instead of _O_RDWR ?

  • Wayne

· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

When I replace _O_RDWR with _O_RDONLY I only get the error message when not running as Administrator. And if I modify the code like this :

Code-Listing 2:

 if((errno = _sopen_s(&drive_descriptor,"//./PhysicalDrive0", _O_RDONLY, _SH_DENYNO, _S_IREAD | _S_IWRITE)) != 0)
 {
     printf("Failed to open //./PhysicalDrive0 for writing : %s\n", strerror(errno));
     exit(EXIT_FAILURE);
 }
 else
 {
     printf("Successfully opened //./PhysicalDrive0!");
 }

the following message appears:

Successfully opened //./PhysicalDrive0!

when run as Administrator.

0 Votes 0 ·

What kind of disk are you trying to access?

AFAIK, using "//./PhysicalDrive0" is accessing the Master
Boot Record. Perhaps requesting write permission to the
MBR is not allowed.

  • Wayne

0 Votes 0 ·

When I use CreateFile() to open and write to the physical drive (Note that I don't want to use this function for portability reason) like this:

Code-Listing 3:

 int main(int argc, char * argv[])
 {
     HANDLE drive;
     if((drive = CreateFile("//./PhysicalDrive0", GENERIC_ALL, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0)) == INVALID_HANDLE_VALUE)
     {
         printf("Failed to open //./PhysicalDrive0 for writing\n");
     }
    
     byte mbr[512];
     if (!WriteFile(drive, mbr, 512, NULL, 0))
     {
         printf("Failed to write to //./PhysicalDrive0 for writing\n");
     }
     else
     {
         printf("Successfully wrote to //./PhysicalDrive0!\n");
     }
    
     return(EXIT_SUCCESS);
 }


it is writing to the MBR/GPT sector and I get the message :

Successfully wrote to //./PhysicalDrive0!


So it must be some other reason.

0 Votes 0 ·
Show more comments