关于内存映射MapViewOfFile()的使用

zhuozhe0550 2017-10-16 10:36:40

我开辟了一个1024k的空间,一直循环写数据,但数据超过1024k后就会崩,,
组长告诉我要判断,当大小超出以后,需要扩大文件大小,用MapViewOfFile()修改文件指针偏移量.
我不知道LPVOID WINAPI MapViewOfFile(
  __in HANDLE hFileMappingObject,
  __in DWORD dwDesiredAccess,
  __in DWORD dwFileOffsetHigh,
  __in DWORD dwFileOffsetLow,
  __in SIZE_T dwNumberOfBytesToMap
  );
dwFileOffsetHigh, dwFileOffsetLow这两参数应该怎么写.

int main()
{
HANDLE hFile = CreateFile(_T("1.txt"), GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
if (hFile == INVALID_HANDLE_VALUE) {
puts("fail to create file");
goto fail_open;
}

SetFileValidData(hFile, 1024);

HANDLE hMap = CreateFileMapping(hFile, 0, PAGE_READWRITE, 0, 1024, 0);
if (hMap == NULL) {
puts("fail to create mapping");
goto fail_map;
}

LPVOID pContent = MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 1024);
if (pContent == 0) {
puts("fail to open mapping");
goto fail_view;
}

// 此处是一个循环,一直在往文件里写数据.
char* pStr = (char*) pContent;
strcpy(pStr, "221221221");

UnmapViewOfFile(pContent);
fail_view:
CloseHandle(hMap);
fail_map:
CloseHandle(hFile);
fail_open:
return 0;
}
...全文
1938 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-10-17
  • 打赏
  • 举报
回复
Creating a File-Mapping Object The first step in mapping a file is to open the file by calling the CreateFile function. To ensure that other processes cannot write to the portion of the file that is mapped, you should open the file with exclusive access. In addition, the file handle should remain open until the process no longer needs the file-mapping object. An easy way to obtain exclusive access is to specify zero in the fdwShareMode parameter of CreateFile. The handle returned by CreateFile is used by the CreateFileMapping function to create a file-mapping object. The CreateFileMapping function returns a handle to the file-mapping object. This handle will be used when creating a file view so that you can access the shared memory. When you call CreateFileMapping, you specify an object name, the number of bytes to be mapped from the file, and the read/write permission for the mapped memory. The first process that calls CreateFileMapping creates the file-mapping object. Processes calling CreateFileMapping for an existing object receive a handle to the existing object. You can tell whether or not a successful call to CreateFileMapping created or opened the file-mapping object by calling the GetLastError function. GetLastError returns NO_ERROR to the creating process and ERROR_ALREADY_EXISTS to subsequent processes. The CreateFileMapping function fails if the access flags conflict with those specified when the CreateFile function opened the file. For example, to read and write to the file: Specify the GENERIC_READ and GENERIC_WRITE values in the fdwAccess parameter of CreateFile. Specify the PAGE_READWRITE value in the fdwProtect parameter of CreateFileMapping. File Mapping Size The size of the file-mapping object is independent of the size of the file being mapped. However, if the file-mapping object is larger than the file, the system expands the file before CreateFileMapping returns. If the file-mapping object is smaller than the file, the system maps only the specified number of bytes from the file. The dwMaximumSizeHigh and dwMaximumSizeLow parameters of CreateFileMapping allow you to specify the number of bytes to be mapped from the file. Windows 95: The dwMaximumSizeHigh parameter is not used because it is not supported by the 32-bit file system. The value should be zero. When you do not want the size of the file to change (for example, when mapping read-only files), call CreateFileMapping and specify zero for both dwMaximumSizeHigh and dwMaximumSizeLow. Doing this creates a file-mapping object exactly the same size as the file. Otherwise, you must calculate or estimate the size of the finished file because file-mapping objects are static in size; once created, their size cannot increase or decrease. Windows NT: The size of a file-mapping object backed by a named file is limited by disk space. The size of a file view is limited to the largest available contiguous block of unreserved virtual memory. This is at most 2 GB minus the virtual memory already reserved by the process. Windows 95: The size of a file-mapping object backed by a named file is also limited by disk space. The size of a file view is limited to the largest available contiguous block of unreserved virtual memory in the shared address space. This is at most 1 GB minus the virtual memory in use by other processes, such as 16-bit Windows-based applications or Win32-based applications using file mapping.
paschen 版主 2017-10-17
  • 打赏
  • 举报
回复
zhuozhe0550 2017-10-16
  • 打赏
  • 举报
回复
比如超过1024K后,我再扩1024k,这时候pStr需要重新获取,还是可以继续用现在这个.重新获取的话,MapViewOfFile中那两个参数怎么写

64,644

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧