zoukankan      html  css  js  c++  java
  • Creating Named Shared Memory

    To share data, multiple processes can use memory-mapped files that the system paging file stores.

    First Process

    The first process creates the file mapping object by calling the CreateFileMapping function with INVALID_HANDLE_VALUE and a name for the object. By using the PAGE_READWRITE flag, the process has read/write permission to the memory through any file views that are created.

    Then the process uses the file mapping object handle that CreateFileMapping returns in a call to MapViewOfFile to create a view of the file in the process address space. The MapViewOfFile function returns a pointer to the file view, pBuf. The process then uses the CopyMemory function to write a string to the view that can be accessed by other processes.

    Prefixing the file mapping object names with "Global\" allows processes to communicate with each other even if they are in different terminal server sessions. This requires that the first process must have the SeCreateGlobalPrivilege privilege.

    When the process no longer needs access to the file mapping object, it should call the CloseHandle function. When all handles are closed, the system can free the section of the paging file that the object uses.

    #include <windows.h>
    #include <stdio.h>
    #include <conio.h>
    #include <tchar.h>
    
    #define BUF_SIZE 256
    TCHAR szName[]=TEXT("Global\\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
     
       if (hMapFile == NULL) 
       { 
          _tprintf(TEXT("Could not create file mapping object (%d).\n"), 
                 GetLastError());
          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);
    
          return 1;
       }
    
       
       CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));
        _getch();
    
       UnmapViewOfFile(pBuf);
    
       CloseHandle(hMapFile);
    
       return 0;
    }
    
    
    

    Second Process

    A second process can access the string written to the shared memory by the first process by calling the OpenFileMapping function specifying the same name for the mapping object as the first process. Then it can use the MapViewOfFile function to obtain a pointer to the file view, pBuf. The process can display this string as it would any other string. In this example, the message box displayed contains the message "Message from first process" that was written by the first process.

    #include <windows.h>
    #include <stdio.h>
    #include <conio.h>
    #include <tchar.h>
    #pragma comment(lib, "user32.lib")
    
    #define BUF_SIZE 256
    TCHAR szName[]=TEXT("Global\\MyFileMappingObject");
    
    int _tmain()
    {
       HANDLE hMapFile;
       LPCTSTR pBuf;
    
       hMapFile = OpenFileMapping(
                       FILE_MAP_ALL_ACCESS,   // read/write access
                       FALSE,                 // do not inherit the name
                       szName);               // name of mapping object 
     
       if (hMapFile == NULL) 
       { 
          _tprintf(TEXT("Could not open file mapping object (%d).\n"), 
                 GetLastError());
          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);
    
          return 1;
       }
    
       MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);
    
       UnmapViewOfFile(pBuf);
    
       CloseHandle(hMapFile);
     
       return 0;
    }
    
    
  • 相关阅读:
    用Asp.Net实现类似DWR的功能
    Icesword FAQ端口 进程 服务篇
    用脚本实时显示Linux网络流量
    为DropDownList 添加optgroup分组以及为ListItem 加式样
    C# 中Treeview无限级目录实现
    .NET 2.0 WinForm Control DataGridView 编程36计(一)
    如何:从 Windows 窗体 DataGridView 控件中移除自动生成的列
    分组显示的select下拉选框
    如何用命令行查找并快速定位ARP病毒母机
    在.NET上如何根据字符串动态创建控件
  • 原文地址:https://www.cnblogs.com/zjoch/p/2086223.html
Copyright © 2011-2022 走看看