- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
背景:我正在尝试创建一个可以被多个进程访问的内存映射文件。在下面的代码中,我只放入了与我目前必须使事情变得更简单的问题有关的代码。根据 msdn,我应该能够创建一个文件映射,映射文件的 View 并关闭我从 CreateFileMapping 收到的句柄,而 MapViewOfFile 将使我的 FileMap 保持事件状态。在我 UnmapViewOfFile 之前,FileMap 应该仍然可以访问。
MSDN: CreateFileMapping function
Mapped views of a file mapping object maintain internal references to the object, and a file mapping object does not close until all references to it are released. Therefore, to fully close a file mapping object, an application must unmap all mapped views of the file mapping object by calling UnmapViewOfFile and close the file mapping object handle by calling CloseHandle. These functions can be called in any order.
问题: 在成功映射文件 View 然后关闭 CreateFileMapping 接收到的句柄后,FileMap 不再存在(它应该仍然存在)并且我的 MemMapFileReader 能够创建一个新映射错误 0。(当它应该收到错误 183“已经存在”时)
错误的解决方案:不关闭句柄允许 MemMapFileReader 程序访问它,但会导致 MemMapFileCreator 中的句柄泄漏,因为在进程关闭之前句柄永远不会关闭。
问题我错过了什么或做错了什么?
内存映射文件创建器
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <iostream>
#define BUF_SIZE 256
TCHAR szName[] = TEXT("MyFileMappingObject");
TCHAR szMsg[] = TEXT("Message from first process.");
int _tmain()
{
HANDLE hMapFile;
LPCTSTR pBuf;
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
BUF_SIZE, // maximum object size (low-order DWORD)
szName); // name of mapping object
DWORD lastError = GetLastError();
if (hMapFile == NULL)
{
_tprintf(TEXT("Could not create file mapping object (%d).\n"),
GetLastError());
std::cin.get();
return 1;
}
pBuf = (LPTSTR)MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
_tprintf(TEXT("Could not map view of file (%d).\n"),
GetLastError());
CloseHandle(hMapFile);
std::cin.get();
return 1;
}
CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));
CloseHandle(hMapFile);
_getch();
UnmapViewOfFile(pBuf);
return 0;
}
内存映射文件读取器
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <iostream>
#pragma comment(lib, "user32.lib")
#define BUF_SIZE 256
TCHAR szName[] = TEXT("MyFileMappingObject");
int _tmain()
{
HANDLE hMapFile;
LPCTSTR pBuf;
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE, // read/write access
0,
BUF_SIZE,
szName); // name of mapping object
DWORD lastError = GetLastError();
if (hMapFile == NULL)
{
_tprintf(TEXT("Could not open file mapping object (%d).\n"),
GetLastError());
std::cin.get();
return 1;
}
pBuf = (LPTSTR)MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
_tprintf(TEXT("Could not map view of file (%d).\n"),
GetLastError());
CloseHandle(hMapFile);
std::cin.get();
return 1;
}
MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
std::cin.get();
return 0;
}
最佳答案
来自 MSDN:
Mapped views of a file mapping object maintain internal references to the object, and a file mapping object does not close until all references to it are released. Therefore, to fully close a file mapping object, an application must unmap all mapped views of the file mapping object by calling UnmapViewOfFile and close the file mapping object handle by calling CloseHandle. These functions can be called in any order.
CreateFileMapping 文档指出,要完全关闭文件,必须关闭所有句柄,顺序无关紧要。此逻辑不可逆:您不能关闭一个句柄并期望使用其他句柄,就好像文件映射没有“关闭”一样。
换句话说,这意味着要清理文件映射,您需要按任意顺序关闭所有句柄。但是,您不能关闭底层文件映射对象并仍然使用依赖于它的 View 。
关于c++ - CreateFileMapping、MapViewOfFile、处理泄漏的 c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29542495/
我正在尝试使用 2 个同时运行的线程,并且都使用 FileMapping 功能通过不同的句柄将文件映射到内存中。问题是 MapViewOfFile 函数有时会将两个线程上的映射分配到相同的内存偏移量。
我想将文本写入文件。文本长度未知。所以想不出设置要使用的mapped memory的大小,就设置为100。然后,问题出现了!字符串写入成功,但剩余的100字节空间被NULL填满!!我怎样才能避免它??
使用 win32api,我希望下面的程序创建两个进程并创建一个文件映射。 (使用 C++) 我不知道我应该在 Handle CreateFileMapping(....我试过: PROCCESS_IN
我正在创建一个在不同应用程序之间共享内存的 DLL。 创建共享内存的代码如下所示: #define NAME_SIZE 4 HANDLE hSharedFile; create(char[NAME_S
我在将部分卷映射到内存时遇到问题。卷已成功打开,我可以从中读取,但 CreateFileMapping() 失败。 GetLastError() 返回 87,其中有 following意思是:“参数不
函数CreateFileMapping可用于在页面文件中分配空间(如果第一个参数是INVALID_HANDLE_VALUE)。分配的空间稍后可以内存映射到进程虚拟地址空间。 为什么我要这样做而不是仅使
我正在尝试按照 MSDN 上的教程进行操作:Creating Named Shared Memory . 不幸的是,我从 CreateFileMapping() 得到了 NULL。文件 shmfile
我有两个用 C 编写的窗口窗体应用程序,一个包含一个由两个整数组成的结构,另一个将使用 CreateFileMapping 接收它。 虽然没有直接关系,但我想准备好三个事件,这样每个进程都可以相互“对
只是想知道我是否正在像这样在 Windows 平台上创建共享内存阵列: HANDLE hFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAG
我希望使用命名共享内存实现IPC。 为此,其中一个步骤是使用 CreateFileMapping() 获取映射内存对象的句柄。 我完全按照 MSDN 网站的建议进行操作:http://msdn.mic
我正在开发一个针对桌面系统的应用程序,该系统可能只有 256MB RAM(Windows 2000 及更高版本)。在我的应用程序中,我有一个大文件 (>256MB),其中包含大约 160 字节/每个的
我是第一次尝试使用 CreateFileMapping,当我使用 GetLastError() 时出现此错误: ERROR_INVALID_HANDLE: The handle is invalid.
我按照设计在多个进程之间共享内存数据。 根据设计,应用程序需要多个内存文件,就像每个进程需要一个内存映射文件一样。但是有些进程需要更少的内存映射文件,比如 10KB 或更小。有些进程需要超过 1mb
请你帮我理解在本地/全局命名空间中是否可以有 2 个同名的共享内存对象?我知道,如果我们第一次创建了一个共享内存对象,那么我们需要调用 OpenFileMapping/MapViewOfFile 来打
亲爱的 friend ,我需要你的帮助。 我需要使用 MMF(内存映射文件)将 .bmp 文件发送到另一个进程(对话框)并在那里显示它但问题是图像以相反的颜色和上下颠倒的方式显示。 这是源代码: 在第
我正在使用 this MSDN link 上的教程实现一种将数据从一个进程传输到另一个进程的方法。尽管有人建议我 earlier question要使用 Pipe 方法,由于某些限制,我别无选择,只能
我正在使用共享内存映射进行进程间通信。该映射需要在 session 之间可见,因此我试图在全局命名空间中创建它。我知道正在运行的用户需要 SeCreateGlobalPrivilege 才能使其正常工
我正在尝试使用 CreateFileMapping 和 OpenFileMapping 在进程之间共享内存。这没有像我希望的那样工作 - OpenFileMapping 返回 null 并且 GetL
背景:我正在尝试创建一个可以被多个进程访问的内存映射文件。在下面的代码中,我只放入了与我目前必须使事情变得更简单的问题有关的代码。根据 msdn,我应该能够创建一个文件映射,映射文件的 View 并关
我担心使用内存映射 IO 的危险,通过 CreateFileMapping ,在 FAT 文件系统上。具体场景是用户直接从 U 盘打开文档(是的,你尝试禁止他们这样做!)。 MSDN Managing
我是一名优秀的程序员,十分优秀!