yunate
yunate

Reputation: 79

Direct writing a disk results in error code 5 according to GetLastError

I am trying to direct write a USB disk in Windows. I used the CreateFile function to create a disk handle and used the WriteFile function to try to write the file. The WriteFile function failed with status code 5 (according to GetLastError). I tried to lock the disk volume by using FSCTL_DISMOUNT_VOLUME, but the volume was hidden, so I could not get its volume letter.

Code:

    u8 Disk::Init()
    {
        char fn[24] = { 0 };
        sprintf(fn, "\\\\.\\PhysicalDrive%d", (int)m_nDiskNum);
        m_hDisk = CreateFileA(fn, GENERIC_WRITE | GENERIC_READ,
            FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
            OPEN_EXISTING,
            FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, 0);

        if (INVALID_HANDLE_VALUE == m_hDisk)
        {
            return (FAIL);
        }

        //m_DiskInfo.Init();
        return Lock();
    }

    u8 Disk::Lock()
    {
        STORAGE_DEVICE_NUMBER d1;
        DWORD nr;
        char dn[8], c;

        if (!DeviceIoControl((HANDLE)m_hDisk, IOCTL_STORAGE_GET_DEVICE_NUMBER,
            NULL, 0, &d1, sizeof(d1), &nr, 0))
        {
            return (FAIL);
        }

        strcpy(dn, "\\\\.\\A:");

        // some volume is hided, so I cannot get its letter
        for (c = 'C'; c < 'Z'; c++)
        {
            HANDLE hd;
            STORAGE_DEVICE_NUMBER d2;

            dn[4] = c;
            hd = CreateFileA(dn, GENERIC_READ | GENERIC_WRITE,
                FILE_SHARE_READ | FILE_SHARE_WRITE,
                NULL, OPEN_EXISTING, 0, 0);

            if (hd == INVALID_HANDLE_VALUE)
            {
                continue;
            }

            if ((!DeviceIoControl(hd, IOCTL_STORAGE_GET_DEVICE_NUMBER,
                NULL, 0, &d2, sizeof(d2), &nr, 0)) ||
                (d1.DeviceType != d2.DeviceType) ||
                (d1.DeviceNumber != d2.DeviceNumber))
            {
                CloseHandle(hd);
                continue;
            }

            if (!DeviceIoControl(hd, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0,
                &nr, 0))
            {
                CloseHandle(hd);
                return (FAIL);
            }

            m_LockedVolums.push_back(hd);
        }

        return (SUCC);
    }

    u8 Disk::UnLock()
    {
        for (auto it : m_LockedVolums)
        {
            CloseHandle(it);
        }

        m_LockedVolums.clear();
        return (SUCC);
    }

    u8 Disk::WriteSector(_Out_opt_ u8 * cBuf, int nStartSec, int nSecCont/* = 1*/)
    {
        if (Seek(nStartSec))
        {
            return(FAIL);
        }

        unsigned long nWrited;

        if ((!WriteFile((HANDLE)m_hDisk, cBuf, nSecCont * m_nBytePerSec, &nWrited, NULL))
            || (nSecCont * m_nBytePerSec != (int)nWrited))
        {
            return(FAIL);
        }

        return(SUCC);
    }

Upvotes: 1

Views: 872

Answers (1)

Soonts
Soonts

Reputation: 21926

I don’t think you’re properly locking the volume.

FSCTL_DISMOUNT_VOLUME IO control dismounts the file system, but I don’t think it locks it for writing. Use FSCTL_LOCK_VOLUME afterwards (or instead, if the volume has no Windows-readable filesystem and therefore wasn’t mounted) to request write access.

P.S. I think you need your app to run elevated to be able to use that API.

Upvotes: 0

Related Questions