You're in a tight spot when you hit ERROR_DIRECT_ACCESS_HANDLE (0x00000082). It usually happens in low-level disk tools, backup software that reads raw volumes, or forensic apps. A classic trigger: you're writing a tool that opens \\.\C: with CreateFile, and then you try to call something like ReadFile expecting it to work like a normal file. It won't. The OS is telling you the handle you got is for a partition, but you're trying to use it for something that isn't raw disk I/O — like seeking to a file offset or using it with a regular file API.
What's Actually Going On?
When you open a partition with CreateFile and the share mode flags, Windows gives you a handle that allows raw reads and writes of sectors. But that's it. It's not a file handle in the traditional sense — no directory lookups, no file caching, no standard file operations.
The error comes from the I/O manager when it sees a request on that handle that isn't in the allowed set. For example, if you try to use DeviceIoControl with a control code that isn't partition-related, or if you pass that handle to functions like GetFileInformationByHandle, you'll get this error.
Root Cause in Plain English
Windows separates raw disk handles from file handles. A partition handle only supports raw I/O — reading and writing clusters directly. The moment you ask it to do something that requires a file system context, the OS slaps you with 0x82.
Fix: Use the Right Handle for the Right Job
If you're writing code that needs to touch the partition raw, you have to stick to raw APIs. Here's my step-by-step approach:
- Open the partition correctly. Use
CreateFilewithGENERIC_READ | GENERIC_WRITE, and don't forget theFILE_SHARE_READ | FILE_SHARE_WRITEflags. You also needOPEN_EXISTINGand leave the template null. If you're only reading, skipGENERIC_WRITEto avoid lock issues. - Check the handle type. After opening, call
GetFileType. If it returnsFILE_TYPE_DISK, good. But that's not enough — you need to make sure you're using raw I/O functions exclusively. - Use only ReadFile and WriteFile for raw sector access. These work fine with partition handles. Just make sure your byte offsets are aligned to sector boundaries. Mismatched alignment can cause weird errors that look like this one.
- Avoid non-raw APIs. Don't call anything like
SetFilePointerwith random offsets, orDeviceIoControlwith file system control codes. Stick toFSCTL_LOCK_VOLUMEandFSCTL_DISMOUNT_VOLUMEif you need to unmount first. - If you need file-level access, open the volume differently. Instead of
\\.\C:, open\\.\C:\somefilewith normal file APIs. That gives you a regular file handle.
Real-World Scenario: Why This Happens in Backup Tools
I've seen this exact error in a custom backup agent that opened a partition to read the MBR, then tried to use the same handle to read a file's content. The developer reused the handle to save code. Don't do that. Open two separate handles: one for raw partition access and one for file access.
Still Failing? Check These
- Driver interference. If you're using a filter driver, it might be intercepting IRPs incorrectly. Try disabling it temporarily to test.
- Handle duplication. If you duplicate the handle with
DuplicateHandle, the new handle might not carry the same access rights. UseGetHandleInformationto verify. - Antivirus or security software. Some security tools hook disk APIs and can break raw I/O. Run your code on a clean VM to rule that out.
- Check your privileges. You need
SeBackupPrivilegeand sometimesSeManageVolumePrivilegefor full access. Without them, you might get a different error, but it's worth verifying.
That's the meat of it. Fix your handle usage and the error disappears. If it still happens, you're dealing with a deeper system issue, and I'd start by checking drivers.