I have a FileMapping class that allows me to also lock a file for exclusive use by my process by using the Win32 API function LockFileEx().
bool FileMapping::lockFile(bool wait) {
if (isFileLocked())
return true;
// We want an exclusive lock.
DWORD flags = LOCKFILE_EXCLUSIVE_LOCK;
// If we don't want the thread to block, we have to set the appropriate flag.
if (!wait)
flags |= LOCKFILE_FAIL_IMMEDIATELY;
m_isFileLocked = LockFileEx(m_fileDesc, flags, 0, (DWORD) m_mappingLength, (DWORD) (((uint64_t) m_mappingLength) >> 32), NULL);
return m_isFileLocked;
}
Whenever I get to the LockFileEx() call I get an access violation:
Unhandled exception at 0x7466c2ec in tftpServer.exe: 0xC0000005:
Access violation reading location 0x00000008.
The file handle m_fileDesc is definitely a valid handle (mapping the file into memory with that handle works) and m_mappingLength is just a size_t containing the length of the mapped file portion in bytes.
Does anybody have an idea how to fix this?
Your last argument is
NULL, while it should be a pointer to aOVERLAPPEDstructure. The error about reading location 0x00000008 probably corresponds to the documented requirement that:Given the
hEventmember comes after two pointers, in 32 bit compilation it’d be 8 bytes from the beginning of the structure. SoLockFileExis probably trying to read the hEvent member, and crashes.