0

Whenever I open a physical drive, I can't use lockfile to lock a byte range on the drive. I always get error code 1.

#include <Windows.h>
#include <stdio.h>

int main()
{
    HANDLE drive = CreateFileA("\\\\.\\PhysicalDrive0",
        FILE_ALL_ACCESS,
        FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL);
    if (drive == INVALID_HANDLE_VALUE)
        return 0;
    if (!LockFile(drive,
        0,
        0,
        512,
        0))
    {
        printf("error: %ld\n", GetLastError());
    }
    return 0;
}

I want to lock the master boot record of the drive, the first 512 bytes.

Arush Agarampur
  • 1,340
  • 7
  • 20
  • 1
    The [Error Code](https://learn.microsoft.com/en-us/windows/desktop/Debug/system-error-codes--0-499-) '1' means `ERROR_INVALID_FUNCTION`. So apparently `LockFile` doesn't support this. Why do you want to lock the MBR? Is it necessary? – zx485 Jan 19 '19 at 00:36
  • 1
    You can't use `LockFile/Ex()` on a physical drive, only on a file. – Remy Lebeau Jan 19 '19 at 00:48
  • the wholea disk mounted by *Filesystem\RAW* because it not formated as known filesystem (formated is partitions on it). but raw file system not support `IRP_MJ_LOCK_CONTROL` - call `IopInvalidDeviceRequest` for it. this return `STATUS_INVALID_DEVICE_REQUEST` . win32 translate it to `ERROR_INVALID_FUNCTION` – RbMm Jan 19 '19 at 01:02
  • *So apparently LockFile doesn't support this.* strictly say `LockFile` translate passed file handle to related device object and call on it `FastIoLock` if this fast io implemented or, if not, send `IRP_MJ_LOCK_CONTROL.IRP_MN_LOCK` to device. the result of such call already device depended. usual only filesystems like ntfs,fat,etc implement this request. the raw file system does not. – RbMm Jan 19 '19 at 01:09

0 Answers0