|
-
December 30th, 2009, 03:06 AM
#1
Writing directly on a harddisk without using File
Hi all,
How can we write at a given sector in my hard disk?
I have tried it using this code but nothing happens
Code:
HANDLE hFile ;
DWORD dwResult;
hFile = CreateFile (L"\\\\.\\PhysicalDrive3",
GENERIC_WRITE,
0, // do not share
NULL, // default security
CREATE_ALWAYS, // overwrite existing
FILE_ATTRIBUTE_NORMAL, // normal file
NULL);
WriteFile (hFile, TEST_STR, strlen(TEST_STR), &dwResult, NULL);
Can anybody help me in this?
Thanks in advance
IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH
-
December 30th, 2009, 06:45 AM
#2
Re: Writing directly on a harddisk without using File
Forget it. You just can't do this from user mode. And even if you manage to do that somehow at kernel level, you're definitely gonna break your drive's file system.
Best regards,
Igor
-
December 30th, 2009, 09:39 AM
#3
Re: Writing directly on a harddisk without using File
You just can't do this from user mode. And even if you manage to do that somehow at kernel level, you're definitely gonna break your drive's file system.
If you run this code as admin it works fine... and when you use this kind of code, you usually don't need a filesystem. If you do need a filesystem then this is not the way to go, because raw writing doesn't know anything of a filesystem.
-
January 1st, 2010, 08:38 PM
#4
Re: Writing directly on a harddisk without using File
How can we write at a given sector in my hard disk?
I would definitely NOT recommend writing to a hard drive with the following code. What I would suggest is that you do a bit for bit copy from one USB device to another USB device to verify that the code works.
The code sample has not been tested. I'm not sure it even works. But it's a starting point for you. You'll need admin privileges to execute the code.
Finally, keep in mind that you'll have to write full sectors at a time. Writing partial sectors will not work.
Code:
#define _WIN32_WINNT 0x0500 // Needed for IOCTL_DISK_UPDATE_PROPERTIES
#pragma comment( lib, "user32.lib" )
#include <windows.h>
int main (void)
{
OVERLAPPED ovl;
HANDLE hFile;
BOOL bStatus;
DWORD dwPartitions = 16;
DWORD dwStatus = 0, dwBytesReturned, dwBytesWritten, dwNumberSectors = 1, dwSectorSize = 512;
char pBuffer = NULL;
pBuffer = (char) malloc((dwNumberSectors * dwSectorSize) * sizeof(char));
// Load pBuffer here
hFile = CreateFile(
"\\\\.\\PhysicalDrive3",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH,
NULL
);
bStatus = DeviceIoControl(
hFile,
FSCTL_LOCK_VOLUME,
NULL,
0,
NULL,
0,
&dwBytesReturned,
NULL
);
if (!(WriteFile(
hFile,
(LPVOID) pBuffer,
dwNumberSectors * dwSectorSize,
&dwBytesWritten,
&ovl
)))
{
dwStatus = GetLastError();
}
else if (dwBytesWritten != (dwNumberSectors * dwSectorSize))
{
dwStatus = ERROR_WRITE_FAULT;
}
bStatus = DeviceIoControl(
hFile,
FSCTL_DISMOUNT_VOLUME,
NULL,
0,
NULL,
0,
&dwBytesReturned,
NULL
);
bStatus = DeviceIoControl(
hFile,
FSCTL_UNLOCK_VOLUME,
NULL,
0,
NULL,
0,
&dwBytesReturned,
NULL
);
bStatus = DeviceIoControl(
hFile,
IOCTL_DISK_UPDATE_PROPERTIES,
NULL,
0,
NULL,
0,
&dwBytesReturned,
NULL
);
DRIVE_LAYOUT_INFORMATION* pDLI =
(DRIVE_LAYOUT_INFORMATION*)malloc(sizeof(DRIVE_LAYOUT_INFORMATION) + dwPartitions * sizeof(PARTITION_INFORMATION));
DWORD dwBufferSize = sizeof(DRIVE_LAYOUT_INFORMATION) + dwPartitions * sizeof(PARTITION_INFORMATION);
bStatus = DeviceIoControl(
hFile,
IOCTL_DISK_GET_DRIVE_LAYOUT,
NULL,
0,
pDLI,
dwBufferSize,
&dwBytesReturned,
NULL
);
bStatus = DeviceIoControl(
hFile,
IOCTL_DISK_SET_DRIVE_LAYOUT,
pDLI,
dwBytesReturned,
NULL,
0,
&dwBytesReturned,
NULL
);
return bStatus;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|