无法为我刚刚打开的文件创建文件映射,出现“找不到路径”

发布于 2025-01-01 13:37:05 字数 606 浏览 4 评论 0原文

我正在尝试使用 CreateFileMapping() 函数(在 Windows 上)。

现在,在这段代码中:

hFile = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_DELETE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 
hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, sizeInBlocks*SO_BLOCK_SIZE, filename);

文件已正确创建,但 hFileMapping 的值为 NULL!这很奇怪,因为我正在使用 hFile (这是一个有效的句柄!),并且当我在调用 CreateFileMAPing()< 后检查 GetLastError() 状态时/code> 我收到ERROR_PATH_NOT_FOUND,这很奇怪,因为另一个函数只是用它来创建文件。

有什么想法吗?

I'm trying to the CreateFileMapping() function (on windows).

Now, in this piece of code:

hFile = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_DELETE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 
hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, sizeInBlocks*SO_BLOCK_SIZE, filename);

The file is created allright, but the value of hFileMapping is NULL! Which is weird because I'm using hFile (which is a valid handle!) and when I check GetLastError() status after the call to CreateFileMApping() I'm getting ERROR_PATH_NOT_FOUND, which is weird, because another function just used it to create the file.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

浅忆 2025-01-08 13:37:05

好吧,一直在做一些挖掘(我知道应该提前进行)。 CreateFileMapping() 的最后一个参数应该是不包含反斜杠的字符串。我使用了创建的文件路径作为该参数(以使其成为唯一的名称)。因此,返回的错误实际上应用于该参数,而不是应用于作为第一个参数传递的文件句柄。

Ok, been doing some digging (should've been beforehand, I know). The last parameter of CreateFileMapping() should be a string NOT containing backslashes. And I've used the filepath I've created as that parameter (to make it a unique name). So, the error returned was actually applying to that parameter and NOT to the file handle passed as the first parameter.

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