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.