调用 LockFileEx() 时发生访问冲突
我有一个 FileMapping 类,它允许我使用 Win32 API 函数 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;
}
每当我进行 LockFileEx()
调用时,我都会遇到访问冲突:
tftpServer.exe 中 0x7466c2ec 处未处理的异常:0xC0000005:
访问冲突读取位置 0x00000008。
文件句柄m_fileDesc
绝对是一个有效的句柄(使用该句柄将文件映射到内存中)并且m_mappingLength
只是一个包含长度的size_t
映射文件部分的字节数。
有人知道如何解决这个问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的最后一个参数是 NULL,而它应该是指向 OVERLAPPED 结构的指针。有关读取位置 0x00000008 的错误可能对应于记录的要求:
鉴于
hEvent
成员位于两个指针之后,在 32 位编译中,它将距结构开头 8 个字节。因此 LockFileEx 可能正在尝试读取 hEvent 成员,然后崩溃。Your last argument is
NULL
, while it should be a pointer to aOVERLAPPED
structure. The error about reading location 0x00000008 probably corresponds to the documented requirement that:Given the
hEvent
member comes after two pointers, in 32 bit compilation it'd be 8 bytes from the beginning of the structure. SoLockFileEx
is probably trying to read the hEvent member, and crashes.引用您链接到的文档:
所以你最后的论点是错误的。
Quoting the docs you link to:
So your last argument is wrong.