1

The code below is required by my app to write disk sectors of external USB drives. It works on most Win10 PCs, but it's returning error 5 for permission denied on a couple PCs. I have exclusions created for both Windows Defender and Malwarebytes. There's nothing in the event viewer related to the failure. The read function works without error.

I tried adding calls to FSCTL_LOCK_VOLUME and FSCTL_DISMOUNT_VOLUME, but this doesn't help. Probably not needed anyway since I'm accessing the physical disk after it's been cleaned, and not any volumes.

Any idea what could cause this, or how to resolve?

Would be great to learn if there's any alternate methods of reading and writing disk sectors.

BOOL Partitioner::BlockWrite(wchar_t* devIdentifier, unsigned __int64 lngStartbyte, DWORD bytesToRead, BYTE* buf)
{
    BOOL ret = FALSE;

    HANDLE devHan = CreateFile(devIdentifier, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
    if (devHan != INVALID_HANDLE_VALUE)
    {
        // Seek to the starting block to write
        LARGE_INTEGER startByte;
        startByte.QuadPart  = lngStartbyte;
        SetFilePointer(devHan, startByte.LowPart, (long*)&startByte.HighPart, FILE_BEGIN);

        // Write the data (this is where error 5 is returned)
        DWORD bytesWritten = 0;
        ret = WriteFile(devHan, buf, bytesToRead, &bytesWritten, NULL);
        FlushFileBuffers(devHan);
        CloseHandle(devHan);
    }
    else
    {
        ret = GetLastError();
        wchar_t msg[PATH_BUFFER_SIZE] = {0};
        swprintf_s(msg, WCSIZE_FULL(msg), L"Error= %d, byte= %llu", ret, lngStartbyte);
        mLog->LogError(msg);
    }

    return ret;
}
3
  • Can I confirm whether it is CreateFile or WriteFile that causes ERROR_ACCESS_DENIED. If it can run normally on most PCs and a few are rejected, then I think you need to compare the permission differences between different PCs, otherwise it is difficult for us to reproduce the problem.
    – Zeus
    Sep 23, 2020 at 2:41
  • @Zhu Song, WriteFile is failing. ReadFile is OK. I'm not sure what permissions need to be compared. Sep 23, 2020 at 6:12
  • You can refer to What causes WriteFile to return ERROR_ACCESS_DENIED?
    – Zeus
    Sep 23, 2020 at 6:58

1 Answer 1

1

I found the answer where I wasn't expecting. I thought this had to be something related to how the file handles were being opened. Instead, turning off Real-time protection in the Virus threat protection settings for Windows 10 caused the error 5s to go away. To resolve without disabling real-time protection, you need to add an allowed app exclusion for each of the installed EXEs.

You can do this in code by scripting PowerShell:

string script = "powershell -Command \"Add-MpPreference -ControlledFolderAccessAllowedApplications '" + GetAppPath() + "\\AppServer.exe" + "'";
Process.Start(new ProcessStartInfo() { FileName = "cmd.exe", Arguments = "/c " + script, CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden }).WaitForExit();
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.