`
mywebcode
  • 浏览: 999814 次
文章分类
社区版块
存档分类
最新评论

Chapter17— Memory-Mapping Files 进程间共享数据进行通信

 
阅读更多

第十七章的实例有个利用 Memory-Mapped File 来实现进程间共享数据和通信。

Step1:调用CreateFileMapping 函数创建一个 Memory-Mapped File Object。

该函数的原型如下:

HANDLE WINAPI CreateFileMapping(
  __in          HANDLE hFile,
  __in          LPSECURITY_ATTRIBUTES lpAttributes,
  __in          DWORD flProtect,
  __in          DWORD dwMaximumSizeHigh,
  __in          DWORD dwMaximumSizeLow,
  __in          LPCTSTR lpName
);
  • 第一个参数 hFile 设为INVALID_HANDLE_VALUE 表示你仅是将该 Memory-Mapped File Object 存放在 系统的分页文件中,而不是某个指定的磁盘文件当中。
  • 第二个参数 lpAttributes 是安全属性,一般默认是 NULL
  • 第三个参数 flProtect 是指定文件视图的保护属性的。具体的就参考MSDN,下面实例中就使用PAGE_READWRITE 值,表示有读写权限
  • 第四个参数 dwMaximumSizeHigh ,指定最大文件视图大小中64-bit中的高32位
  • 第五个参数dwMaximumSizeHigh,指定最大文件视图大小中64-bit中的低32位
  • 第六个参数 lpName,指定给文件视图的名字。

Step2:调用MapViewOfFile 函数将文件视图映射入 进程地址空间。

该函数的原型如下:

LPVOID WINAPI MapViewOfFile(
  __in          HANDLE hFileMappingObject,
  __in          DWORD dwDesiredAccess,
  __in          DWORD dwFileOffsetHigh,
  __in          DWORD dwFileOffsetLow,
  __in          SIZE_T dwNumberOfBytesToMap
);

  • 第一个参数 hFileMappingObject 就填入 上面 CreateFileMapping 函数的返回值
  • 第二个参数 dwDesiredAccess ,指定进程的访问权
  • 限,下面实例中使用FILE_MAP_READ | FILE_MAP_WRITE 表示有读写权限,注意此处的权限不能高于试图文件本身的权限,即第一步中指定的 flProtect 指定的权限。
  • 第三个参数 dwFileOffsetHigh,表示视图文件的偏移量64位中的高 32 位
  • 第四个参数 dwFileOffsetLow,表示视图文件的偏移量64位中的低 32 位
  • 第五个参数 dwNumberOfBytesToMap,表示总有多少个字节被映射入该进程的地址空间

Step3:读写 mapping file 内容

第三步中MapViewOfFile 函数返回一个指针,通过该指针就可以实现对 mapping file 的读写,这样进程间就可以进行通信了。


Step4:之后调用UnmapViewOfFile 将Memory-Mapping File 从进程的地址空间中移除


Step5:最后关闭 Memory-Mapping File 句柄


运行过程截图如下



代码示例

#include <windows.h>
#include <TCHAR.H>
#include <stdio.h>

void main()
{
    int choice;
    HANDLE hFile_Map = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4 * 1024, TEXT("MMFSharedData"));

    if (hFile_Map == NULL)
    {
        printf("Failed to create file mapping!\n");
        getchar();

        return;
    }
    else if (GetLastError() == ERROR_ALREADY_EXISTS)
    {
        printf("Mapping already exists - not created.\n");
        getchar();
    }

    // File mapping created successfully.

    // Map a view of the file into the address space.
    PVOID pView = MapViewOfFile(hFile_Map, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
    if (NULL == pView)
    {
        printf("Failed to map view of file.\n");
        getchar();

        return;
    }

    while (true)
    {
        getchar();
        system("cls");
        printf("\n\t1.写入 mapping file 内容.");
        printf("\n\t2.读取 mapping file 内容.");
        printf("\n\t3.退出程序");
        printf("\n\n请输入你的选择序号:");
        scanf("%d", &choice);

        if (choice == 3)
      break;

    switch (choice)
    {
    case 1:
      printf("输入要写入的内容:");
      scanf("%s", pView);
      break;
    case 2:
      printf("显示已写入的内容:");
      printf("%s\n", pView);
      break;
        default:
            printf("你的选择序号有误。\n");
            break;
        }
        getchar();
    }
    
    UnmapViewOfFile(pView);
    CloseHandle(hFile_Map);    
}

《windows核心编程》(笔记)系列文章是本人看《windows核心编程》时的一些学习笔记,有疏忽之处,欢迎各位网友指正。QQ邮箱:job.zhanghui@qq.com

分享到:
评论

相关推荐

    UNIX Network Programming Volume 2(Unix网络编程卷2英文版)

    12.4 4.4bsd anonymous memory mapping 315 12.5 svr4/dev/zero memory mapping 316 12.6 referencing memory-mapped objects 317 12.7 summary 322 chapter 13. posix shared memory 13.1 introduction 325 ...

    微软内部资料-SQL性能优化2

    This allows a process to access any portion of the physical memory by mapping it into the applications window. When used in combination with Intel’s Physical Addressing Extensions (PAE) on Windows ...

    BSD4.4设计手册.7z

    The first two implementations by Ken Thompson had no memory mapping, and thus made no hardware-enforced distinction between user and kernel space Ritchie, 1988. A message-passing system could have ...

    深入理解LINUX内核(影印版)(第3版)

    Chapter 17. Page Frame Reclaiming Section 17.1. The Page Frame Reclaiming Algorithm Section 17.2. Reverse Mapping Section 17.3. Implementing the PFRA Section 17.4. Swapping Chapter 18. The Ext2 and ...

    数据库系统概念Database System Concept(英文第6版)文字版

    Chapter 17 Database-System Architectures 17.1 Centralized and Client–Server Architectures 769 17.2 Server System Architectures 772 17.3 Parallel Systems 777 17.4 Distributed Systems 784 17.5 Network ...

    Metal.编程向导英文版.and.Reference.via.Swift.pdf.zip

    Chapter 17: Machine Vision 241 How a Computer Sees the World 241 Noise and Smoothing 242 Sobel Edge Detection 244 Thresholding 245 Histograms 246 Facial Recognition 246 Summary 253 Chapter 18:...

    eac3to V3.17

    * optimized memory allocation * fixed: adding subtitle caption count to filenames sometimes didn't work * fixed: subtitle caption counts in log sometimes had wrong track numbers * fixed: all non-...

    Python Cookbook, 2nd Edition

    Chapter 1. Text Introduction Recipe 1.1. Processing a String One Character at a Time Recipe 1.2. Converting Between Characters and Numeric Codes Recipe 1.3. Testing Whether an Object Is ...

    The Busy Coders Guide to Android Development最终版2019

    Each bullet shown below represents a chapter. Use the search field in the nav bar to search all of the CommonsWare books to see what they hold! Key Android Concepts Choosing Your Development ...

    Geoserver Beginner`s Guide

    Representing the world 17 Time for action – exploring OpenStreetMap 20 Adding more colors to your maps 23 Choropleth maps 23 Proportional maps 25 Time for action – making your thematic map 26 ...

    pro_apache_third_edition..pdf

    Memory..............................................................................................................31 Network Interface...................................................................

    Microprocessor Design Vhdl.pdf

    Example: Designing a Car Security System.................................................................................................17 2.10 Introduction to VHDL.....................................

Global site tag (gtag.js) - Google Analytics