I'm trying to write information directly to disk. There are two handlers: _hFileSource, _hFileDest.
_hFileSource - points to some file. _hFileDest - points to PhysicalDrive1 (\\. \ PhysicalDrive1)

_hFileSource = CreateFile(_sourcePath, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, (_useSystemCache ? FILE_ATTRIBUTE_NORMAL : FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING), 0); _hFileDest = CreateFile(_destPath, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, (_useSystemCache ? FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED : FILE_FLAG_NO_BUFFERING | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED), 0); 

I write to disk in this way

 for (int i = 0; i < 20; i++) { _readFileSize += _blockSize; DeviceIoControl(_hFileDest, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &status, NULL); SetLastError(0); SetFilePointer(_hFileDest, i*512, NULL, FILE_BEGIN); WriteFile(_hFileDest, _cacheBuffer->GetBufferData() + i*512, 512, 0, NULL); DeviceIoControl(_hFileDest, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, &status, NULL); int test = GetLastError(); _countBlock--; DeviceIoControl(_hFileDest, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &status, NULL); } 

When writing to test, I get error 5. If I write only to the first sector, then the error will be absent. But it will always overwrite 1 sector. If I use SetFilePointer(_hFileDest, 0, NULL, FILE_CURRENT) then it turns out that I only have access to 1 sector. How can I write information to other sectors?

  • 2
    GetLastError for whom came up? - zenden2k
  • Give the value GetLastError() - Cerbo
  • one
    And what's the point of calling GetLastError after three APIs, if you don’t check the return code for any of them? Maybe you do not have WriteFile falls off, but DeviceIoControl. - Vladimir Martyanov Nov.
  • It's just that all the error falls exactly after WriteFile - Valentin Chikunov

0