CreateFileMapping and MapViewOfFile function

In the software development process, the often encounter needs to share data between processes. A calculation data such as process creation, B process graphically display data. This development approach can separate a large program into separate small program, improve the success rate of software can also be more suitable for team development together, accelerate the development of the software. Before using spokenNamed Pipes way to achieveThe following data sharing on ways to use the file mapping. CreateFileMapping first to use the function to create a file handle to want to share data, then use MapViewOfFile to acquire a shared memory address, and then use the name of the shared OpenFileMapping function to open the file in another process, so that you can achieve different processes to share data.

Function CreateFileMapping, MapViewOfFile the following statement:

WINBASEAPI
__out
HANDLE
WINAPI
CreateFileMappingA(
    __in     HANDLE hFile,
    __in_opt LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
    __in     DWORD flProtect,
    __in     DWORD dwMaximumSizeHigh,
    __in     DWORD dwMaximumSizeLow,
    __in_opt LPCSTR lpName
    );
WINBASEAPI
__out
HANDLE
WINAPI
CreateFileMappingW(
    __in     HANDLE hFile,
    __in_opt LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
    __in     DWORD flProtect,
    __in     DWORD dwMaximumSizeHigh,
    __in     DWORD dwMaximumSizeLow,
    __in_opt LPCWSTR lpName
    );
#ifdef UNICODE
#define CreateFileMapping CreateFileMappingW
#else
#define CreateFileMapping CreateFileMappingA
#endif // !UNICODE
 
WINBASEAPI
__out
LPVOID
WINAPI
MapViewOfFile(
    __in HANDLE hFileMappingObject,
    __in DWORD dwDesiredAccess,
    __in DWORD dwFileOffsetHigh,
    __in DWORD dwFileOffsetLow,
    __in SIZE_T dwNumberOfBytesToMap
    );

hFileIt is to create a handle to shared files.

lpFileMappingAttributesFile sharing attributes.

flProtectWhen a file is read-write mapping file attributes.

dwMaximumSizeHighFile sharing is the size of the high byte.

dwMaximumSizeLowFile sharing is the size of the low byte.

lpNameShared file object name.

hFileMappingObjectShared file object.

dwDesiredAccessFile sharing attributes.

dwFileOffsetHighIt is the offset address of the file sharing area.

dwFileOffsetLowIt is the offset address of the file sharing area.

dwNumberOfBytesToMapShared data length.

Examples of calling the function as follows:

//File Sharing.
   void FileMapping(void)
   {
                     // Open the file sharing of objects.
          m_hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE,_T("TestFileMap"));
          if (m_hMapFile)
          {
                                 // display file data sharing.
               LPTSTR lpMapAddr = (LPTSTR)MapViewOfFile(m_hMapFile,FILE_MAP_ALL_ACCESS,
                    0,0,0);
                OutputDebugString(lpMapAddr);
          }
          else
          {
                                 // Create a shared file.
                m_hMapFile = CreateFileMapping( (HANDLE)0xFFFFFFFF,NULL,
                    PAGE_READWRITE,0,1024,_T("TestFileMap"));
 
                                 // copy data to a shared file.
                LPTSTR lpMapAddr = (LPTSTR)MapViewOfFile(m_hMapFile,FILE_MAP_ALL_ACCESS,
                    0,0,0);
                std::wstring strTest(_T("TestFileMap"));
                wcscpy(lpMapAddr,strTest.c_str());                
  
                FlushViewOfFile(lpMapAddr,strTest.length()+1);
          }
   }


 

 

Reproduced in: https: //www.cnblogs.com/yongqiangyue/archive/2012/08/01/3984787.html

Intelligent Recommendation

Win7 failed to use shared memory method CreateFileMapping

When a shared memory method is used to create a memory shared object in a normal application, the CreateFileMapping function always returns a handle of NULL, and returns a error code of 0x5 by GetLast...

C ++ CreatefileMapping Memory Map Memory Map Miss

1. Briefly describe shared memory The principle of sharing memory is to map a physical memory to the virtual address space of different processes, so that each process can read the same data to achiev...

C++ shared memory (1): program A implemented by CreateFileMapping puts data, program B takes data

The latest learning shared memory, online to view some information, I wrote a demo, record here, welcome the passing God to provide valuable advice and suggestions_ The program is divided into CreateF...

C/C++ code for inter-process communication using shared memory under Windows environment---using CreateFileMapping and MapV

                There are many ways to communicate between processes. Last time we talked about the most foolish "shared...

Use CreateFileMapping to communicate between processes and use semaphores to synchronize operations-image transmission between processes

useShared memory + semaphore to communicate between processes to realize image transmission between processes recommendedopencv3.x version, Because opencv4.x does not support the conversion between Ip...

More Recommendation

Visual C ++ network programming classic case explain Chapter 8 network file transfer functions related to memory-mapped file functions CreateFileMapping

Memory-mapped files and virtual memory is similar to the memory address space is only required when the program contents only to the physical disk space Users can CreateFileMapping () to open or creat...

function

Function is to use keywords“function” a defined segment hasIndependent scopeCan beRepeated executionThe statement block, JavaScript functions generally include ordinary functions, variable...

Function (a)

Basic definition Definition: A function is a collection of a set of statements enclosed by a function name. If you want to execute this function, you only need to call its function name. characteristi...

2020/10/9 acwing- monoton

Monoton topic Analysis of Algorithms Source code topic A integer number of lengths having n is given, and the first number of each number is smaller than that, if it does not exist, output -1. Input f...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top