调用 LockFileEx() 时发生访问冲突

发布于 2024-12-08 16:56:00 字数 976 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事还在继续 2024-12-15 16:56:00

您的最后一个参数是 NULL,而它应该是指向 OVERLAPPED 结构的指针。有关读取位置 0x00000008 的错误可能对应于记录的要求:

您必须将 hEvent 成员初始化为有效句柄或零。

鉴于 hEvent 成员位于两个指针之后,在 32 位编译中,它将距结构开头 8 个字节。因此 LockFileEx 可能正在尝试读取 hEvent 成员,然后崩溃。

Your last argument is NULL, while it should be a pointer to a OVERLAPPED structure. The error about reading location 0x00000008 probably corresponds to the documented requirement that:

You must initialize the hEvent member to a valid handle or zero.

Given the hEvent member comes after two pointers, in 32 bit compilation it'd be 8 bytes from the beginning of the structure. So LockFileEx is probably trying to read the hEvent member, and crashes.

小ぇ时光︴ 2024-12-15 16:56:00

引用您链接到的文档:

lpOverlapped [输入,输出]

<块引用>

指向函数与锁定请求一起使用的 OVERLAPPED 结构的指针。此结构是必需的,包含锁定范围开头的文件偏移量。您必须将 hEvent 成员初始化为有效句柄或零。

所以你最后的论点是错误的。

Quoting the docs you link to:

lpOverlapped [in, out]

A pointer to an OVERLAPPED structure that the function uses with the locking request. This structure, which is required, contains the file offset of the beginning of the lock range. You must initialize the hEvent member to a valid handle or zero.

So your last argument is wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文