Why can not I write to a physical drive in C language?
I ran the code. At first I was successful. But it has been failing ever since.
Here is my code.
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
int main()
{
char cur_drv[100];
wchar_t text1[100];
HANDLE g_hDevice;
DWORD dwRead;
DWORD dwpointer;
char buf[512];
int ret = 0;
sprintf(cur_drv, "\\\\.\\PhysicalDrive%d", 3);
mbstowcs(text1, cur_drv, strlen(cur_drv) + 1);
LPCWSTR test = text1;
for (int i=0; i<512; i++)
buf[i] = '0';
g_hDevice = CreateFile(test, GENNERIC_WRITE | GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTINNG, FILE_ATTRIBUTE_NORMAL, NULL);
dwpointer = SetFilePointer(g_hDevice, (0<<9), (0>>(32-9)), FILE_BEGIN);
if (dwpointer != 0xFFFFFFFF)
ret = WriteFile(g_hDevice, buf, 512, &dwRead, NULL);
CloseHandle(g_hDevice);
return 0;
}
After running the above code, check the hex value. I Checked hex value. At first I was successful. However, it is not written after that. When the ret value is printed, it is initially 1, but after that it is 0. What is the reason?
User contributions licensed under CC BY-SA 3.0