WriteFile()函数成功退出但不在硬盘上写入字节

时间:2016-09-04 14:54:09

标签: c winapi partition

我想直接读写硬盘分区。为此,我通过访问测试分区G:(2​​GB)来使用C.我已成功读取字节扇区。我想从扇区1 读取字节并将它们写入扇区3908880 ,但我无法在磁盘上写入。有趣的是,WriteFile()方法成功执行,但是当我使用 WinHex编辑器来查看字节时。它没有出现。

我看过一些描述特权问题的类似问题,但我没有特权问题,函数执行成功,但没有写字节。

这是我的代码:

HANDLE getDeviceHandle(wchar_t* partition, char mode)
{
HANDLE device;
int retCode = 1;

if (mode == 'r')
{
    device = CreateFile(
        partition,                                  // Partition to open
        GENERIC_READ,                               // Access mode
        FILE_SHARE_READ | FILE_SHARE_WRITE,         // Share Mode
        NULL,                                       // Security Descriptor
        OPEN_EXISTING,                              // How to create
        0,                                          // File attributes
        NULL);                                      // Handle to template
}
else if(mode == 'w')
{
    device = CreateFile(
        partition,                                  // Partition to open
        GENERIC_READ | GENERIC_WRITE,               // Access mode
        FILE_SHARE_READ | FILE_SHARE_WRITE,         // Share Mode
        NULL,                                       // Security Descriptor
        OPEN_EXISTING,                              // How to create
        0,                                          // File attributes
        NULL);                                      // Handle to template

}

if (device == INVALID_HANDLE_VALUE)
    retCode = -1;

    if(retCode == 1)
        return device;
    else
        return NULL;
}

int WriteSector(HANDLE device ,BYTE* bytesToWrite, DWORD size, int sectorNo )
{
    char buffForPrint[512] = { 0 };
    int Code = 0;
    DWORD byteswritten;
    int NoOfSectorsOnPartition = 0;
    DWORD bytesReturnedSize = 0;

    if (NULL == device)
    {
        printf("Exiting from WriteSector\n");
        return 0;
    }
    else
    {
        int ret = getNoOfSectors(device, bytesReturnedSize);

        if (-1 != ret)
        {
            NoOfSectorsOnPartition = ret;

            if (sectorNo > NoOfSectorsOnPartition)
            {
                printf("Selected sector out of range");
                Code = -1;
                return Code;

            }else
            {
                DWORD status;

                if (!DeviceIoControl(device, IOCTL_DISK_IS_WRITABLE, NULL, 0, NULL, 0, &status, NULL))
                {
                    // error handling; not sure if retrying is useful

                }else if (!WriteFile(device, bytesToWrite, size, &byteswritten, NULL))
                    {
                        printf("Error in writing.Error Code: %i\n", GetLastError());
                        Code = -1;
                        return Code;
                    }
                    else
                    {
                        printf("Sector Written\n");
                        Code = 1;
                    }
            }
        }
    }
    return Code;
}


int main()
{
    static BYTE read[512];  
    HANDLE hand;
    int sector =1;
    hand = getDeviceHandle(L"\\\\.\\G:", 'r');

    if (ReadSector(hand, read, 512, sector) == 1)
    {
       printf("successfully read sector %i\n", sector);
    }

    sector = 3908880;

    hand = getDeviceHandle(L"\\\\.\\G:", 'w');
    if (WriteSector(hand,read,SECTOR_SIZE,sector) == 1)  //SECTOR_SIZE 512
    {
        printf("successfully wrote sector %i\n",sector);
    }

    CloseHandle(hand);              // Close the handle

    getch();
}

0 个答案:

没有答案