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

    #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;
    }


    #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;
    }

  • 相关阅读:
    1111实验二 作业调度模拟实验
    1006实验一实验报告
    0909对操作系统的认识
    南阳OJ-138 找球号(二)(hash表应用)
    南阳OJ-38 布线问题(最小生成树应用_prim)
    插入排序
    南阳OJ-756 重建二叉树(二叉树的中序遍历和后序遍历求先序遍历)
    南阳OJ-63 小猴子下落(数据结构-二叉树)
    UVA OJ-11095 Maximum Product(暴力求解法)
    UVA OJ-725 Division (暴力求解法)
  • 原文地址:https://www.cnblogs.com/lzjsky/p/1872839.html
Copyright © 2011-2022 走看看